CODE function
The CODE function in Excel returns the ASCII (or Unicode) code of the first character in a text string. This function is useful when you need to know the numerical representation of a character, which can be particularly helpful when working with text manipulation or encoding.
Syntax
=CODE(text)
Parameters
- text (required): The text string or cell reference that contains the character whose ASCII (or Unicode) code you want to retrieve. Only the first character of the string is considered, and the function returns the code for that character.
Key Points
- ASCII Code: The
CODEfunction primarily works with ASCII characters, which are in the range 0 to 255. For example, the letter “A” corresponds to the ASCII code 65, soCODE("A")will return 65. - Unicode Characters: In newer versions of Excel (Excel 2013 and later), the
CODEfunction can also return the Unicode code point for characters outside the standard ASCII range (beyond 255). However, this is limited to characters that can be represented by a single code point (up to 65535). - Returns a Number: The function returns a numeric value corresponding to the character’s code, which can be used for further text analysis or manipulation.
Example Usage
1. Getting the ASCII Code for a Character
To find the ASCII code of the character “A”:
=CODE("A")
This will return:
65
2. Using CODE with Non-Alphabetic Characters
The CODE function works for any character, not just letters. For example, the space character (ASCII code 32):
=CODE(" ")
This will return:
32
3. Getting the ASCII Code of Special Characters
You can use CODE to find the code of special characters like a line break, tab, or other symbols:
- For a line break (CHAR(10)):
=CODE(CHAR(10))This will return:
10 - For a tab character (CHAR(9)):
=CODE(CHAR(9))This will return:
9
4. Using CODE with a Cell Reference
If you have a string in a cell (say A1 contains the text “Hello”) and want to get the ASCII code of the first character:
=CODE(A1)
This will return:
72
(as “H” is the first character, and its ASCII code is 72).
5. Using CODE with Unicode Characters
In newer versions of Excel, the CODE function can also handle Unicode characters (beyond the ASCII range). For instance, the character “😀” (Unicode code point U+1F600):
=CODE("😀")
This may return:
128512
which corresponds to the Unicode code point for the “Grinning Face” emoji.
Notes
- Single Character: The
CODEfunction only looks at the first character in the text string. If you provide a longer string, it will return the ASCII/Unicode code of the first character only. - Compatibility: The
CODEfunction returns values for characters based on the current character encoding (ASCII or Unicode, depending on your version of Excel). In older versions of Excel, it is limited to ASCII codes (1-255). In modern versions, it can return Unicode code points beyond that range. - Control Characters: Some ASCII codes correspond to control characters (like newline or tab). For example,
CODE(CHAR(10))returns10, which represents a line break (newline).
Related Functions
CHAR: The reverse ofCODE. WhileCODEreturns the ASCII or Unicode code for the first character in a string,CHARtakes an ASCII or Unicode code and returns the corresponding character.Example:
=CHAR(65) ' Returns "A"UNICHAR: Similar toCHAR, but for Unicode characters beyond the 255 ASCII range. It allows you to work with a much larger set of characters, such as emoji or other symbols.Example:
=UNICHAR(128512) ' Returns the "Grinning Face" emoji (😀)TEXT: TheTEXTfunction formats numbers or dates as text. While it doesn’t return ASCII codes, it’s useful for formatting text or numeric values when working with codes.
The CODE function is a simple yet powerful tool in Excel for extracting the numeric representation of the first character in a string, useful for text processing and manipulation tasks.