Returning a Hex value from an integer

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Returning a Hex value from an integer

跳至解决方案
2,229 次查看
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.

标签 (1)
标记 (1)
0 项奖励
回复
1 解答
1,155 次查看
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 项奖励
回复
1 回复
1,156 次查看
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 项奖励
回复