2
votes

Let's say i have a 1x32 double array Input in matlab workspace. This variable has all positive decimal values. I want to convert each value to Hex & store it in another array Output
I use dec2hex(Input) & it generates a character string with Hex values. Now, i want an array of Hex numbers & not a string.
How do i convert this Hex string into Hex array of 1x32 Output
If i use str2num or str2double, it gives empty & NaN respectively?
How to do it

2
Hex numbers include strings (A,...,F) naturally, so the type would be string not `num'. Usually you can use 'dec2hex' and 'hex2num' functions together to convert to hex-string and convert back. And just use the hex-string for print and display stuff. - NKN
As i said, i want Hex array not Hex string because i want to send this array into Xilinx ISE - KharoBangdo
There is no "array of Hex numbers". The values of array elements are simply numbers. These numbers can be represented in decimal, hexadecimal, binary, or any number of other bases, but the underlying number is still the same. Also, hexadecimal representation of decimal values is very weird; normally the values would be integers. - beaker

2 Answers

5
votes

to get neither empty nor Nan values use `hex2dec'. something like this works for me:

a=1:20; 
b=dec2hex(a); 
c=hex2dec(b)

ans =

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
1
votes

Matlab does not manage hexadecimal numbers per se, only decimal notation. That's why matlab stores hexadecimal numbers in string format.

To do an addition of hex for example, you have to pass through the decimal notation:

a='ABC'; 
b='123'; 
c=dec2hex(hex2dec(a)+hex2dec(b))