3

I am trying to convert a short type into 2 bytes type for store in a byte array, here is the snippet thats been working well "so far".

if (type == "short")
{
   size = data.size;
   databuffer[index+1] = (byte)(data.numeric_data >> 8);
   databuffer[index] = (byte)(data.numeric_data & 255);
   return size;
}

Numeric_data is int type. It all worked well till i process the value 284 (decimal). It turns out that 284 >> 8 is 1 instead of 4.

The main goal is to have:

byte[0] = 28
byte[1] = 4
Cœur
  • 37,241
  • 25
  • 195
  • 267
ffenix
  • 543
  • 1
  • 5
  • 22

5 Answers5

4

Is this what you are looking for:

    static void Main(string[] args)
    {
        short data=284;

        byte[] bytes=BitConverter.GetBytes(data);
        // bytes[0] = 28
        // bytes[1] = 1
    }
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
2

Just for fun:

public static byte[] ToByteArray(short s)
{
    //return, if `short` can be cast to `byte` without overflow
    if (s <= byte.MaxValue) 
        return new byte[] { (byte)s };

    List<byte> bytes = new List<byte>();
    byte b = 0;
    //determine delta through the number of digits
    short delta = (short)Math.Pow(10, s.ToString().Length - 3);
    //as soon as byte can be not more than 3 digits length
    for (int i = 0; i < 3; i++) 
    {
        //take first 3 (or 2, or 1) digits from the high-order digit
        short temp = (short)(s / delta);
        if (temp > byte.MaxValue) //if it's still too big
            delta *= 10;
        else //the byte is found, break the loop
        {
            b = (byte)temp;
            break;
        }
    }
    //add the found byte
    bytes.Add(b);
    //recursively search in the rest of the number
    bytes.AddRange(ToByteArray((short)(s % delta))); 
    return bytes.ToArray();
}

this recursive method does what the OP need with at least any positive short value.

horgh
  • 17,918
  • 22
  • 68
  • 123
  • this is the perfect answer, how you can do it with one line? – ffenix Feb 08 '13 at 07:06
  • @ffenix: Like everyone said, this is the wrong way to do it. (Sorry, not one line here, my bad) – leppie Feb 08 '13 at 07:06
  • i still dont know why the wrong way, client is expecting that and no its not hex, visual studio shows me everything on decimal. so yes this is what i needed. – ffenix Feb 08 '13 at 07:09
  • 1
    @ffenix please, check this another time....your original code did used bytes (like in the answer by ja72) – horgh Feb 08 '13 at 07:16
1

Why would 284 >> 8 would be 4?

Why would 284 be split in two bytes equal to 28 and 4?

The binary representation of 284 is 0000 0001 0001 1100. As you can see, there are two bytes (eight bits) which are 0000 0001 (256 in decimal) and 0001 1100 (28 in decimal).

284 >> 8 is 1 (0000 0001) and it is correct.

284 should be split in two bytes equal to 256 and 24.

You conversion is correct!

ybo
  • 16,967
  • 2
  • 28
  • 31
  • thanks but that didn't solve or help me in any way i still want to achieve: byte[0] = 28 (decimal) byte[1] = 4 (decimal) in base of a short value. – ffenix Feb 08 '13 at 06:34
  • In that case, my questions remain open. Why would it be 28 and 4? Why not 2 and 84? What happens to 3284? What is the rule for conversion? – ybo Feb 08 '13 at 06:41
  • because 284 means the buffer size, and i need from that buffer the first 2 bytes to indicate the size which is 284... – ffenix Feb 08 '13 at 06:44
  • That certainly doesn't explain why you don't keep the binary representation or why you divide by 10 to populate the least significant byte. – ybo Feb 08 '13 at 06:53
1

If you insist:

short val = 284;
byte a = (byte)(val / 10);
byte b = (byte)(val % 10);

Disclaimer:

This does not make much sense, but it is what you want. I assume you want values from 0 to 99. The logical thing to do would be to use 100 as the denominator and not 10. But then again, I have no idea what you want to do.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • What i am doing is sending a packet, the first 2 bytes are used to indicate the packet len. After all the processing the packet size is 284 so the first 2 bytes should be 284 or 0x11C – ffenix Feb 08 '13 at 06:46
  • 3
    As everyone else here already said, you are mixing up hex and dec... short val = 0x284 would give the expected result with your original code.. given that you fail to see this, I suspect you just grabbed that code from somewhere ? ;) – Roger Johansson Feb 08 '13 at 06:55
1

Drop the nonsense conversion you are using and go for System.BitConverter.ToInt16

  //to bytes
  var buffer = System.BitConverter.GetBytes(284); //your short value

  //from bytes
  var value = System.BitConverter.ToInt16(buffer, 0);
Roger Johansson
  • 22,764
  • 18
  • 97
  • 193