8

So, I have a string of 13 characters.

string str = "HELLOWORLDZZZ";

and I need to store this as ASCII representation (hex) in a uint variable. How do I do this?

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
Andy Hin
  • 30,345
  • 42
  • 99
  • 142

6 Answers6

12

You can use Encoding.ASCII.GetBytes to convert your string to a byte array with ASCII encoding (each character taking one byte). Then, call BitConverter.ToUInt32 to convert that byte array to a uint. However, as @R. Bemrose noted in the comments, a uint is only 4 bytes, so you'll need to do some partitioning of your array first.

bdukes
  • 152,002
  • 23
  • 148
  • 175
  • How exactly does a uint represent 4 byte string? How does "ABCD" get represented in a uint? – Andy Hin Nov 16 '10 at 21:19
  • 1
    @whydna, The [7-bit ASCII table](http://www.neurophys.wisc.edu/comp/docs/ascii/) is used to convert between bit representations and characters. So "ABCD" becomes `01000001 01000010 01000011 01000100` which in decimal is "1094861636", the value that the `uint` would contain. – jball Nov 16 '10 at 21:23
  • Awesome. So I just have 1 more question. So I convert my string into Ascii byte array, then I put it through the BitConvert.ToUint32, which returns the value "1145258561" for the string "ABCD". Converting that to HEX I get "44434241" which is awesome! Except one thing- why is it backwards? (41 = A, 42 = B, etc..). I'm sure it has something to do with big endian little endian – Andy Hin Nov 16 '10 at 21:38
  • 1
    @whydna Sorry, yeah, `BitConverter.ToUInt32` is little endian, so `1145258561` is the correct decimal value for an input of `"ABCD"`. You can reverse the array before calling `BitConverter.ToUInt32` and you will get the decimal value I gave above, which corresponds to `41424344` in hex. – jball Nov 16 '10 at 21:47
  • Great. Works great. Just for any future reference, it is depenent on the endian-ness of your computer system - so you need to check BitConvert.IsLittleEndian and reverse the array if neccessary. – Andy Hin Nov 16 '10 at 21:56
4

I think this is the method you want

Convert.ToUInt32(yourHexNumber, 16);

see the documentation here.

user467384
  • 1,137
  • 4
  • 22
  • 38
1
uint.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
Femaref
  • 60,705
  • 7
  • 138
  • 176
1

See my comment, but if you want to just convert an ASCII string to Hex, which is what I suspect:

public string HexIt(string yourString)
{
    string hex = "";
    foreach (char c in yourString)
    {
        int tmp = c;
        hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
    }
    return hex;
}
Ta01
  • 31,040
  • 13
  • 70
  • 99
  • 1
    += on a string in a loop is a very bad idea!!! This creates a ton of garbage. Use StringBuilder. – Bryan Nov 16 '10 at 21:30
  • wasn't meant to be efficient, I think most of us are still wondering what the original intent of the question is. – Ta01 Nov 16 '10 at 21:31
0

This will convert your string (with a Base 16 representation) to a uint.

uint val = Convert.ToUInt32(str, 16);
Adam Maras
  • 26,269
  • 6
  • 65
  • 91
0

Now I guess I understand what you want in a comment on bdukes answer.

If you want the hex code for each character in the string you can get it using LINQ.

var str = "ABCD";
var hex = str.Select(c => ((int)c).ToString("X"))
    .Aggregate(String.Empty, (x, y) => x + y);

hex will be a string 41424344

BrunoLM
  • 97,872
  • 84
  • 296
  • 452