unicode2native

Converts text to bytes in a given character encoding.

Category: Language Fundamentals > Data Types

Syntax

b = unicode2native(str)

Encodes with the default encoding of the system.

b = unicode2native(str, encoding)

Encodes with the named encoding, such as 'UTF-8' or 'ISO-8859-1'.

Description

Text in MATLAB is Unicode, but files and network protocols carry bytes in a specific encoding. unicode2native produces those bytes. In UTF-8, a takes one byte and é takes two (195 169), while in 'ISO-8859-1' both take one byte. The number of bytes is therefore not always the number of characters.

The inverse operation is native2unicode.

Input arguments

str
Character vector or string.
encoding
Name of the character encoding.

Output arguments

b
Row vector of class uint8 with the encoded bytes.

Example

Bytes in two encodings

b = unicode2native('aé', 'UTF-8')
class(b)
unicode2native('é', 'ISO-8859-1')
Output
b = 1×3 uint8 row vector

    97   195   169
ans = 'uint8'
ans = 1×2 uint8 row vector

   195   169

See also

native2unicode char double typecast fopen