-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin2hex.m
87 lines (52 loc) · 1.61 KB
/
bin2hex.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function x = bin2hex(BinStr)
% BIN2HEX (BinStr,...output)
%
% Converts binary strings of any length to hexadecimal pairs. Adds leading
% zeros if there is not an even number of hex bits.
%
% Now also supports cell array inputs.
%
%
% Author: Richard Medlock 2001
% Check to see what the input is:
if iscell(BinStr)
NCells = length(BinStr)
for C = 1:NCells
Str = BinStr{C};
x{C} = doconvert(Str);
end
elseif ~ischar(BinStr)
error('Input must be a string or character or cell array.')
else
x = doconvert(BinStr);
end
%-----------------------------------------------
function out = doconvert(BinStr)
Bits = length(BinStr);
Words = Bits/4;
WholeWords = floor(Words);
PartWords = Words-WholeWords;
while PartWords > 0
BinStr = ['0' BinStr];
Bits = length(BinStr);
Words = Bits/4;
WholeWords = floor(Words);
PartWords = Words-WholeWords;
end
% For each WORD (4-bits), convert it to HEX based on the following rules:
Words = length(BinStr)/4;
HEXi = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
HexStr = [];
for W = 1:Words
%For each word, convert it first to decimal:
P = (W*4)-3;
Word = BinStr(P:P+3);
DEC = bin2dec(Word);
HEX = HEXi{DEC+1};
HexStr = [HexStr HEX];
end
LHex = length(HexStr); %If the Hex String has an odd number of bits, add 0 to the front.
if isodd(LHex)
HexStr = ['0' HexStr];
end
out = HexStr;