Results 1 to 6 of 6

Thread: [2005] Convert a number to a byte array

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    [2005] Convert a number to a byte array

    I noticed that packets are sent in an array of bytes.

    For example:

    { 0xF7, 0x13, 0xFE }

    How can I convert a number such as 396 to a byte array such as this?

    Thanks

    Code examples aren't necessary. I'd rather just have the algorithm/technique in the conversion explained to me.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Convert a number to a byte array

    The first thing that comes to mind is the BitConverter:
    vb Code:
    1. Dim myBytes() As Byte = BitConverter.GetBytes(396)
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] Convert a number to a byte array

    The output that gives me is 140, 1, 0, and 0. 140 is 3 digits and not two so it cannot fit in the two bit 0xYZ format, no? Or perhaps I need to convert 140 to hex first?

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Convert a number to a byte array

    Quote Originally Posted by Fromethius
    The output that gives me is 140, 1, 0, and 0. 140 is 3 digits and not two so it cannot fit in the two bit 0xYZ format, no? Or perhaps I need to convert 140 to hex first?
    Why do you need them in hex? It will still be the same value, must a different "format".
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [2005] Convert a number to a byte array

    140 is 140, the computer doesn't care if they are in hex or dec. The byte will hold anything up to 255. The only difference between seeing that as hex or dec is which one you would prefer to see it in. However, when you are looking at it, and saying that it has three characters, that it because you are looking at a string representation of the number. The number is one byte, nothing more.
    My usual boring signature: Nothing

  6. #6
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2005] Convert a number to a byte array

    If you still want them in hex (although the computer doesn't care either way), you can use the Hex function.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

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