Text to Hex Converter
Text to hex converter: type or paste text and get its hexadecimal bytes. Choose UTF-8, UTF-16, ASCII, Latin-1 or Windows-1252, then the separator, prefix and letter case. Each character's bytes are listed. Nothing leaves your browser.
Use Text to Hex for Unicode characters; use ASCII to Hex for ASCII character codes.
Hex dump, xxd style (15 bytes)
00000000: 4772 c3bc c39f 652c 20e4 b896 e795 8c Gr....e, ......
How text is converted to hexadecimal
Text is not stored as letters. A computer stores numbers, and a text encoding is the agreement about which number stands for which character. Converting text to hex is therefore two steps: encode the characters into bytes with a chosen encoding, then write each byte as two hex digits. The encoding step is where the differences come from, because the same string can be a different series of bytes depending on the encoding.
The German word Grüße shows both steps. Five characters become seven bytes in UTF-8, because ü and ß are each two bytes long, and the seven bytes are written as 14 hex digits.
| Byte | Hex | Decimal | Part of |
|---|---|---|---|
| 1 | 47 | 71 | G |
| 2 | 72 | 114 | r |
| 3 | C3 | 195 | ü |
| 4 | BC | 188 | ü |
| 5 | C3 | 195 | ß |
| 6 | 9F | 159 | ß |
| 7 | 65 | 101 | e |
The bytes C3 BC are one character. Counting hex digits and dividing by two gives the size in bytes and says nothing about the number of characters, which is also why the output of this converter is longer than the input suggests when the text has accents or symbols.
It helps to keep three things apart. A character is what you see. A code point is the number Unicode assigns to it, written U+00FC for ü. A byte is what ends up in the hex, and the bytes for one code point depend on the encoding. Hex written from code points (00FC) and hex written from UTF-8 bytes (C3 BC) are both legitimate, they answer different questions, and mixing them up is the commonest reason a converted value is rejected by the system that receives it. This converter always produces bytes.
How many bytes one character takes in UTF-8, UTF-16 and Latin-1
UTF-8 uses one byte for the 128 ASCII characters and up to four for the rest. UTF-16 uses two bytes for the first 65,536 code points and four for the others, in pairs called surrogates. Latin-1 and Windows-1252 use one byte for every character they have and cannot represent the rest at all. The characters below were chosen to hit each case.
| Character | UTF-8 | UTF-16 LE | UTF-16 BE | Latin-1 | Windows-1252 |
|---|---|---|---|---|---|
| A U+0041 | 41 | 41 00 | 00 41 | 41 | 41 |
| é U+00E9 | C3 A9 | E9 00 | 00 E9 | E9 | E9 |
| € U+20AC | E2 82 AC | AC 20 | 20 AC | not possible | 80 |
| 中 U+4E2D | E4 B8 AD | 2D 4E | 4E 2D | not possible | not possible |
| 😀 U+1F600 | F0 9F 98 80 | 3D D8 00 DE | D8 3D DE 00 | not possible | not possible |
UTF-16 stores 😀 (U+1F600) as the surrogate pair D83D DE00, which is why it takes four bytes in either order. The euro sign is the usual trap for single-byte encodings: it is 80 in Windows-1252 but does not exist in Latin-1, so the same text can be encodable in one and not in the other. The mapping tables are in the WHATWG Encoding Standard.
Hex output formats: separators, prefixes and letter case
Hex has no single written form. Debuggers print 48 65 21, source code wants 0x48, 0x65, 0x21, URLs want %48%65%21 and databases often want 486521. The bytes are identical; only the packaging differs. Pick the form the destination expects, because a prefix that one parser requires another rejects. The text is Hi!, and the converter produces every one of these from it.
| Format | Output |
|---|---|
| Plain | 486921 |
| Spaces, upper case | 48 69 21 |
| Colons | 48:69:21 |
| Comma and space | 48, 69, 21 |
| 0x prefix | 0x48, 0x69, 0x21 |
| \x escapes | \x48\x69\x21 |
| Percent | %48%69%21 |
Letter case does not change the value, but it matters when strings are compared as text: a1 and A1 are different strings that name the same byte. Hash digests are traditionally written in lower case, memory addresses and color codes often in upper case. If you compare hex from two sources as plain strings, normalize the case first.
Why two tools give different hex for the same text
When the output of two converters disagrees, the cause is almost always one of four things.
- Encoding. One tool used UTF-8 and the other UTF-16, or Latin-1. For plain ASCII text UTF-8 and Latin-1 agree and UTF-16 inserts a 00 byte after each letter.
- Byte order mark. Some tools prepend FF FE (UTF-16 little-endian) or EF BB BF (UTF-8) to the output. This converter does not add one; a mark is written only if it is in your text.
- Line endings. A line break typed on Windows may travel as two characters. A, a break and B give 41 0A 42 with a line feed alone and 41 0D 0A 42 with a carriage return and line feed.
- Normalization. The letter é can be one character, U+00E9, or two, an e followed by a combining accent. They look the same on screen. In UTF-8 the first is C3 A9 and the second is 65 CC 81, so text copied from a file that stores accents in decomposed form (macOS file names do) gives different hex than text typed on a keyboard.
If you need to match hex from another system, find out which of these it uses and select the same options. The decoder on the main page shows the byte table, which is the quickest way to compare two sources byte by byte.
Hex in source code: how languages escape characters
Programming languages let you write a character by its number inside a string, but each one uses a different notation and a different meaning for the digits. A \x escape is a byte or a single code unit; a \u escape is a Unicode code unit or code point. Copying hex from this converter into a string literal works only if the language reads the escape the same way.
| Language | Euro sign | Emoji | Note |
|---|---|---|---|
| C, UTF-8 string literal | \xE2\x82\xAC | \xF0\x9F\x98\x80 | Each \x is one byte of the UTF-8 form |
| Python 3 string | \u20ac | \U0001f600 | \x allowed only up to 0xFF, as a code point |
| Python 3 bytes | b"\xe2\x82\xac" | b"\xf0\x9f\x98\x80" | The same UTF-8 bytes |
| JavaScript | \u20AC | \u{1F600} | Also \uD83D\uDE00 as two code units |
| JSON | \u20ac | \ud83d\ude00 | Code points above FFFF need a surrogate pair |
| URL | %E2%82%AC | %F0%9F%98%80 | Percent-encoding of the UTF-8 bytes |
The practical rule: to paste UTF-8 bytes into C, a Python bytes literal or a URL, use the converter with the 0x or \x prefix and UTF-8 encoding. To write a character into a JavaScript, JSON or Python string, use its code point (the U+ value) with \u, not the UTF-8 bytes, because \xE2\x82\xAC in a Python string is three Latin-1 characters and no euro sign.
Text to hex in Python, JavaScript and the shell
"Hello".encode("utf-8").hex() # '48656c6c6f'
"Hello".encode("utf-8").hex(" ") # '48 65 6c 6c 6f' (separator argument, Python 3.8+)Buffer.from("Hello", "utf8").toString("hex") // '48656c6c6f'
// Browser, no Buffer:
Array.from(new TextEncoder().encode("Hello"), b => b.toString(16).padStart(2, "0")).join(" ") // '48 65 6c 6c 6f'printf 'Hello' | xxd -p # 48656c6c6f
printf 'Hello' | od -An -tx1 # 48 65 6c 6c 6f
([Text.Encoding]::UTF8.GetBytes("Hello") | ForEach-Object { $_.ToString("X2") }) -join " " # 48 65 6C 6C 6FNote that echo Hello | xxd -p without -n or printf adds a trailing 0A, because echo ends its output with a line feed. That extra byte is the most frequent reason a command-line result is one byte longer than the one from a converter.
Where hex-encoded text is used
Hex lets binary-unsafe systems carry arbitrary text. SQL has a hex string literal for exactly this: X'48656C6C6F' is standard SQL, and MySQL also accepts 0x48656C6C6F. PostgreSQL writes binary data as a hex string in the form '\x48656c6c6f' when the type is bytea. In each case the database stores the bytes, and what they mean as text depends on the column's character set when you read them back.
HTML has its own hex notation for characters: the numeric reference H is the letter H and 😀 is the emoji, written by code point rather than by UTF-8 byte. URLs use percent-encoding for the UTF-8 bytes, as in RFC 3986, so the string Grüße appears in an address as Gr%C3%BC%C3%9Fe. Hex also shows up in log files and in debugging output where a string may contain characters that would corrupt the log, so each byte is written out instead.
Checking the result by converting it back
A round trip is the simplest test. Paste the hex into the hex decoder with the same encoding and the original text should come back, and a mismatch tells you which side picked a different encoding. The swap button in the tool does this in one click: the output becomes the input of the reverse direction. The length check is separate: divide the hex digit count by two and compare with the byte count the converter reports under the output.
For text you intend to send over a channel that carries only letters and digits, Base64 is shorter than hex, at four characters for every three bytes against six for hex, and the two forms can be converted into each other. For ASCII-only text, ASCII to hex has the lookup tables.
Questions about converting text to hex
Why is the hex of my text longer than I expected?
Each byte is two hex digits, and each character can be several bytes. Plain English letters are one byte each, so 5 letters give 10 digits. An accented letter is two bytes in UTF-8 and an emoji is four, so a short string with symbols grows quickly.
Which encoding should I choose?
Use UTF-8 unless a system tells you otherwise. It is what web pages, JSON and most APIs expect. Choose UTF-16 for Windows internals and Java or .NET memory dumps, and ASCII or Latin-1 only when the receiving side is limited to one byte per character.
What happens to characters the encoding cannot hold?
The converter reports the character and its position, and writes 3F (a question mark) in its place so the length stays right. A euro sign cannot be written in ASCII or Latin-1, for example. Switch to UTF-8 or Windows-1252, which has the euro sign at 80.
Does a new line add bytes?
Yes. A line break in the input box is one character, a line feed, written 0A. Text copied from a Windows file may contain a carriage return as well, 0D 0A. If your hex has an extra 0A at the end, the input ended with a line break.
Is text to hex the same as encryption?
No. The output is the same data in another notation, and anyone can turn it back into text with a hex decoder. It hides nothing.
