-
Sep 8th, 2011, 04:14 PM
#1
Thread Starter
Fanatic Member
[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
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Sep 8th, 2011, 07:02 PM
#2
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.
-
Sep 8th, 2011, 07:54 PM
#3
Thread Starter
Fanatic Member
Re: Conversion UINT32->string
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
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
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Sep 8th, 2011, 08:02 PM
#4
Re: [RESOLVED] Conversion UINT32->string
Hmmm, didn't think of using Encoding. Even easier than I thought.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|