ASCII to Hex Converter

ASCII to hex converter: enter ASCII text and get one hex byte per character, so ASCII becomes 41 53 43 49 49. Characters outside ASCII are flagged. Pick plain, spaced, 0x or \x output. Conversion happens in your browser.

Use ASCII to Hex for ASCII character codes; use Text to Hex for Unicode characters.

Text to hex
5 characters
5 bytes · ASCII
Ctrl+Enter runs · your input stays in this browser
Hex dump, xxd style (5 bytes)
00000000: 4153 4349 49                             ASCII

ASCII to hex: look up each character's code

ASCII gives every character a number from 0 to 127, and hex is the number written in base 16. To convert ASCII to hex, take each character in turn, find its code and write that code as two hex digits. The word ASCII is five characters and five bytes, so the result is ten digits.

ASCII as ASCII hex
CharacterDecimalHexBinary
A654101000001
S835301010011
C674301000011
I734901001001
I734901001001

Read down the Hex column and the result is 41 53 43 49 49. The second I has the same code as the first because the table is a function of the character alone; position in the text does not matter. Every ASCII character fits in one byte, so the output length is always exactly twice the input length.

One ASCII character becomes two hex digitsH is 72, binary 01001000, split into 0100 and 1000, hex 48; i is 105, binary 01101001, split into 0110 and 1001, hex 69.H= 720100100048hex 48i= 1050110100169hex 69
The upper four bits give the first hex digit and the lower four bits give the second, so no division is needed when you start from the binary form.

Working out the hex by hand

There are two routes when no table is at hand. From the decimal code, divide by 16: the quotient is the first hex digit and the remainder is the second. The letter z is 122, and 122 = 7 × 16 + 10, so the digits are 7 and A, giving 7A. From the binary form, split the eight bits into two groups of four and read each group as a hex digit, as in the diagram: H is 0100 1000, which is 4 and 8, so 48.

With a little practice the anchors do the work without arithmetic. If a letter is in the capitals, its hex is 40 plus its place in the alphabet (A is 1, so 41; Z is 26 = 1A, so 5A). In lower case it is 60 plus the place, so z is 60 + 1A = 7A. For digits it is 30 plus the digit. The only characters that need a lookup are punctuation, and they are few: the space is 20, the full stop 2E, the comma 2C, the hyphen 2D and the slash 2F.

Hex values by kind of character

The 95 printable characters sit in four groups, and the first hex digit tells you which one. The lists below give every character in a group with its hex code, ready to read across. The space comes first at 20; the unprintable codes (00 to 1F and 7F) are on the ASCII table.

