read and convert binary file

69 views (last 30 days)
Gaston Melo
Gaston Melo on 14 May 2023
Commented: Star Strider on 14 May 2023
Hi to all,
I have a file with this data:
this is a large file (I only show a portion of it). I want to convert this data (8 bit format) to a decimal number, so AA will be 170 and so on. I tried to use readcell but for some reason Im unable to convert to decimal. Tried also Fopen and Fread to read the file but only give decimal representation of easch char in the file ( output like this AA ----> 65 65).
I want the output be like " 170 8 0 0 ...."
Regards
GAston

Accepted Answer

Star Strider
Star Strider on 14 May 2023
It would help to have the file.
That aside, the textscan function is likely the easiest way to read it —
str = 'AA 08 00 00 00 00 02 40 3A 84';
Outc = textscan(str, '%2x', 'Delimiter',' ')
Outc = 1×1 cell array
{10×1 uint64}
Outn = cell2mat(Outc).'
Outn = 1×10
170 8 0 0 0 0 2 64 58 132
You have the file and we don’t, so you will also need to use fopen and fclose to open, read, and then close the file after reading it.
.
  7 Comments
Gaston Melo
Gaston Melo on 14 May 2023
Thanks to both of you. Thanks for share your vast knolowdge. Cheers!!
Star Strider
Star Strider on 14 May 2023
To use textscan, try something like this —
fidi = fopen('led.txt','rt');
datac = textscan(fidi, '%2c', 'Delimiter',' ', 'CollectOutput',1)
datac = 1×1 cell array
{2858×2 char}
fclose(fidi);
datav = cell2mat(datac);
D = hex2dec(datav(2:size(datav,1)-1,:));
Dbfr = buffer(D, 10);
% vv = factor(size(Dbfr,2))
for k = 1:13:size(Dbfr,2)
fprintf('Columns %4d - %4d —\n', [1 13]+(k-1))
Section = Dbfr(:, (1:13)+(k-1))
end
Columns 1 - 13 —
Section = 10×13
170 0 0 58 31 255 240 2 255 6 66 34 105 8 0 0 132 118 240 255 80 1 0 4 118 255 0 0 2 31 42 169 2 7 26 1 0 31 253 0 0 0 0 0 26 80 150 118 25 0 118 118 0 0 0 0 0 0 6 4 7 195 0 192 128 0 0 0 0 26 2 150 26 246 86 128 1 124 0 0 0 80 1 6 7 255 0 255 64 28 103 0 0 0 0 0 150 204 1 119 255 118 26 62 0 0 64 34 6 6 224 5 105 6 36 8 31 0 0 0 118 204 204 255 26 255 0 128 0 118
Columns 14 - 26 —
Section = 10×13
192 64 44 31 37 31 105 31 6 37 34 15 64 1 118 128 118 170 118 255 118 236 118 118 144 0 28 225 64 192 26 192 6 192 34 17 17 65 65 24 128 118 1 118 1 0 1 118 204 24 84 146 247 12 126 191 105 191 2 17 17 128 127 22 33 255 154 128 86 255 86 254 204 26 1 254 236 205 26 2 6 37 6 41 66 8 16 198 26 34 240 118 155 0 85 0 104 151 0 0 255 118 118 255 105 64 34 191 34 26 65 194 26 5 33 17 15 255 118 118 86 118 118 150 255 118 236 146 26 144
Columns 27 - 39 —
Section = 10×13
168 36 34 1 3 17 34 17 107 26 31 192 2 202 128 118 82 144 150 118 150 0 118 118 1 80 33 17 17 3 17 26 17 105 31 130 190 191 16 150 146 24 236 205 118 204 255 118 254 1 86 150 26 1 191 2 127 66 127 32 192 105 8 27 28 118 144 255 82 254 146 254 255 1 255 26 2 26 105 1 26 9 134 3 169 86 17 6 48 16 8 255 82 118 237 255 82 26 2 26 0 0 204 0 64 253 66 34 168 17 0 64 128 34 31 252 32 118 237 146 118 202 237 1 118 1 118 118 255 26
Columns 40 - 52 —
Section = 10×13
1 32 0 29 0 28 4 34 33 255 38 31 65 0 26 1 26 32 26 0 118 40 255 40 118 150 32 0 29 4 28 0 26 31 255 36 255 255 58 26 2 26 0 26 4 118 118 255 40 255 253 146 2 32 1 29 1 28 105 43 34 255 39 56 65 0 26 0 26 0 26 255 0 40 255 40 146 150 32 0 29 8 28 0 6 32 255 37 255 65 59 26 4 26 0 26 1 0 40 255 40 255 150 146 0 29 2 32 16 28 2 255 35 255 26 57 65 1 26 0 26 0 26 254 255 40 255 118 146 150
Columns 53 - 65 —
Section = 10×13
60 65 47 130 192 18 0 31 2 18 105 18 253 146 150 64 254 1 26 4 118 0 26 255 24 255 65 63 3 105 18 0 26 192 18 1 6 255 18 150 146 238 255 24 32 118 1 26 0 0 191 26 61 65 1 6 255 18 105 18 0 18 34 18 1 146 150 154 0 254 26 255 24 32 26 118 26 0 65 31 2 34 18 0 6 255 18 0 31 0 18 150 118 111 118 24 64 0 251 26 1 118 32 26 62 43 0 31 254 18 34 18 0 26 192 18 4 146 0 154 118 255 26 118 26 64 118 1 24 0
Columns 66 - 78 —
Section = 10×13
18 105 16 255 18 0 66 66 66 66 66 66 66 26 255 26 223 26 1 41 41 41 41 41 41 41 0 6 64 18 4 26 22 22 22 22 22 22 22 4 0 0 24 0 118 86 86 86 86 86 86 86 18 34 18 253 18 105 37 37 37 37 37 37 37 26 118 26 255 26 255 118 118 118 118 118 118 118 0 31 0 18 0 6 0 0 0 0 0 0 0 1 118 64 26 4 0 111 111 111 111 111 111 111 26 192 18 1 18 27 27 27 27 27 27 27 27 118 1 24 0 26 118 118 118 118 118 118 118 118
Columns 79 - 91 —
Section = 10×13
66 66 66 66 66 66 66 66 66 66 66 66 66 41 41 41 41 41 41 41 41 41 41 41 41 41 22 22 22 22 22 22 22 22 22 22 22 22 22 86 86 86 86 86 86 86 86 86 86 86 86 86 37 37 37 37 37 37 37 37 37 37 37 37 37 118 118 118 118 118 118 118 118 118 118 118 118 118 0 0 0 0 0 0 0 0 0 0 0 0 0 111 111 111 111 111 111 111 111 111 111 111 111 111 27 27 27 27 27 27 27 27 27 27 27 27 27 118 118 118 118 118 118 118 118 118 118 118 118 118
Columns 92 - 104 —
Section = 10×13
66 66 66 66 66 66 66 66 66 66 66 66 66 41 41 41 41 41 41 41 41 41 41 41 41 41 22 22 22 22 22 22 22 22 22 22 22 22 22 86 86 86 86 86 86 86 86 86 86 86 86 86 37 37 37 37 37 37 37 37 37 37 37 37 37 118 118 118 118 118 118 118 118 118 118 118 118 118 0 0 0 0 0 0 0 0 0 0 0 0 0 111 111 111 111 111 111 111 111 111 111 111 111 111 27 27 27 27 27 27 27 27 27 27 27 27 27 118 118 118 118 118 118 118 118 118 118 118 118 118
Columns 105 - 117 —
Section = 10×13
66 66 66 66 66 66 66 66 66 66 66 66 66 41 41 41 41 41 41 41 41 41 41 41 41 41 22 22 22 22 22 22 22 22 22 22 22 22 22 86 86 86 86 86 86 86 86 86 86 86 86 86 37 37 37 37 37 37 37 37 37 37 37 37 37 118 118 118 118 118 118 118 118 118 118 118 118 118 0 0 0 0 0 0 0 0 0 0 0 0 0 111 111 111 111 111 111 111 111 111 111 111 111 111 27 27 27 27 27 27 27 27 27 27 27 27 27 118 118 118 118 118 118 118 118 118 118 118 118 118
Columns 118 - 130 —
Section = 10×13
66 66 66 66 66 0 255 2 4 18 16 31 14 41 41 41 41 41 12 255 169 26 30 12 118 43 22 22 22 22 22 31 171 6 32 0 8 48 15 86 86 86 86 86 118 40 43 0 143 168 0 43 37 37 23 37 37 40 255 7 0 8 31 18 31 118 118 118 118 118 2 255 43 2 12 118 169 118 0 0 2 0 0 16 31 4 31 0 48 31 48 111 111 118 111 111 168 118 26 118 168 0 118 0 27 27 27 27 0 170 48 16 40 0 10 48 22 118 118 118 118 143 40 0 0 2 143 169 0 43
Columns 131 - 143 —
Section = 10×13
23 31 48 20 10 68 244 159 196 244 196 228 255 43 118 0 26 30 138 30 130 138 43 138 26 251 31 48 12 32 6 70 76 64 72 68 228 32 68 118 0 26 0 0 6 6 118 6 138 26 0 138 48 20 32 31 8 228 66 135 212 196 16 68 196 0 26 0 118 254 30 30 131 30 138 0 138 138 12 16 31 40 70 68 70 72 68 252 68 196 228 26 0 118 2 30 138 6 30 138 43 138 138 24 16 31 48 2 68 76 64 68 196 68 196 228 255 0 118 0 30 168 6 118 138 138 138 138 24 247
Columns 144 - 156 —
Section = 10×13
68 0 189 88 128 78 128 168 166 255 169 161 160 138 2 178 255 0 6 0 93 151 220 8 55 54 196 212 189 69 66 59 168 164 164 164 1 79 171 138 30 170 97 30 97 89 147 24 146 255 255 7 228 136 189 168 164 168 169 165 255 165 39 171 161 26 254 162 92 147 93 88 113 0 148 98 7 55 0 6 2 127 214 127 165 168 165 167 66 90 171 64 0 254 145 255 145 147 24 24 150 45 255 5 68 189 1 168 168 168 214 0 255 47 160 65 64 138 58 41 8 92 8 255 1 0 100 54 45 9
Columns 157 - 169 —
Section = 10×13
4 167 169 166 130 6 246 64 176 64 0 64 244 99 147 169 37 254 0 111 118 131 118 143 118 72 64 25 70 167 190 166 32 0 38 216 49 44 169 255 101 255 149 130 147 255 128 118 131 131 130 40 167 168 127 150 190 150 0 16 0 34 12 0 0 8 8 145 255 134 255 0 59 0 118 168 143 36 1 1 168 162 190 168 243 105 47 31 26 16 66 0 255 91 12 139 8 111 255 118 118 118 138 30 90 17 169 163 190 127 2 64 0 53 105 168 0 255 99 90 149 142 127 254 118 0 0 255 40 154
Columns 170 - 182 —
Section = 10×13
168 48 190 26 34 27 40 14 33 105 31 169 27 40 0 1 118 26 118 2 26 1 255 118 40 97 112 4 22 35 64 66 1 4 23 31 0 111 192 66 40 24 118 0 41 86 0 118 86 0 0 118 64 1 207 1 16 22 18 31 2 22 2 168 111 118 64 255 0 41 86 0 118 118 86 41 40 0 107 34 26 31 32 1 31 51 173 26 27 0 4 130 118 26 118 41 2 118 0 40 86 118 0 41 31 31 4 51 0 31 191 191 0 64 34 1 15 118 118 0 0 111 118 1 86 4 41 118 9 111
Columns 183 - 195 —
Section = 10×13
0 168 167 3 9 168 255 103 88 26 127 164 129 155 36 6 99 0 40 255 62 255 132 145 147 255 169 1 161 92 236 255 6 190 247 2 168 214 15 36 223 129 255 255 255 111 197 96 41 8 255 100 1 166 1 4 26 1 1 169 64 88 128 168 168 223 30 9 59 118 9 223 36 118 255 0 92 8 4 161 167 169 169 14 189 1 253 27 54 255 226 108 247 30 89 40 97 195 223 131 97 255 145 255 4 134 169 1 255 255 167 168 64 168 168 168 15 41 36 36 223 255 118 30 36 118 92 47 8 98
Columns 196 - 208 —
Section = 10×13
6 2 6 255 1 32 40 50 37 47 57 32 105 97 111 0 127 9 24 43 43 43 43 43 26 255 93 170 32 169 6 254 42 52 39 49 105 1 6 255 147 255 40 0 255 43 43 43 43 255 0 0 168 164 0 255 16 34 44 54 41 51 6 33 6 45 72 0 255 59 43 43 43 43 43 0 40 254 170 2 6 164 31 36 46 56 43 53 31 255 0 147 108 0 72 118 43 43 43 43 43 118 255 143 34 84 168 247 51 38 48 35 45 55 51 16 0 86 255 40 108 0 43 43 43 43 43 0 41 137
Columns 209 - 221 —
Section = 10×13
68 1 65 132 70 245 1 255 168 0 169 2 6 168 86 43 196 30 100 0 255 40 212 47 9 0 0 68 65 68 196 26 134 170 255 11 164 169 0 143 0 146 168 194 118 254 40 255 111 169 138 119 0 1 125 70 65 31 105 255 171 169 196 166 0 13 86 82 138 10 118 255 255 15 39 136 146 111 70 70 13 169 65 51 6 171 4 169 169 64 31 168 0 99 168 146 0 0 40 237 40 40 118 118 6 34 68 2 125 32 169 255 0 255 255 249 40 2 118 138 9 82 26 40 255 190 255 255 130 2
Columns 222 - 234 —
Section = 10×13
189 40 103 167 6 31 31 26 111 0 0 0 0 178 2 62 30 0 118 86 118 0 0 0 0 0 26 30 31 103 31 40 34 64 254 254 254 254 0 197 197 118 62 118 2 118 0 255 255 255 255 0 169 167 40 64 40 26 192 67 24 26 28 30 0 89 6 2 118 2 168 185 131 138 138 138 138 0 103 3 28 24 24 6 41 22 0 0 0 0 0 62 236 6 132 168 0 40 0 0 0 0 0 1 31 161 3 190 6 6 104 0 57 57 0 0 0 118 146 236 139 0 0 0 0 132 132 0 0 0
Columns 235 - 247 —
Section = 10×13
0 34 0 34 0 34 0 65 0 90 0 115 0 137 130 0 130 0 130 0 129 0 129 0 129 0 34 0 34 0 34 0 55 0 80 0 105 0 130 130 0 130 0 130 0 129 0 129 0 129 0 129 0 34 0 34 0 45 0 70 0 95 0 120 0 0 130 0 130 0 129 0 129 0 129 0 129 0 34 0 34 0 34 0 60 0 85 0 110 0 135 130 0 130 0 130 0 129 0 129 0 129 0 129 0 34 0 34 0 50 0 75 0 100 0 125 0 0 130 0 130 0 129 0 129 0 129 0 129 0
Columns 248 - 260 —
Section = 10×13
140 0 160 0 185 0 39 0 39 0 39 0 39 129 0 129 0 129 0 130 0 130 0 130 0 130 0 150 0 175 0 39 0 205 0 39 0 39 0 0 129 0 129 0 130 0 129 0 130 0 130 0 145 0 165 0 190 0 195 0 39 0 39 0 39 129 0 129 0 129 0 129 0 130 0 130 0 130 0 155 0 180 0 39 0 210 0 215 0 39 0 0 129 0 129 0 130 0 129 0 129 0 130 0 39 0 170 0 39 0 200 0 39 0 39 0 39 130 0 129 0 130 0 129 0 130 0 130 0 130
Columns 261 - 273 —
Section = 10×13
0 39 0 220 0 39 0 39 0 39 0 39 0 0 130 0 129 0 130 0 130 0 130 0 130 0 39 0 39 0 39 0 39 0 39 0 39 0 39 130 0 130 0 130 0 130 0 130 0 130 0 130 0 39 0 225 0 39 0 39 0 230 0 39 0 0 130 0 129 0 130 0 130 0 129 0 130 0 39 0 39 0 39 0 39 0 39 0 39 0 240 130 0 130 0 130 0 130 0 130 0 130 0 129 0 39 0 39 0 39 0 39 0 235 0 39 0 0 130 0 130 0 130 0 130 0 129 0 130 0
Columns 274 - 286 —
Section = 10×13
245 0 39 0 255 0 39 0 39 0 39 0 39 129 0 130 0 129 0 130 0 130 0 130 0 130 0 39 0 39 0 14 0 39 0 24 0 39 0 0 130 0 130 0 130 0 130 0 130 0 130 0 39 0 39 0 4 0 39 0 39 0 39 0 0 130 0 130 0 130 0 130 0 130 0 130 0 0 0 39 0 250 0 19 0 39 0 39 0 39 0 0 130 0 129 0 130 0 130 0 130 0 130 0 39 0 39 0 9 0 39 0 39 0 39 0 0 130 0 130 0 130 0 130 0 130 0 130 0 0
Any newline charracters in the original file are lost here, if that is important.
.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!