Converting decimals to hexadecimals

1. Junior Member
Join Date
Dec 1969
Posts
14

Converting decimals to hexadecimals

I know it&#039;s a silly question (at least it seems to be), but I wasn&#039;t able to find anywhere an answer to it. How do I make this conversion?

2. Senior Member
Join Date
Dec 1969
Posts
10,852

RE: Converting decimals to hexadecimals

[code language="C#"]<BR>String.Format("{0:x}", yourDecimalValue);<BR>[/code]<BR><BR>Should do it for you.

3. Junior Member
Join Date
Dec 1969
Posts
14

RE: Converting decimals to hexadecimals

Thanks a lot, that worked well. But now I&#039;ve got another problem. I can&#039;t seem to convert back from hexa to dec... I found another way that converted from dec to hexa, [code language="C#"]<BR>Convert.ToString(myDecimalValue, 16);<BR>[/code], but it doesn&#039;t work backwards. It doesn&#039;t identify F, for example, as a number and so, F is 70 and f is 102.<BR><BR>How am I supposed to convert it back?<BR>

4. Junior Member
Join Date
Dec 1969
Posts
14

RE: Converting decimals to hexadecimals

Ok, I figured out a way. For those who need to do the same, this is what I did:<BR>[code language="C#"]<BR>Uri.FromHex(myHexValue); //only converts char<BR>[/code]<BR>And so, when I needed to convert a number that was larger than 1 char, I converted char by char and multiplied by 16... Like this:<BR>FF<BR>(15 * 16) + 15<BR><BR>It&#039;s really a manual way to do it, but it&#039;s the only one I found. If anyone knows a better one, let me know.<BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

Ugh...do it the easy way...

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemint32classparsetopic3.asp<BR><BR>String s = "F73C";<BR>int num = Int32.Parse( s, AllowHexSpecifier );<BR><BR>(Or use 0x0200 in place of the named AllowHexSpecifier.)<BR><BR>The docs for AllowHexSpecifier say that the string can *NOT* have the 0x prefix, so you&#039;d need to strip it if it is there.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•