Printable ASCII characters and their hex codes, by group
GroupHex rangeCharacters and codes
Digits30 to 390=30 1=31 2=32 3=33 4=34 5=35 6=36 7=37 8=38 9=39
Capital letters41 to 5AA=41 B=42 C=43 D=44 E=45 F=46 G=47 H=48 I=49 J=4A K=4B L=4C M=4D N=4E O=4F P=50 Q=51 R=52 S=53 T=54 U=55 V=56 W=57 X=58 Y=59 Z=5A
Lower case letters61 to 7Aa=61 b=62 c=63 d=64 e=65 f=66 g=67 h=68 i=69 j=6A k=6B l=6C m=6D n=6E o=6F p=70 q=71 r=72 s=73 t=74 u=75 v=76 w=77 x=78 y=79 z=7A
Punctuation, 21 to 2F21 to 2F!=21 "=22 #=23 $=24 %=25 &=26 '=27 (=28 )=29 *=2A +=2B ,=2C -=2D .=2E /=2F
Punctuation, 3A to 403A to 40:=3A ;=3B <=3C ==3D >=3E ?=3F @=40
Punctuation, 5B to 605B to 60[=5B \=5C ]=5D ^=5E _=5F `=60
Punctuation, 7B to 7E7B to 7E{=7B |=7C }=7D ~=7E

Most of the work is memorizing three anchors: 30 for zero, 41 for A and 61 for a. Count forward from the anchor to reach the rest of a run. The letter M is the 13th letter, so it is 41 + 0C = 4D. The digit 7 is 30 + 7 = 37. The letter n is 61 + 0D = 6E.

ascii2hex output formats

The converter writes the same bytes in whichever notation the next step needs. Fixed-width hex without separators is common in protocols and databases. Spaced upper case is what debuggers print. The 0x and \x prefixes paste directly into C, C++, Python and JavaScript source.

ASCII in common hex notations
NotationOutput
Plain, lower case4153434949
Plain, upper case4153434949
Spaced41 53 43 49 49
Colons41:53:43:49:49
0x prefix (C array body)0x41, 0x53, 0x43, 0x49, 0x49
\x escapes (string literal)\x41\x53\x43\x49\x49

Letter case is only a matter of style, but stay consistent inside one file: some parsers accept 0x and not 0X, and plain string comparison treats 4a and 4A as different values.

Line breaks: 0A, 0D and the trailing newline

A line break is a character like any other, and ASCII has two for it. Line feed (0A) ends a line on Unix, Linux and macOS. Carriage return and line feed (0D 0A) end a line on Windows and in network text protocols. Two lines typed in the input box come out as 6F 6E 65 0A 74 77 6F when the break is a line feed alone, and as 6F 6E 65 0D 0A 74 77 6F when it is the Windows pair.

A trailing break at the very end of the input adds a final 0A that is easy to miss on screen. When a hex string is compared with a value from another system and differs by exactly one byte at the end, the break is the first thing to check. Files created by many editors end with a final line feed by convention, while text pasted into a form often does not.

The same character in decimal, binary, octal and hex

Documentation uses all four bases for ASCII codes, and old Unix manuals favor octal. Converting between them is arithmetic on the same number. Hex is the compact one, because two hex digits cover a byte exactly: the first digit is the upper four bits and the second the lower four.

One code in four bases
CharacterDecimalHexOctalBinary
A654110101000001
a976114101100001
0483006000110000
space322004000100000
~1267E17601111110

Binary shows why the layout works. Every printable ASCII byte starts with 0 (it is 7-bit), and the upper-case letter A and the lower-case a differ only in the third bit from the left. Reading the high four bits as a hex digit gives the first digit of the code, which is why 4 and 5 mean capitals and 6 and 7 mean lower case.

Hex prefixes that give away the format of text data

Text formats begin with characters that are easy to spot once you know their hex. A JSON object starts with 7B (the opening brace), usually followed by 22 (a quotation mark), so 7B 22 is the beginning of a key. An XML declaration begins 3C 3F 78 6D 6C, which is the five characters <?xml. An HTML document that opens with a doctype starts 3C 21 44 4F 43 54 59 50 45, the nine characters <!DOCTYPE. A byte order mark in front of any of these (EF BB BF) means the first three bytes are not part of the text.

Knowing these makes a raw dump readable at a glance. If a capture begins 3C 68 74 6D 6C 3E, it is the tag <html>; if it begins 7B 0A 20 20, it is a pretty-printed JSON object with a line feed and two spaces of indentation. The line-feed byte 0A in the third position is the clue that the file was formatted by a program and not minified.

ASCII hex in protocols and file formats

Hex is the usual way to write ASCII bytes when a protocol carries binary-safe text. The NMEA 0183 sentences sent by GPS receivers end in a checksum that is the XOR of every character between the $ and the * sign, written as two hex digits. For the sentence GPGLL,4916.45,N,12311.12,W,225444,A, the characters XOR to 1D, so the line ends *1D. The checksum is itself ASCII hex: the bytes sent are 31 44, the characters 1 and D.

Intel HEX files, used to load firmware into microcontrollers, are ASCII text from start to finish. Each line begins with a colon and writes the record in hex digits, so the bytes on disk are the hex digits' own ASCII codes (the digit 4 stored as 34, the letter F as 46). The format therefore doubles the size of the data and then writes each digit as a byte. Similar conventions appear in SMTP, HTTP chunk sizes (which are hex numbers written in ASCII), and MIME quoted-printable text.

What happens to characters outside ASCII

ASCII has no code for é, ñ, €, or any character from a non-Latin script. A letter such as é has several possible byte forms depending on the encoding (E9 in Latin-1, C3 A9 in UTF-8), so there is no single correct ASCII hex for it. The converter does not pick one: it marks the character, states its Unicode value and its position, writes 3F in its place so the output length matches the input, and tells you to use a wider encoding.

For text that includes such characters, text to hex converts with UTF-8, UTF-16 or Latin-1. To check that a hex string contains only ASCII, run it through hex to ASCII: any byte above 7F is reported. The definition of the 7-bit code is in RFC 20.

ASCII to hex in Python, JavaScript, C and the shell

Python
"ASCII".encode("ascii").hex()                  # '4153434949'
[format(ord(c), "02X") for c in "ASCII"]      # ['41', '53', '43', '49', '49']
JavaScript
[..."ASCII"].map(c => c.charCodeAt(0).toString(16).padStart(2, "0")).join(" ")   // '41 53 43 49 49'
C
const char *s = "ASCII";
for (const char *p = s; *p; p++) printf("%02X ", (unsigned char)*p);   /* 41 53 43 49 49 */
Shell and PowerShell
printf 'ASCII' | xxd -p              # 4153434949
printf 'ASCII' | od -An -tx1         # 41 53 43 49 49

[int[]][char[]]"ASCII" | ForEach-Object { $_.ToString("X2") }   # 41 53 43 49 49

The cast to unsigned char in the C line matters: on platforms where char is signed, a byte above 7F would otherwise print as FFFFFF followed by two digits.

Questions about converting ASCII to hex

What is the hex code for the letter A?

41. Capital A is ASCII 65, and 65 in hexadecimal is 4 × 16 + 1 = 41. Lower case a is 61, and the digit character 0 is 30.

What is the difference between ascii2hex and text to hex?

ASCII to hex is the 128-character case: each character is exactly one byte and the table has 128 rows. Text to hex covers everything Unicode can write, where a character may need up to four bytes. For ASCII-only input the two give the same output.

Why does a space become 20?

The space is a printable character with code 32 in decimal, and 32 is 20 in hex (2 × 16). It is the first printable ASCII character, which is why punctuation and digits follow it at 21 and above.

Can I get the hex without spaces?

Yes. Choose no separator and the output is one continuous string. Spaces, commas, colons and the 0x, \x and % prefixes are all options, and the byte values are the same in each.

What does the converter do with é or €?

They are not ASCII. The converter flags each one with its position and writes 3F, a question mark, in its place. Use the text to hex converter with UTF-8 when the input contains such characters.