tags:

views:

201

answers:

5

I'm trying to use an integer as the numerical representation of a string, for example, storing "ABCD" as 0x41424344. However, when it comes to output, I've got to convert the integer back into 4 ASCII characters. Right now, I'm using bit shifts and masking, as follows:

int value = 0x41424344;
string s = new string (
              new char [] { 
                 (char)(value >> 24), 
                 (char)(value >> 16 & 0xFF),
                 (char)(value >> 8 & 0xFF),
                 (char)(value & 0xFF) });

Is there a cleaner way to do this? I've tried various casts, but the compiler, as expected, complained about it.

A: 
public string ConvertToHex(string asciiString)
{
    string hex = "";
    foreach (char c in asciiString)
    {
        int tmp = c;
        hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
    }
    return hex;
} 

It will convert string to hex as you required.

VOX
I think the OP is looking for the opposite. Given a number, it should be converted back to the string.
Kobi
A: 
public static string ToHexString(string value)
{
    return value.Aggregate(new StringBuilder("0x"),
        (sb, c) => sb.AppendFormat("{0:x2}", (int)c)).ToString();
}
Daniel Renshaw
+1  A: 

Characters are 16 bit, so you have to encode them into eight bit values to pack them in an integer. You can use the Encoding class to convert between characters and bytes, and the BitConverter class to convert between bytes and integer

Here is conversion both ways:

string original = "ABCD";

int number = BitConverter.ToInt32(Encoding.ASCII.GetBytes(original), 0);

string decoded = Encoding.ASCII.GetString(BitConverter.GetBytes(number));

Note that the order of the bytes in the integer depends on the endianess of the computer. On a little endian system the numeric value of "ABCD" will be 0x44434241. To get the reverse order, you can reverse the byte array:

byte[] data = Encoding.ASCII.GetBytes(original);
Array.Reverse(data);
int number = BitConverter.ToInt32(data, 0);

byte[] data2 = BitConverter.GetBytes(number);
Array.Reverse(data2);
string decoded = Encoding.ASCII.GetString(data2);

Or if you are using framework 3.5:

int number =
  BitConverter.ToInt32(Encoding.ASCII.GetBytes(original).Reverse().ToArray() , 0);

string decoded =
  Encoding.ASCII.GetString(BitConverter.GetBytes(number).Reverse().ToArray());
Guffa
+2  A: 
int value = 0x41424344;
string s = Encoding.ASCII.GetString(
               BitConverter.GetBytes(value).Reverse().ToArray());

(The above assumes that you're running on a little-endian system. For big-endian you could just drop the .Reverse().ToArray() part, although if you are on a little-endian system then it would probably make more sense for you to just store "ABCD" as 0x44434241 in the first place, if possible.)

LukeH
This works wonderfully. Thanks.
bluewall21
A: 

Hi if the string is never longer than 8 chars and a kind of Hexstring, you could use the base variable 16 have a look at the Conversion functions from the Convert class.

string s = "ABCD";
uint i = Convert.ToUInt32( s, 16 );
MessageBox.Show( Convert.ToString( i, 16 ) );

regards Oops

Oops
This isn't about hex but about ascii, the string can well be `"Aa4@?"`.
Kobi
OK sorry I completely misunderstood the question
Oops