From: Tom Bryan on
On 5/25/10 11:38 AM, Jordan wrote:
> I know there must be an easy way to do this, but I'm new to matlab and
> have not been able to figure it out and have tried searching for a
> solution without luck.
>
> I need to convert a signed integer to a 16-bit sign-extended (2's
> complement) hex number for output to a file.
>
> For example:
>
> 3 -> 0003
> -3 -> FFFD
>
> I've tried using dec2hex(), but it does not support negative number.
> I've also tried playing around with a fi object but could not quite
> figure out how to make that work as I needed it to.
>
> Any suggestions?
>
> Thanks!

The HEX reference page in the Fixed-Point Toolbox documentation has
examples of writing and reading hex data to and from files. It is
located in the documentation at Fixed-Point Toolbox > Functions > Radix
Conversion > hex

http://www.mathworks.com/access/helpdesk/help/toolbox/fixedpoint/ref/hex.html

Using fi objects, your example would be

a = fi([3; -3], 1, 16, 0);
hex(a)

ans =

0003
fffd

The inverse also works. You can take hex strings and assign them into a
fi object.

The reference page continues the example using fprintf and fgetl to
write and read hex data to and from files.

Best wishes,
Tom Bryan
tom.bryan(a)mathworks.com