Hexadecimal and Binary Values
You can represent numbers as hexadecimal or binary values. In some contexts, these representations of numbers are more convenient. For example, you can represent the bits of a hardware register using binary values. In MATLAB®, there are two ways to represent hexadecimal and binary values:
As literals. Starting in R2019b, you can write hexadecimal and binary values as literals using an appropriate prefix as notation. For example,
0x2A
is a literal that specifies 42—and MATLAB stores it as a number, not as text.As strings or character vectors. For example, the character vector
'2A'
represents the number 42 as a hexadecimal value. When you represent a hexadecimal or binary value using text, enclose it in quotation marks. MATLAB stores this representation as text, not a number.
MATLAB provides several functions for converting numbers to and from their hexadecimal and binary representations.
Write Integers Using Hexadecimal and Binary Notation
Hexadecimal literals start with a 0x
or 0X
prefix, while binary literals start with a 0b
or 0B
prefix. MATLAB stores the number written with this notation as an integer. For example, these two literals both represent the integer 42
.
A = 0x2A
A = uint8
42
B = 0b101010
B = uint8
42
Do not use quotation marks when you write a number using this notation. Use 0
-9
, A
-F
, and a
-f
to represent hexadecimal digits. Use 0
and 1
to represent binary digits.
By default, MATLAB stores the number as the smallest unsigned integer type that can accommodate it. However, you can use an optional suffix to specify the type of integer that stores the value.
To specify unsigned 8-, 16-, 32-, and 64-bit integer types, use the suffixes
u8
,u16
,u32
, andu64
.To specify signed 8-, 16-, 32-, and 64-bit integer types, use the suffixes
s8
,s16
,s32
, ands64
.
For example, write a hexadecimal literal to be stored as a signed 32-bit integer.
A = 0x2As32
A = int32
42
When you specify signed integer types, you can write literals that represent negative numbers. Represent negative numbers in two's complement form. For example, specify a negative number with a literal using the s8
suffix.
A = 0xFFs8
A = int8
-1
Because MATLAB stores these literals as numbers, you can use them in any context or function where you use numeric arrays. For example, you can create a 64-bit signed integer array without a loss of precision for large integers.
C = [0xFF000000001F123As64 0x1234FFFFFFFFFFFs64]
C = 1x2 int64 row vector
-72057594035891654 81997179153022975
For comparison, when you convert an array of large integers (larger than flintmax
) using int64
, precision can be lost because MATLAB initially represents a numeric array input as double precision by default.
C_inaccurate = int64([-72057594035891654 81997179153022975])
C_inaccurate = 1x2 int64 row vector
-72057594035891656 81997179153022976
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'
If you start with a hexadecimal literal, then you can convert it to text representing its binary value using dec2bin
.
D = 0x8F; binStr = dec2bin(D)
binStr = '10001111'
Bitwise Operations with Binary Values
One typical use of binary numbers is to represent bits. For example, many devices have registers that provide access to a collection of bits representing data in memory or the status of the device. When working with such hardware you can use numbers in MATLAB to represent the value in a register. Use binary values and bitwise operations to represent and access particular bits.
Create a number that represents an 8-bit register. It is convenient to start with binary representation, but the number is stored as an integer.
register = 0b10010110
register = uint8
150
To get or set the values of particular bits, use bitwise operations. For example, use the bitand
and bitshift
functions to get the value of the fifth bit. (Shift that bit to the first position so that MATLAB returns a 0
or 1
. In this example, the fifth bit is a 1
.)
b5 = bitand(register,0b10000); b5 = bitshift(b5,-4)
b5 = uint8
1
To flip the fifth bit to 0
, use the bitset
function.
register = bitset(register,5,0)
register = uint8
134
Since register
is an integer, use the dec2bin
function to display all the bits in binary format. binStr
is a character vector, and represents the binary value without a leading 0b
prefix.
binStr = dec2bin(register)
binStr = '10000110'
See Also
bin2dec
| bitand
| bitshift
| bitset
| dec2bin
| dec2hex
| hex2dec
| sprintf
| sscanf
Related Topics
- Convert Text to Numeric Values
- Convert Numeric Values to Text
- Formatting Text
- Bit-Wise Operations
- Perform Cyclic Redundancy Check