Convert unsigned char to C in MatLab

I am trying to convert unsigned char in C to matlab code, unsigned char vector is filled with hex values. Below C code:

int main()
{
  unsigned char value = 0xaa;
  signed char temp;
  // cast to signed value
  temp = (signed char) value;
  // if MSB is 1, then this will signed extend and fill the temp variable with 1's
  temp = temp >> 7;
  // AND with the reduction variable
  temp = temp & 0x1b;
  // finally shift and reduce the value
  printf("%u",((value << 1)^temp));
}

      

The Matlab function I am creating to do the same:

value = '0xaa';
temp = int8(value);
temp2 = int8(value);
temp = bitsra(temp,7);
temp = and(temp,'0x1b');
galois_value =   xor(bitsll(temp2,1),temp);
disp(galois_value);

      

The values ​​printed in each code are different, does anyone know what's going on?

+3


source to share


1 answer


You created a line:

value = '0xaa';

      

Of 4 characters ['0' 'x' 'a' 'a']

.



In MATLAB, you usually don't handle variable bits, but if you want, try:

temp = int8(hex2dec('aa'));

      

+2


source







All Articles