How do I show the hex or binary representation for an integer or fixed-point variable in MATLAB?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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?
채택된 답변
MathWorks Fixed Point Team
2020년 8월 26일
편집: MathWorks Fixed Point Team
2020년 8월 28일
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
댓글 수: 5
Another possibility is to use dec2hex and/or dec2bin.
>> x = int32(12345);
>> H = dec2hex(x)
H =
'3039'
>> B = dec2bin(x)
B =
'11000000111001'
You can confirm that this is the correct representation by defining a literal constant using that pattern (if you're using release R2019b or later) or using hex2dec or bin2dec (if you're using release R2020a or later.)
>> xH = 0x3039s32
xH =
int32
12345
>> xB = 0b11000000111001s32
xB =
int32
12345
>> xH2 = int32(hex2dec(H))
xH2 =
int32
12345
>> xB2 = int32(bin2dec(B))
xB2 =
int32
12345
And yet another way for hex output
>> k = int32(12345)
k =
int32
12345
>> kh = sprintf('%08x',typecast(k,'uint32'))
kh =
'00003039'
>> k = int32(-12345)
k =
int32
-12345
>> kh = sprintf('%08x',typecast(k,'uint32'))
kh =
'ffffcfc7'
trying to convert a hexidecimal into an octal number.
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'
Currently, dec2base is limited to int64 and uint64 values <= flintmax.
But the trick of converting to a Fixed-Point Designer fi object can still be used.
u = int64(flintmax) + 1
u = int64
9007199254740993
dec2base( fi( u ), 2)
ans = '0000000000100000000000000000000000000000000000000000000000000001'
dec2base( u, 2)
Error using dec2base
First argument must be an array of integers, 0 <= D <= flintmax.
First argument must be an array of integers, 0 <= D <= flintmax.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Test and Debug에 대해 자세히 알아보기
참고 항목
2020년 8월 26일
2023년 3월 22일
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
