Mehmet Ergülcü

Text to hex and back, and why a ? is a bug

· 3 min

gocliencodinghex

A text-to-hex converter sounds like the easiest program there is. I wrote one years ago in JavaFX, and recently rewrote it in Go: HexConverter, a desktop window plus a command line tool, both single binaries without a Java runtime. The rewrite is a good excuse to talk about the one thing the old version got wrong.

A converter has to be reversible

Convert some text to hex and back, and you expect the same text. The Java version could not promise that. Ask it for the Turkish ğ in US-ASCII, which cannot hold that letter, and it quietly wrote ? (3f). Convert that back and you get a question mark instead of ğ. The program reported success and the information was gone.

The Go version refuses instead:

$ hexconv -charset US-ASCII to-hex ğ
hexconv: 'ğ' (character 1) cannot be written in US-ASCII

The exit code is 1, so a script notices. The same letter in a character set that does have it works:

$ hexconv -charset ISO-8859-9 to-hex ğ
f0
$ hexconv -charset UTF-8 to-hex ğ
c49f

The same letter is one byte (f0) in ISO-8859-9 and two bytes (c4 9f) in UTF-8. That is the whole reason this tool asks you for a character set: hex is the bytes, and the bytes depend on the encoding. The supported ones are UTF-8, UTF-16, ISO-8859-1, ISO-8859-9, windows-1254 and US-ASCII.

The command line

$ go install github.com/ergulcu/HexConverter/cmd/hexconv@latest

$ hexconv to-hex "Merhaba"
4d657268616261
$ echo "4d 65 72 68 61 62 61" | hexconv to-text
Merhaba

Without text after the command, the input comes from stdin. The line break that echo adds is not encoded, so pipes behave the way you would hope. The exit code is 0 on success, 1 when the input cannot be converted and 2 for wrong usage.

Hex input is forgiving, bad input is not

Hex shows up in many shapes: pasted from a debugger, a packet dump or a log. All of these mean the same thing, in upper or lower case:

$ echo "48 65 6c 6c 6f" | hexconv to-text
Hello
$ echo "48:65:6C:6C:6F" | hexconv to-text
Hello
$ echo "0x48,0x65,0x6c,0x6c,0x6f" | hexconv to-text
Hello
$ echo "48-65-6c-6c-6f" | hexconv to-text
Hello

Spaces, line breaks, :, ,, - and 0x prefixes between the bytes are accepted. What is not accepted is anything that would force a guess:

$ hexconv to-text 486
hexconv: the hex text has an odd number of digits (3); every byte needs two digits
$ hexconv to-text zz
hexconv: 'z' is not a hexadecimal digit; only 0-9 and a-f are allowed

UTF-16 and the byte order mark

UTF-16 stores each character in two bytes, and the order of those bytes matters. The tool follows what Java does: when encoding, it writes a byte order mark first, and when decoding, input without one is read as big endian.

$ hexconv -charset UTF-16 to-hex Hi
feff00480069
$ echo "feff00480069" | hexconv -charset UTF-16 to-text
Hi
$ echo "00480069" | hexconv -charset UTF-16 to-text
Hi

feff is the byte order mark. The last command has none, and still reads Hi correctly because it assumes big endian.

The window, and the package

The desktop window is built with Fyne. Type text or hex on the left, choose the character set, press Text To Hex or Hex To Text, and Copy Result puts the answer on the clipboard. Fyne needs a C compiler (cgo), which is why the window is a separate program from the command line tool. The command line tool is pure Go and builds anywhere; on Windows it comes out at about 3 MB.

The conversion logic is a Go package, hexconv, that other programs can import, so the window, the command line tool and your own code all share the same rules.

Source code: github.com/ergulcu/HexConverter

© 2026 Mehmet Ergülcü SvelteKit · Tailwind · <1 KB JS