How do I show the hex or binary representation for an integer or fixed-point variable in MATLAB?

188 views (last 30 days)
When designing or debugging integer or fixed-point algorithms, there are many cases where it is helpful for the display of values to show their hexidecimal or binary representations. How do I get the display of a variable in the MATLAB Command Window to show my preference of hex or binary?

Accepted Answer

MathWorks Fixed Point Team
Edited: MathWorks Fixed Point Team on 28 Aug 2020
Use of MATLAB's format command and/or Fixed-Point Designer's fipref object can be use to achieve hex display, binary display, or even octal display.
Hex display method 1: format hex
To have the MATLAB Command Window show hex representations of all types of variables, not just integer and fixed-point, the format command can be used.
format hex
a = int8([35,-3])
b = fi(a)
a =
1×2 int8 row vector
23 fd
b =
23 fd
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 0
But keep in mind that this also affects other variables types like double and single.
c = single(pi)
c =
single
40490fdb
Hex display method 2: fipref.NumberDisplay
For fixed-point fi-objects, an alternate way to have the MATLAB Command Window show hex representations is to use fipref.
format long % restore format for everything else
fpr = fipref;
fpr.NumberDisplay = 'hex'
d = fi([35,127;-1,-128],1,8,0)
fpr =
NumberDisplay: 'hex'
NumericTypeDisplay: 'full'
FimathDisplay: 'full'
LoggingMode: 'Off'
DataTypeOverride: 'ForceOff'
d =
23 7f
ff 80
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 0
Note, that fipref does not affect doubles, singles, int8 or any other class, only fi objects.
a = int8([35,-3])
c = single(pi)
a =
1×2 int8 row vector
35 -3
c =
single
3.1415927
Binary display method: fipref.NumberDisplay
For fixed-point fi-objects, fipref can be used to have the MATLAB Command Window show binary, hex, or even octal representations.
fpr = fipref;
fpr.NumberDisplay = 'bin'
d = fi([35,127;-1,-128],1,8,0)
fpr =
NumberDisplay: 'bin'
NumericTypeDisplay: 'full'
FimathDisplay: 'full'
LoggingMode: 'Off'
DataTypeOverride: 'ForceOff'
d =
00100011 01111111
11111111 10000000
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 0
Quick trick to display binary for MATLAB integers.
To show binary for a MATLAB integer such as int8 or uint64, an easy trick is to use fipref and convert the integer to its fixed-point equivalent. Converting to the fi equivalent is the trivial effort of calling fi()
fpr = fipref;
fpr.NumberDisplay = 'bin';
myInt64Var = int64([14;-1;-5;realmax])
fi(myInt64Var)
myInt64Var =
4×1 int64 column vector
14
-1
-5
9223372036854775807
ans =
0000000000000000000000000000000000000000000000000000000000001110
1111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111011
0111111111111111111111111111111111111111111111111111111111111111
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 64
FractionLength: 0
In R2017a and later, the conversion of a built-in integer to it's fi equivalent does NOT require a Fixed-Point Designer license. However, it does require that your MATLAB installation include MATLAB Coder OR Simulink OR Fixed-Point Designer.
Note: hex or binary display of fi is stored integer
But aware that the binary or hex display is of the fi object's stored integer value. Recall that for binary-point scaling.
RealWorldValue = StoredIntegerValue * 2^-FractionLength
And, in general
RealWorldValue = StoredIntegerValue * Slope + Bias
y = fi( 35*2^-5, 0, 8, 5 )
y =
00100011
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 8
FractionLength: 5
Restoring normal display of values
To restore normal display of values, use MATLAB's format command and fipref.
format long % or short or long g or ...
fpr = fipref;
fpr.NumberDisplay = 'RealWorldValue';
a = int8(37)
b = single(pi)
c = fi( 35*2^-5, 0, 8, 5 )
a =
int8
37
b =
single
3.1415927
c =
1.093750000000000
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 8
FractionLength: 5
  4 Comments
Walter Roberson
Walter Roberson on 27 Dec 2021
If you are not using fixed-point then,
S = 'ffffcfc7';
as_uint64 = sscanf(S, '%lx')
as_uint64 = uint64 4294954951
as_octal = dec2base(as_uint64, 8)
as_octal = '37777747707'

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by