Main Content

hex

Hexadecimal representation of stored integer of fi object

Description

b = hex(a) returns the stored integer of fi object a in hexadecimal format as a character vector.

Fixed-point numbers can be represented as

real-world value=2fraction length×stored integer

or, equivalently as

real-world value=(slope×stored integer)+bias

The stored integer is the raw binary number, in which the binary point is assumed to be at the far right of the word.

Tip

hex returns the hexadecimal representation of the stored integer of a fi object. To obtain the hexadecimal representation of the real-world value of a fi object, use dec2hex.

example

Examples

collapse all

Create a signed fi object with values -1 and 1, a word length of 8 bits, and a fraction length of 7 bits.

a = fi([-1 1], 1, 8, 7)
a = 
   -1.0000    0.9922

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 8
        FractionLength: 7

Find the hexadecimal representation of the stored integers of fi object a.

b = hex(a)
b = 
'80   7f'

This example shows how to write hexadecimal data from the MATLAB® workspace into a text file.

Define your data.

x = (0:15)/16;
a = fi(x, 0, 16, 16);

Extract each hexadecimal value as a separate string using the extract function.

data = string(a.hex);
data = extract(data,alphanumericsPattern);

Use the writematrix function to write your data to the text file hexdata.txt.

writematrix(data',"hexdata.txt",Delimiter="comma",QuoteString=false);

To see the contents of the file you created, use the type function.

type hexdata.txt
0000,1000,2000,3000,4000,5000,6000,7000,8000,9000,a000,b000,c000,d000,e000,f000

This example shows how to read hexadecimal data from a text file back into the MATLAB® workspace.

Define your data.

x = (0:15)/16;
a = fi(x, 0, 16, 16);

Write your data to a text file named hexdata.txt. For more details about writing hexadecimal data to a file, see Write Hex Data to a File.

data = string(a.hex);
data = extract(data,alphanumericsPattern);
writematrix(data',"hexdata.txt",Delimiter="comma",QuoteString=false);

Read the contents of hexdata.txt into an array of strings.

str = readmatrix("hexdata.txt",OutputType="string");

Create a fi object with the correct scaling and assign it the hex values stored in the str variable.

b = fi([], 0, 16, 16);
b.hex = str
b = 
         0    0.0625    0.1250    0.1875    0.2500    0.3125    0.3750    0.4375    0.5000    0.5625    0.6250    0.6875    0.7500    0.8125    0.8750    0.9375

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Unsigned
            WordLength: 16
        FractionLength: 16

Input Arguments

collapse all

Input array, specified as a fi object.

Data Types: fi

Version History

Introduced before R2006a