tags:

views:

5049

answers:

6

How can I convert the following?

2934 (integer) to B76 (hex)

Let me explain what I am trying to do. I have User IDs in my database that are stored as integers. Rather than having users reference their IDs I want to let them use the hex value. The main reason is because it's shorter.

So not only do I need to go from integer to hex but I also need to go from hex to integer.

Is there an easy way to do this in C#?

+9  A: 
int myInt = 2934;
string myHex = myInt.ToString("X");  // gives you hex
int myNewInt = Convert.ToInt32(myHex, 16);  // back to int again.

see here for more info & examples - http://msdn.microsoft.com/en-us/library/bb311038.aspx.

Scott Ivey
Beat me by 8 seconds.
Joel Coehoorn
+4  A: 
string HexFromID(int ID)
{
    return ID.ToString("X");
}

int IDFromHex(string HexID)
{
    return int.Parse(HexID, System.Globalization.NumberStyles.HexNumber);
}

I really question the value of this, though. You're stated goal is to make the value shorter, which it will, but that isn't a goal in itself. You really mean either make it easier to remember or easier to type.

If you mean easier to remember, then you're taking a step backwards. We know it's still the same size, just encoded differently. But your users won't know that the letters are restricted to 'A-F', and so the ID will occupy the same conceptual space for them as if the letter 'A-Z' were allowed. So instead of being like memorizing a telephone number, it's more like memorizing a GUID (of equivalent length).

If you mean typing, instead of being able to use the keypad the user now must use the main part of the keyboard. It's likely to be more difficult to type, because it won't be a word their fingers recognize.

A much better option is to actually let them pick a real username.

Joel Coehoorn
The goal really is to take up fewer characters. Take twitter for example where they only allow 140 character messages. We're doing something similar so we're trying to give our users a way of shortening their user ID.
codette
In that case, you should think about a binary representation. This is likely a 32bit int that just doesn't use the negative portion, meaning 16bits of resolution. You can put that in one unicode character pretty easily.
Joel Coehoorn
+3  A: 

Try the following to convert it to hex

public static string ToHex(this int value) {
  return String.Format("0x{0:X}", value);
}

And back again

public static int FromHex(string value) {
  if ( value.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) {
    value = value.SubString(2);
  }
  return Int32.Parse(value, NumberStyles.HexNumber);
}
JaredPar
Not terribly sure why I deserved a -1 but I guess I offended someone.
JaredPar
Maybe they had a problem with how you spelt format? O_o
Gavin Miller
or the "0x" bit, which is something the OP didn't really want
Philippe Leybaert
i corrected the format spelling - but didn't downvote. downvotes with no explanation make me grumpy too...
Scott Ivey
@Philippe, perhaps but it seems like a very silly thing to downvote for. Especially considering half the answers didn't originally have the hex -> int part
JaredPar
Well, it didn't really answer the OP's question. In fact, the code you presented didn't work for the OP's problem. I rarely downvote, but if I do, I always make a comment and check back to see if the answer was edited, and in many cases I remove the downvote again. No offense.
Philippe Leybaert
@Philippe neither did the other answers which had only half solutions.
JaredPar
(I'm already sorry I commented). The question was "how do I convert 2934 to B76". Other answers did indeed only provide half of the solution, but your's converted "2934" to "0xB76". It's not a bad solution at all, but it's not an answer to the question that was asked.
Philippe Leybaert
+15  A: 
// Store integer 182
int decValue = 182;
// Convert integer 182 as a hex in a string variable
string hexValue = decValue.ToString("X");
// Convert the hex string back to the number
int decAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

from http://www.geekpedia.com/KB8_How-do-I-convert-from-decimal-to-hex-and-hex-to-decimal.html

Gavin Miller
you can also specify the number of digits by using: decValue.ToString("X4")
Martin
A: 

To Hex:

string hex = intValue.ToString("X");

To int:

int intValue = int.Parse(hex, System.Globalization.NumberStyles.HexNumber)
Brandon
A: 

A fashionably belated response, but have you considered some sort of Integer shortening implementation? If the only goal is to make the user ID as short as possible, I'd be interested to know if there is any other apparent reason why you specifically require hexadecimal conversion - unless I missed it of course. Is it clear and known (if so required) that user IDs are in actual fact a hexadecimal representation of the real value?

Wynand