Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting negative integers to HEX values

626 views
Skip to first unread message

Arun Chhabra

unread,
Sep 21, 1998, 3:00:00 AM9/21/98
to
*****
To respond to this message, remove "no-spam" from the reply-to address
*****

I was trying to convert decimal integer values to HEX format. As per the
"dec2hex" command it appears that this should not be a problem.

However, I am not able to convert a negative number to the expected HEX
value (or vice versa using the hex2dec command). As I understand, this
command is meant to work with "integer decimal" values (or convert to it, in
the case of dec2hex).

Specifically, as an example, consider the decimal value:

-5698. It's equivalent hex value should be FFFFE9C4, instead Matlab 4.0
gives:
-5.698000e+003.

OR

-10 should be FFFFFFF6, but instead the result output by Matlab is:
-1.000000e+001

I would appreciate any help that you are able to provide.

Regards,
Arun

Alexandre Kampouris

unread,
Sep 23, 1998, 3:00:00 AM9/23/98
to

Arun Chhabra wrote:


> However, I am not able to convert a negative number to the expected HEX value
> (or vice versa using the hex2dec command). As I understand, this command is
> meant to work with "integer decimal" values (or convert to it, in the case of
> dec2hex).

> Specifically, as an example, consider the decimal value:
>
> -5698. It's equivalent hex value should be FFFFE9C4, instead Matlab 4.0
> gives:
> -5.698000e+003.

On Matlab 5, this returns an error :

» dec2hex(-5,3)
??? Error using ==> dec2hex
D must be a non-negative integer.

For integers there are several ways to represent negative numbers.

I'd suppose you're looking for two's complement representation, which is the
most common:

function [s]=myhex(i,n);
if i<0,
s=dec2hex(16^n+i,n);
else
s=dec2hex(i,n);
end;

This can be improved to meet your particular needs... There is no protection or
error checking in this version. I leave it as an exercise to modify this for an
arbitrary word length, instead of a number of 4 bit nibbles. :-) And remember
that Matlab works with values in IEEE floating point representation, with a
mantissa of 53 bits or so. Don't try converting too wide a field.

The two's complement representation of a negative number will depend on the word
length, whereas a positive number will always be the same, if you ignore the
varying number of leading zeros.

Alexandre

Troy Richards

unread,
Oct 9, 1998, 3:00:00 AM10/9/98
to
This is in response to Jeremy Berkowitz post of 9/29/98

Jeremy,
Check out www.nr.com and look at on-line book in chapter 4.5 on Gaussian
Quadratues. I have converted gaulag and gauher from fortran to matlab if
you are interested.
--

Troy Richards
e-mail troy.r...@drea.dnd.ca
Phone (902)426-3100 Ext 285
FAX (902)426-9654
---------------------------------
Mail Delivery
DREA
PO BOX 1012
Dartmouth NS, B2Y 3Z7
---------------------------------
Courier Delivery
DREA
9 Grove St.
Dartmouth NS, B3A 3C5
---------------------------------

Troy Richards

unread,
Oct 9, 1998, 3:00:00 AM10/9/98
to
0 new messages