Returning a Hex value from an integer

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Returning a Hex value from an integer

ソリューションへジャンプ
2,241件の閲覧回数
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,167件の閲覧回数
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,168件の閲覧回数
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 件の賞賛
返信