R2012b convert HEX to binary and extract individual bits

I have a data set that is output in HEX. I know how to convert the HEX to binary, even though in the R2012b release it's kinda convoluted.
>> DecimalNumber = hex2dec(X) %Where my HEX number is 4 digits in length 1-F in value for each digit
>> BinaryNumber = dec2bin(DecimalNumber,16) %giving me all 16 binary digits even when there are leading zero's
What I want to do is then break this into bit segments where I would get:
bits 1-5
bit 6
bits 7-11
bits 12-16
This will allow me to take my status word and see what the data is doing and what kinds of errors, if any, it are being reported out. I haven't been able to figure out a way to do this. Does anyone have any suggestions?

 Respuesta aceptada

Is this all you are trying to do?
bits01_05 = bin2dec(BinaryNumber(1:5));
bits06_06 = bin2dec(BinaryNumber(6:6));
bits07_11 = bin2dec(BinaryNumber(7:11));
bits12_16 = bin2dec(BinaryNumber(12:16));

3 comentarios

So I now realize where my kids get the not giving enough information the first time from. I always thought I was better than this.
I am uploading a sample source file and a copy of my code so far. The source file is a comma-delimited .txt file. I am wanting to import columns B, D, & E (Time, MSGInfo1, and CmdWrd1 respectively).
The Time column, once I get the format sorted out, is fine, but for the MsgInfo and CmdWrd columns, I need to 1) get the Hex value alone dropping the 0x"space" at the beginning of the message, 2) convert the Hex to binary and 3) break the bits out into smaller messages.
James's idea should work, but I'm worried it won't carry through all of the N cells of data.
I hope this gives a better idea/feel for what I'm trying to do. Let me know if you need more info.
joshmartinmont
joshmartinmont el 15 de Dic. de 2015
Editada: joshmartinmont el 15 de Dic. de 2015
OK. I've managed to import the file and have it give me three variables.
>>dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaerLines', startRow-1, 'ReturnOnError', false);
>>dataArray{1} = datenum(dataArray{1}, 'HH:MM"SS.FFF');
>>Timestamp = dataArray{:, 1};
>>MsgInfo = dec2bin(hex2dec(cellfun(@(x)(x(4:end),dataArray{:, 2}, 'UniformOutput', false)),16);
>>CmndWrd1 = dec2bin(hex2dec(cellfun(@(x)(x(4:end),dataArray{:, 2}, 'UniformOutput', false)),16);
It turns out what I want to do with looking at the bits may be harder than I thought. I need to convert bits 1-5 of CmndWrd1 to decimal, bits 7-11 of CmndWrd1 to decimal. This is easy enough to do, but the hard part is that I want to now make those two numbers into a single string and add in some conditional statements to allow for bit 6 of the CmndWrd1 changing and for bit 7 of MsgInfo to change around.
>>str = {[num2str(bin2dec(CmndWrd1(:,1:5))),'.',num2str(bin2dec(CmndWrd1(:,7:11))),'.',[if CmndWrd1(:,6) == 0, 'R', else, 'T', end],' ',[if MsgInfo(:,7) == 0, 'busB', else, 'busA', end]]}
This would give me a string that would look like: '25.8.R busB'.
I tried using this exact function but get the error "Expression or statement is incomplete or incorrect". Which I figured it would but, but I'm not sure how to make this work.
James Tursa
James Tursa el 15 de Dic. de 2015
Editada: James Tursa el 15 de Dic. de 2015
Try this:
str = {[num2str(bin2dec(CmndWrd1(:,1:5))),'.',num2str(bin2dec(CmndWrd1(:,7:11))),'.',char('R'+2*(CmndWrd1(:,6)-'0')),' bus',char('B'-(MsgInfo(:,7)-'0'))]}

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 15 de Dic. de 2015

Editada:

el 15 de Dic. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by