Description
Use this function toreturn the numeric code for the first character in a text string. Supported in Chains.
CODE returns the numeric Unicode character code corresponding to the first character of the provided text string.
Syntax
CODE(text)
Inputs
This function accepts the following argument:
| Name | Required | Description | Valid input |
|---|---|---|---|
text |
Yes | The text string for which you want to find the code of the first character. | A text string, a reference to a cell containing text, or a formula which results in text. |
Example
Sample data
| A | B | |
|---|---|---|
| Character | Description | |
1 |
A | Uppercase A |
2 |
a | Lowercase a |
3 |
1 | Number one |
4 |
! | Exclamation mark |
5 |
Hello | Word |
Sample formulas
| Use case | Formula | Explanation and Result |
|---|---|---|
| Look up the numeric code for an uppercase letter. | =CODE(A2) |
Returns the Unicode value for uppercase "A". Result: 65 |
| Look up the numeric code for a lowercase letter. | =CODE(A3) |
Returns the Unicode value for lowercase "a". Result: 97 |
| Look up the numeric code for a digit stored as text. | =CODE(A4) |
Returns the Unicode value for the character "1". Result: 49 |
| Look up the numeric code for a punctuation character. | =CODE(A5) |
Returns the Unicode value for the exclamation mark. Result: 33 |
| Look up the numeric code for only the first character of a longer word. | =CODE(A6) |
Reads just the first character of "Hello" ("H") and returns its Unicode value. Result: 72 |
Notes
- CODE returns the number representing the character in the Unicode character set.
- If the text argument is empty, CODE returns a #VALUE! error.
- CODE only considers the first character of the text string, even if the string contains multiple characters.
- CODE is often used in combination with CHAR, which performs the reverse operation (converting a number to its corresponding character).
Tips
- You can use CODE to compare characters based on their Unicode values, which can be useful for sorting or filtering sample data.
- Combine CODE with other text functions such asLEFT, MID, or RIGHT to work with specific characters in a string.
- Remember that CODE returns different values for uppercase and lowercase letters, as well as for numbers and special characters.
- To convert between uppercase and lowercase letters, you can use CODE in combination with simple math operations (Example:
CODE("A") + 32gives the code for "a"). - When working with non-ASCII characters, be aware that CODE returns Unicode values, which may be different from ASCII codes for extended characters.