Results 1 to 4 of 4

Thread: [RESOLVED] Conversion UINT32->string

  1. #1

    Thread Starter
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Resolved [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)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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.

  3. #3

    Thread Starter
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Conversion UINT32->string

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. Dim vBytes() As Byte = BitConverter.GetBytes(sValue)
    2.         Array.Reverse(vBytes)
    3.         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)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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
  •  



Click Here to Expand Forum to Full Width