Mohanavel
Mohanavel

Reputation: 1781

number formation for byte conversion

How to format the number which can be converted to Byte[] (array);

I know how to convert the result value to byte[], but the problem is how to format the intermediate number.

This is by data,

  int packet = 10;
  int value = 20;
  long data = 02; // This will take 3 bytes [Last 3 Bytes]

i need the long value, through that i can shift and I'll fill the byte array like this

Byte[0] = 10;
Byte[1] = 20;
Byte[2] = 00;
Byte[3] = 00;
Byte[4] = 02;

Byte 2,3,4 are data

but formatting the intermediate value is the problem. How to form this

Here is the sample

long data= 683990319104; ///I referred this as intermediate value.

This is the number i receiving from built in application.

 Byte[] by = new byte[5];

            for (int i = 0; i < 5; i++)
            {
                by[i] = (byte)(data & 0xFF);

                data>>= 8; 
            }


  Here 

 Byte[0] = 00;
 Byte[1] = 00;    //Byte 0,1,2  are Data  (ie data = 0)
 Byte[2] = 00;

 Byte[3] = 65;   //Byte 3 is value  (ie Value = 65)
 Byte[4] = 159;  // Byte 4 is packet (is Packet = 159);

This is one sample.

Currently BitConverter.GetBytes(..) is ues to receive.

Byte while sending, the parameter is long.

So i want the format to generate the number 683990319104 from

packet = 159
value = 65
data = 0

I think now its in understandable format :)

Upvotes: 1

Views: 319

Answers (3)

Mohanavel
Mohanavel

Reputation: 1781

oooh,

Its simple




    int packet = 159
    int value = 65
    int data = 0


    long address = packet;

    address = address<<8;
    address = address|value;
    address = address<<24;
    address = address|data;

now the value of address is 683990319104 and i termed this as intermediate value. Let me know the exact term.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062865

The use of 3 bytes to define the long seems unusual; if it is just the 3 bytes... why a long? why not an int?

For example (note I've had to make assumptions about your byte-trimming based on your example - you won't have the full int/long range...):

static void Main() {
    int packet = 10;
    int value = 20;
    long data = 02;

    byte[] buffer = new byte[5];
    WritePacket(buffer, 0, packet, value, data);
    for (int i = 0; i < buffer.Length; i++)
    {
        Console.Write(buffer[i].ToString("x2"));
    }
    Console.WriteLine();
    ReadPacket(buffer, 0, out packet, out value, out data);
    Console.WriteLine(packet);
    Console.WriteLine(value);
    Console.WriteLine(data);
}
static void WritePacket(byte[] buffer, int offset, int packet,
    int value, long data)
{
    // note I'm trimming as per the original question
    buffer[offset++] = (byte)packet;
    buffer[offset++] = (byte)value;

    int tmp = (int)(data); // more efficient to work with int, and
                           //  we're going to lose the MSB anyway...
    buffer[offset++] = (byte)(tmp>>2);
    buffer[offset++] = (byte)(tmp>>1);
    buffer[offset] = (byte)(tmp);
}
static void ReadPacket(byte[] buffer, int offset, out int packet,
     out int value, out long data)
{
    // note I'm trimming as per the original question
    packet = buffer[offset++];
    value = buffer[offset++];
    data = ((int)buffer[offset++] << 2)
        | ((int)buffer[offset++] << 1)
        | (int)buffer[offset];
}

Upvotes: 1

jerryjvl
jerryjvl

Reputation: 20131

Not entirely sure what you are asking exactly, but I think you are looking for BitConverter.GetBytes(data).

Upvotes: 2

Related Questions