[RESOLVED] Conversion UINT32->string
I'm looking for some code to convert a UINT32 to a 4 character string (least significant byte last). The 4 bytes in the UINT32 are AscII coded.
Example: &h2e4e6574 -> ".Net"
I already made a working example using GCHandle and Marshal'ing to copy the bytes from the UINT32 to a byte array, but the code isn't all that pretty and the resulting byte array will need to be reversed before it's AscII encoded to string.
Posting here to get some inspiration for a better or simpler way of doing this.
Thank you in advance
Thomas
Re: Conversion UINT32->string
Use the BitConverter class to convert the UInteger to a Byte array, then use Convert.ToChar to convert each Byte. You can use Array.ConvertAll to convert the Byte array to a Char array, which you can then pass to a String constructor.
Re: Conversion UINT32->string
Quote:
Originally Posted by
jmcilhinney
Use the BitConverter class to convert the UInteger to a Byte array, then use Convert.ToChar to convert each Byte. You can use Array.ConvertAll to convert the Byte array to a Char array, which you can then pass to a String constructor.
The BitConverter class was just what I needed :thumb:
These 3 lines did the job:
vb Code:
Dim vBytes() As Byte = BitConverter.GetBytes(sValue)
Array.Reverse(vBytes)
Return Encoding.ASCII.GetString(vBytes)
#EDIT: Don't understand why, but I cannot +rep you. Says I have to spread some rep first, but it would seem, that you're one of a narrow group helping me every time :)
Re: [RESOLVED] Conversion UINT32->string
Hmmm, didn't think of using Encoding. Even easier than I thought. :thumb: