Since MATLAB stores these literals as numbers, you can use them in any context or function where
you use numeric arrays.
Represent Hexadecimal and Binary Values as Text
You can also convert integers to character vectors that represent them as hexadecimal or binary
values using the dec2hex and dec2bin functions. Convert an integer to hexadecimal.
hexStr = dec2hex(255)
hexStr =
'FF'
Convert an integer to binary.
binStr = dec2bin(16)
binStr =
'10000'
Since these functions produce text, use them when you need text that represents numeric values. For
example, you can append these values to a title or a plot label, or write them to a file that stores
numbers as their hexadecimal or binary representations.
Represent Arrays of Hexadecimal Values as Text
The recommended way to convert an array of numbers to text is to use the compose function. This
function returns a string array having the same size as the input numeric array. To produce
hexadecimal format, use %X as the format specifier.
A = [255 16 12 1024 137]
A = 1×5
255
16
12
1024
137
hexStr = compose("%X",A)
hexStr = 1x5 string
"FF"
"10"
"C"
"400"
"89"
The dec2hex and dec2bin functions also convert arrays of numbers to text representing them as
hexadecimal or binary values. However, these functions return character arrays, where each row
represents a number from the input numeric array, padded with zeros as necessary.
Convert Binary Representations to Hexadecimal
To convert a binary value to hexadecimal, start with a binary literal, and convert it to text
representing its hexadecimal value. Since a literal is interpreted as a number, you can specify it
directly as the input argument to dec2hex.
D = 0b1111;
hexStr = dec2hex(D)
hexStr =
'F'
6 Characters and Strings
6-56
Précédent

- 238/1450

Suivant