Returning a Hex value from an integer

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Returning a Hex value from an integer

Jump to solution
1,548 Views
DavidN_
Contributor I

I am using the 16 bit 9S12C128 MCU.  I wrote this simple code to return a hex value from an integer input.

 

 

word dec_to_hex(signed short number)
{
  word hex_value;
  hex_value = number;

 

  return hex_value;  
}

 

 

What I would like to know is, does this return a 16 bit hex value (i.e. input = 32767, returns 0x7FFF; input = -32768, returns 0x8000)?  I can't really check the output because when I use "%d", I get an integer value; and when I use a "%x", I get the hex value.

 

The reason for this is so that I can output the hex value to a DAC.  Also, the data type "word" in a 16 bit MCU means that one word is 16 bits and the data type "dword" means 32 bits?

 

I have googled this but I could never get a clear answer.  Any help is appreciated.  Thanks in advance.

Labels (1)
Tags (1)
0 Kudos
1 Solution
474 Views
Lundin
Senior Contributor IV
It isn't clear whether "word" is defined as signed or unsigned integer. It is not an ISO C type, it is a non-standard type, so the answer can only be found in your compiler docs. What compiler are you using?


My advise would generally be to avoid all untrusted integer types, including plain "int", "short" etc, and thereby avoid all signedness and porting issues. Use something like this instead:

typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned long uint32;
typedef signed char sint8;
typedef signed short sint16;
typedef signed long sint32;

View solution in original post

0 Kudos
1 Reply
475 Views
Lundin
Senior Contributor IV
It isn't clear whether "word" is defined as signed or unsigned integer. It is not an ISO C type, it is a non-standard type, so the answer can only be found in your compiler docs. What compiler are you using?


My advise would generally be to avoid all untrusted integer types, including plain "int", "short" etc, and thereby avoid all signedness and porting issues. Use something like this instead:

typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned long uint32;
typedef signed char sint8;
typedef signed short sint16;
typedef signed long sint32;

0 Kudos