How to convert Hex data into decimal data?

63 visualizaciones (últimos 30 días)
Shree
Shree el 28 de Mzo. de 2015
Editada: yahia mohammad el 2 de Sept. de 2021
Hi,
I have to convert the hex data I have into one decimal array. I am not able to use this data as input for following reasons.
1. MATLAB is unable to import this data file(data.m)
2. While defining the data in matrix or cell form is giving an error at each element which contains number and string(Example: 0A B5)
Example:(data.m) B5 62 0A 04 00 00 0E 34 B5 62 0D 01 10 00 A0 9A D9 13 00 00 00 00 CB F3 FF
Code:
t=25;
for n=1:1:t
count_hex = count_hex + 1;
A = data(1,n);
B(:,count_hex) = hex2dec(A);
end
What can I do to make it work?

Respuesta aceptada

Jos (10584)
Jos (10584) el 28 de Mzo. de 2015
Hexadecimal values are stored in strings in MatLab.
HexValue = 'FE'
hex2dec = hex2dec(HexValue)
DataHex = {'B5','C0','FF','ABCD'} % using a cell array of strings to store multiple values
DataDec = hex2dec(DataHex) % hex2dec can operate on such a cell array. No need to loop.
If your data file is an ascii file with Hex values separated by blank spaces, you can easily read it in using TEXTREAD:
DataHex = textread('myfile.txt','%s')

Más respuestas (2)

Jan
Jan el 28 de Mzo. de 2015
sscanf('%x') is much faster than hex2dec.
  2 comentarios
Jos (10584)
Jos (10584) el 29 de Mzo. de 2015
+1
... but you'll need a loop around the cell array, or a call to CELLFUN. In my opinion this make the code a little less clear.
yahia mohammad
yahia mohammad el 2 de Sept. de 2021
Editada: yahia mohammad el 2 de Sept. de 2021
HOW TO CONVERT '49204C6F76' TO DECIMAL BY USING MATLAB

Iniciar sesión para comentar.


Vivek Deshmukh
Vivek Deshmukh el 28 de Mzo. de 2015
USE COMMAND HEX2DEC

Categorías

Más información sobre Search Path en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by