Main Content

Unicode and ASCII Values

MATLAB® stores all characters as Unicode® characters using the UTF-16 encoding, where every character is represented by a numeric code value. (Unicode incorporates the ASCII character set as the first 128 symbols, so ASCII characters have the same numeric codes in Unicode and ASCII.) Both character arrays and string arrays use this encoding. You can convert characters to their numeric code values by using various numeric conversion functions. You can convert numbers to characters using the char function.

Convert Characters to Numeric Code Values

You can convert characters to integers that represent their Unicode code values. To convert a single character or a character array, use any of these functions:

  • double

  • uint16, uint32, or uint64

The best practice is to use the double function. However, if you need to store the numeric values as integers, use unsigned integers having at least 16 bits because MATLAB uses the UTF-16 encoding.

Convert a character vector to Unicode code values using the double function.

C = 'MATLAB'
C = 
'MATLAB'
unicodeValues = double(C)
unicodeValues = 1×6

    77    65    84    76    65    66

You cannot convert characters in a string array directly to Unicode code values. In particular, the double function converts strings to the numbers they represent, just as the str2double function does. If double cannot convert a string to a number, then it returns a NaN value.

str = "MATLAB";
double(str)
ans = 
NaN

To convert characters in a string, first convert the string to a character vector, or use curly braces to extract the characters. Then convert the characters using a function such as double.

C = char(str);
unicodeValues = double(C)
unicodeValues = 1×6

    77    65    84    76    65    66

Convert Numeric Code Values to Characters

You can convert Unicode values to characters using the char function.

D = [77 65 84 76 65 66]
D = 1×6

    77    65    84    76    65    66

C = char(D)
C = 
'MATLAB'

A typical use for char is to create characters you cannot type and append them to strings. For example, create the character for the degree symbol and append it to a string. The Unicode code value for the degree symbol is 176.

deg = char(176)
deg = 
'°'
myLabel = append("Current temperature is 21",deg,"C")
myLabel = 
"Current temperature is 21°C"

For more information on Unicode, including mappings between characters and code values, see Unicode.

See Also

| | | | | | | | | | |

Related Topics

External Websites