Yes, ord returns the ASCII value of a character

The ord function takes a single character and returns the number that represents it in the ASCII table. When you run ord('A'), you get 65. When you run ord('a'), you get 97. Every printable character — letters, numbers, punctuation — has its own ASCII number, and ord retrieves it.

This matters in network programming because data traveling over Ethernet or any wired connection moves as numbers, not as letters. When your computer sends the letter "A" across the network, it actually sends the byte value 65. The ord function is how you convert between what you see on screen and what actually travels through the cable.

Most programming languages have an ord function or something equivalent. Python has it built in. JavaScript uses charCodeAt(). C uses int c = 'A'; directly. The concept is the same everywhere: turn a character into its numeric representation.

Key Takeaways

  • The ord function returns a number between 0 and 127 for standard ASCII characters, with uppercase letters starting at 65 and lowercase at 97.
  • Network data is transmitted as bytes, so converting characters to their ASCII values is necessary when building protocols or parsing incoming data.
  • The reverse operation — converting a number back to a character — uses chr in Python or fromCharCode in JavaScript.
  • Extended ASCII and Unicode handle characters beyond the basic 0-127 range, but ord works the same way with those too.

How ord works in Python

In Python, ord is straightforward. You pass it a single character in quotes, and it returns the integer:

ord('A') returns 65 ord('Z') returns 90 ord('a') returns 97 ord('0') returns 48 ord(' ') returns 32 (space character)

You can use ord in a loop to convert an entire string. If you have a message you need to send over a network socket, you might convert each character to its ASCII value, bundle those numbers together, and transmit them. The receiving end does the reverse with chr, converting numbers back into characters.

Why this matters for network protocols

When you write code that sends data across Ethernet, you are working with bytes. A byte is a number from 0 to 255. Text is not native to networks — only numbers are. The ASCII table is the agreement that lets both sides of the connection understand what number means what character.

If you are building a custom protocol or parsing a network packet, you will encounter situations where you need to know the exact byte value of a character. For example, many network protocols use specific characters as delimiters — a pipe character | might mark the boundary between fields in a message. To search for that delimiter in incoming data, you might look for byte value 124, which is what ord('|') returns.

HTTP headers, DNS queries, and many other network standards rely on ASCII. When you inspect raw network traffic with a tool like Wireshark, you see both the hex representation (the raw bytes) and the ASCII interpretation (what those bytes mean as text). The ord function is the bridge between those two views.

The ASCII table and what values mean

Standard ASCII covers values 0 through 127. Values 0 through 31 are control characters — things like newline (10), carriage return (13), and tab (9). These do not print as visible characters but control how text is formatted. Values 32 through 126 are printable: space (32), punctuation, digits, uppercase letters, lowercase letters, and a few more symbols.

Uppercase letters A through Z occupy 65 through 90. Lowercase a through z occupy 97 through 122. Digits 0 through 9 are 48 through 57. This is why ord('A') is 65 and ord('a') is 97 — they are different numbers, which is why the computer treats them as different characters.

Extended ASCII goes up to 255 and includes accented characters and box-drawing symbols. Unicode extends far beyond that to cover every language and symbol. The ord function works with all of these — ord('é') returns 233 in extended ASCII, and ord('€') returns 8364 in Unicode.

Converting back with chr

The opposite of ord is chr, which takes a number and returns the character. chr(65) returns 'A'. chr(97) returns 'a'. When you receive data from the network as raw bytes, you use chr to turn those bytes back into readable text.

A common pattern is to read bytes from a socket, convert each one with chr, and build a string. Or you might use ord to examine each character of an outgoing message, check its value, and decide what to do with it. Together, ord and chr let you move freely between the text world and the byte world.

Common mistakes when using ord

The most common mistake is passing more than one character. ord('AB') will raise an error because ord expects exactly one character, not a string. If you need to convert a whole string, loop through it: for char in message: print(ord(char)).

Another mistake is assuming ord works only with ASCII. It does not — it works with any character your language supports. But if you are working with a protocol that only understands ASCII (many older network protocols do), you need to handle non-ASCII characters separately, usually by encoding them first.

A third mistake is confusing the character with its string representation. ord('5') returns 53, not 5. The character '5' and the number 5 are different things. The character is a symbol that looks like five; the number is the value itself. When you read '5' from the network, ord tells you it arrived as byte 53.

Frequently Asked Questions

What is the difference between ord and just casting a character to an int?

In Python, ord is the standard way to get a character's numeric value. In languages like C, you can cast directly: int value = (int)'A'; does the same thing. Both work because characters are stored as numbers internally. Python just makes it explicit with a function name.

Does ord work with Unicode characters?

Yes. ord('€') returns 8364, the Unicode code point for the Euro symbol. ord('中') returns 20013 for the Chinese character. ord returns the code point value, which can be much larger than 127 for non-ASCII characters.

What happens if I use ord on a newline or tab character?

ord('\n') returns 10 (newline). ord('\t') returns 9 (tab). These are control characters, not printable ones, but they have ASCII values just like letters do. They are useful in network protocols where you need to mark the end of a line or separate fields.

Can I use ord to check if a character is a letter or a number?

You can, but Python has better tools. char.isalpha() and char.isdigit() are clearer. That said, you could check if ord(char) is between 65 and 90 (uppercase), 97 and 122 (lowercase), or 48 and 57 (digits). For network parsing where you are already working with raw bytes, this approach is common.

What is the highest value ord can return?

In Python 3, there is no practical limit — ord works with any valid Unicode character, and Unicode goes up to code point 1,114,111. In older systems or languages with fixed character sizes, the limit might be 127 (ASCII), 255 (extended ASCII), or 65,535 (16-bit Unicode). Check your language's documentation.