Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Converting negative integers to HEX values

Subject: Converting negative integers to HEX values

From: Arun Chhabra

Date: 21 Sep, 1998 16:46:10

Message: 1 of 5

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

Subject: Converting negative integers to HEX values

From: ksettakr@syr.edu (Kraikron Settakraikun)

Date: 11 Sep, 1998 02:21:34

Message: 2 of 5

Try this , it is 2's complement in decimal format before conversion.

NDec=-10;
NHex=dec2hex(bitcmp(abs(NDec)-1,32))

Result:
NHex =

FFFFFFF6

Or

NDec=-5698;
NHex=dec2hex(bitcmp(abs(NDec)-1,32))


Result:
NHex =

FFFFE9BE


Your second result is wrong conversion.

KEng


On Mon, 21 Sep 1998 16:46:10 -0700, "Arun Chhabra" <achhabra@ti.com>
wrote:

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

Subject: Converting negative integers to HEX values

From: Alexandre Kampouris

Date: 23 Sep, 1998 08:47:30

Message: 3 of 5



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

Subject: improper numerical integration

From: Troy Richards

Date: 9 Oct, 1998 09:40:24

Message: 4 of 5

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.richards@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
---------------------------------

Subject: improper numerical integration

From: Troy Richards

Date: 9 Oct, 1998 09:38:32

Message: 5 of 5

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.richards@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
---------------------------------

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us