C# convert string to uint

Andy Hin picture Andy Hin · Nov 16, 2010 · Viewed 37.2k times · Source

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?

Answer

bdukes picture bdukes · Nov 16, 2010

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.