9

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?

15
  • 2
    NOTE: This original string is not hex
    – Andy Hin
    Commented Nov 16, 2010 at 21:06
  • What do you mean is not hex... ascii representation IS a string btw.. Commented Nov 16, 2010 at 21:06
  • See my edit for the new string
    – Andy Hin
    Commented Nov 16, 2010 at 21:07
  • 1
    I do... I am not understanding what you mean by "convert to uint" do you mean the sum of the ascii values of all the characters? Commented Nov 16, 2010 at 21:08
  • 2
    A uint is only 4-bytes long. An ASCII representation is 1 byte long. How exactly do you expect to store 13 characters in a 4-byte field?
    – Powerlord
    Commented Nov 16, 2010 at 21:10

6 Answers 6

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.

5
  • How exactly does a uint represent 4 byte string? How does "ABCD" get represented in a uint?
    – Andy Hin
    Commented Nov 16, 2010 at 21:19
  • 1
    @whydna, The 7-bit ASCII table 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
    Commented Nov 16, 2010 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
    Commented Nov 16, 2010 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
    Commented Nov 16, 2010 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
    Commented Nov 16, 2010 at 21:56
5

I think this is the method you want

Convert.ToUInt32(yourHexNumber, 16);

see the documentation here.

1
uint.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
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;
}
2
  • 1
    += on a string in a loop is a very bad idea!!! This creates a ton of garbage. Use StringBuilder.
    – Bryan
    Commented Nov 16, 2010 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
    Commented Nov 16, 2010 at 21:31
0

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

uint val = Convert.ToUInt32(str, 16);
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.