# convert binary to hex

33 views (last 30 days)
sami ullah on 28 Sep 2020 at 11:28
Commented: sami ullah on 28 Sep 2020 at 20:05
I have a binary 256 bit. i.e 1x 256 char.
I have to convert it to hex then to decimal.
How it will be done?

#### 1 Comment

sami ullah on 28 Sep 2020 at 11:35
For example:
binary='1110001111001100111110010000001111010000000001111111100011110011001111100100000011110100000000011111111000111100110011111001000000111101000000000111111110001111001100111110010000001111010000000001111111100011110011001111100100000011110100000000011111000111';
How to convert above binary into Hex, then to decimal.

Stephen Cobeldick on 28 Sep 2020 at 11:46
Edited: Stephen Cobeldick on 28 Sep 2020 at 11:51
>> B = '1110001111001100111110010000001111010000000001111111100011110011001111100100000011110100000000011111111000111100110011111001000000111101000000000111111110001111001100111110010000001111010000000001111111100011110011001111100100000011110100000000011111000111';
>> H = dec2hex(bin2dec(reshape(B,4,[]).')).' % exact
H = E3CCF903D007F8F33E40F401FE3CCF903D007F8F33E40F401FE3CCF903D007C7
>> D = hex2dec(H) % loss of precision due to conversion to floating point!
D = 1.0304e+77

sami ullah on 28 Sep 2020 at 11:57
Thanks
Stephen Cobeldick on 28 Sep 2020 at 13:43
Faster:
>> H = sprintf('%X',[8,4,2,1]*reshape(B-'0',4,[]))
H =
E3CCF903D007F8F33E40F401FE3CCF903D007F8F33E40F401FE3CCF903D007C7
sami ullah on 28 Sep 2020 at 20:05
thanks