Public Group

Convert a number to a byte array

This topic is 3903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 Oh, btw, this is for C#. However, I know VB.NET, C and C++ as well if you only have the code for that. Or perhaps you could just explain the technique in the conversion.

Share on other sites
Did you try BitConverter.GetBytes()?

Share on other sites

If this is C/C++ you could create a STRUCT which is a UNION of an array of bytes (matching the number type size) and the type itself. You would then set the variable of that STRUCT type via its numeric element and then access all the bytes via the array.

Be aware though that the storage of the bytes of the numeric value may not be the order you think they are ..... (that Big-Endian vs Little-Endian stuff)

Share on other sites
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?

Share on other sites
A single 8-bit byte can hold 256 values: 0 to 255.

140 + 1 * 256 = 396

Share on other sites
This is how I would do it in C:

Suppose you had a long integer and wanted to store it in 4 bytes

long x=0x67452301;unsigned char y[4];y[0] = x & 0x000000ff;        //y[0] is the least significant bytey[1] = (x & 0x0000ff00) >> 8;y[2] = (x & 0x00ff0000) >> 16;y[3] = (x & 0xff000000) >> 24

Share on other sites

class Hex{    List<string> hex_list;    public Hex(int value)    {        hex_list = new List<string>();                Byte[] byteArray = BitConverter.GetBytes(value);        foreach (byte in_value in byteArray)        {            AddHex(in_value);        }    }    private void AddHex(byte in_byte)    {        byte first_significant = (byte)(in_byte % 16);        byte second_significant = (byte)((in_byte - first_significant)/16);        string temp = "0x" + GetHexString(second_significant) + GetHexString(first_significant);        hex_list.Add(temp);    }    private string GetHexString(byte value)    {        switch(value)        {            case 1:                return "1";            case 2:                return "2";            case 3:                return "3";            case 4:                return "4";            case 5:                return "5";            case 6:                return "6";            case 7:                return "7";            case 8:                return "8";            case 9:                return "9";            case 10:                return "A";            case 11:                return "B";            case 12:                 return "C";            case 13:                return "D";            case 14:                return "E";            case 15:                return "F";            default:                return "0";            }    }}

Hope that helps.

theTroll

Thanks! It does!

1. 1
Rutin
68
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 21
• 10
• 33
• Forum Statistics

• Total Topics
633438
• Total Posts
3011880
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!