Accelerating the pace of engineering and science

# hex

Hexadecimal representation of stored integer of fi object

hex(a)

## Description

hex(a) returns the stored integer of fi object a in hexadecimal format as a string. hex(a) is equivalent to a.hex.

Fixed-point numbers can be represented as

$real\text{-}worldvalue={2}^{-fractionlength}×storedinteger$

or, equivalently as

$real\text{-}worldvalue=\left(slope×storedinteger\right)+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.

## Examples

Viewing fi Objects in Hexadecimal Format

The following code

```a = fi([-1 1],1,8,7);
y = hex(a)
z = a.hex```

returns

```y =

80   7f

z =

80   7f
```

Writing Hex Data to a File

The following example shows how to write hex data from the MATLAB® workspace into a text file.

First, define your data and create a writable text file called hexdata.txt:

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

h = fopen('hexdata.txt','w');
```

Use the fprintf function to write your data to the hexdata.txt file:

```for k=1:length(a)
fprintf(h,'%s\n',hex(a(k)));
end
fclose(h);
```

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

```type hexdata.txt
```

MATLAB returns:

```0000
1000
2000
3000
4000
5000
6000
7000
8000
9000
a000
b000
c000
d000
e000
f000
```

Reading Hex Data from a File

The following example shows how to read hex data from a text file back into the MATLAB workspace.

Open hexdata.txt for reading and read its contents into a workspace variable:

```h = fopen('hexdata.txt','r');

nextline = '';
str='';
while ischar(nextline)
nextline = fgetl(h);
if ischar(nextline)
str = [str;nextline];
end
end
```

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```