2

What I have

I've a char array, defined by doing an strcat between two char arrays like this:

coeficientes = strcat(coef_bin, coef_bin_comp)

Then, I want to read each row and convert them to hex. I'm doing it like:

for k=1:11

  coef_binario = coeficientes(1+(k-1)*8:k*8);

  coef_hexadecimal = binaryVectorToHex( coef_binario - '0' );

  fprintf('%s\t%s\n', coef_binario, coef_hexadecimal);

end

What's the result

If I print both coeficientes variable and each coef_binario I take to convert, results are the following:

Note: coeficientes is printed by just removing semicolon after strcat line, but coef_binario is printed by using fprintf as is written above.

coeficientes =

00011111 00111101 01001100 01011011 01111001 10001000 10010111
10110101 11000100 11010011 11110001

00000111     07      11100111     E7      00011101     1D 
00100100     24      11101101     ED      10111111     BF     
11000001     C1      11000111     C7      00100100     24     
10010110     96      11011011     DB

The Problem

As you can see, I've tried to read the coeficientes var row by row using:

coef_binario = coeficientes(1+(k-1)*8:k*8);

But when it's printed, binary code doesn't match with the original one. Any idea about why or how can I do it in order to have it correctly?

Update 1

If I try to split it before doing it, in order to use strsplit before I need to get the string. I've tried:

strs = strsplit(sprintf('%s', coeficientes), ' ')

From this, I obtain again the wrong chain. In fact, after using just sprintf (and not strsplit), what I get is:

0000011111100111000111010010010011101101101111111100000111000111001001001001011011011011

Update 2

How coeficientes is generated:

% coef_k is a vector of decimal numbers, i.e.: [1 3 5 8 11 14]
coef_bin = dec2bin(coef_k);
coef_complementario = 16 - coef_k;

coef_bin_comp = dec2bin(coef_complementario);

coeficientes = strcat(coef_bin, coef_bin_comp)
2
  • It is not clear what coeficientes is. Is it a string? A cell array of strings? Can you add an actual Matlab statement that would produce coeficientes that we can use. Commented May 21, 2016 at 16:06
  • Added @nirvana-msu. Hope it helps. Commented May 21, 2016 at 16:11

2 Answers 2

3

The immediate issue is that MATLAB stores data in column-major order so essentially what is happening is that if your data is a 2D character array (the output of dec2bin):

c = ['00011111'
     '00111101'
     '01001100'
     '01011011'
     '01111001'
     '10001000'
     '10010111'
     '10110101'
     '11000100'
     '11010011'
     '11110001'];

Then when you use a linear index like 1+(k-1)*8:k*8, it reads down the columns rather than across the rows like you want.

For example for k = 1:

k = 1;
c(1+(k-1)*8:k*8)
% 00000111  <---- Clearly not the first row. It is the first 8 entries going
%                 down the first column.

You have four options:

  1. Transpose c prior to your processing (to make the numbers go down the columns instead):

    c = c.';
    
    k = 1;
    c(1+(k-1)*8:k*8)
    % 00011111 <---- The first row like you would expect!
    
  2. Adjust your index expression to go across the columns instead

    k = 1;
    c(k:size(c,1):end)
    % 00011111 <---- The first row like you would expect!
    
  3. Just use normal matrix indexing to grab a row

    k = 1;
    c(k,:)
    % 00011111 <---- The first row like you would expect!
    
  4. Just use the built-in bin2dec followed by dec2hex to get the hexadecimal representation. If you pass a 2D character array to bin2dec it interprets each row as a different binary number.

    hex = dec2hex(bin2dec(c));
    
        1F
        3D
        4C
        5B
        79
        88
        97
        B5
        C4
        D3
        F1
    
Sign up to request clarification or add additional context in comments.

Comments

1

Use strsplit to split your input string using space as a delimiter (available in R2013a+, otherwise use e.g. regexp). You can also replace for loop with cellfun to make it more compact:

coeff = '00011111 00111101 01001100 01011011 01111001 10001000 10010111 10110101 11000100 11010011 11110001';

strs = strsplit(coeff, ' ');
hex = cellfun(@(str) binaryVectorToHex(str-'0'), strs, 'uni', false);

>> hex
hex = 
    '1F'    '3D'    '4C'    '5B'    '79'    '88'    '97'    'B5'    'C4'    'D3'    'F1'

UPDATE

With your input (char matrix) you don't even need strsplit. Just use a combination of bin2dec and dec2hex as proposed by @Suever. You may also want to wrap resulting hex values into individual cells using cellstr.

cellstr(dec2hex(bin2dec(coeficientes)))

2 Comments

I've tried, and to not get an error (since for me, coeff is what strcat returns), I had to use sprintf to have the string in order to use it into strsplit. Then, I've found that after using sprintf, I obtain again the "wrong" chain, just as is shown in my example above =S. I'll update the post with it better explained.
The problem was like Suever pointed, but thanks for the cellfun idea, quite nice! Upvote!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.