Connecting Tech Pros Worldwide Forums | Help | Site Map

C# Int to Hex of the Int (Application)

BageDevimo's Avatar
Newbie
 
Join Date: Jul 2008
Location: New Zealand
Posts: 10
#1: Jul 17 '08
Hiya

Ok, so im writing a program that requires the hex of the int.. but not just this:
Expand|Select|Wrap|Line Numbers
  1.  int myInt = 24;
  2.  string myHex = myInt.ToString("X");
'cause that'll just return 0x18, which is just the same as me going:
Expand|Select|Wrap|Line Numbers
  1. int myInt = 0x18;
Im after the 4 (maybe 8?) bytes of the int, that are stored in memory. How can I get at these?

Thanks!

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Jul 17 '08

re: C# Int to Hex of the Int (Application)


So you want 0x0018 (int16) or 0x00000018(int32) as a string value?
If you don't want the string value but want a byte[] then consider this:
Expand|Select|Wrap|Line Numbers
  1. byte[] mybytes=BitConverter.GetBytes(myInt);
  2.  
BageDevimo's Avatar
Newbie
 
Join Date: Jul 2008
Location: New Zealand
Posts: 10
#3: Jul 17 '08

re: C# Int to Hex of the Int (Application)


Ok, so the Int32 one is just the equvilent of 00 00 00 18 or something, right?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Jul 17 '08

re: C# Int to Hex of the Int (Application)


Yeah
0x18 is the same number as 0x0018 or 0x0000000000000018 or whatever
BageDevimo's Avatar
Newbie
 
Join Date: Jul 2008
Location: New Zealand
Posts: 10
#5: Jul 17 '08

re: C# Int to Hex of the Int (Application)


Ok, cool..

I've got it working, thanks a lot!

I used BitConverter.ToInt32() to get it back to a int also!!

Ben Anderson,
Reply