1 2 Previous Next 26 Replies Latest reply: Jul 24, 2007 3:23 PM by 807605

# How to cram two bytes into a short?

Hi all,

I have some byte[] data that I am receiving, and I need to put two bytes into one short (or int, or whatever), as the byte[] will form a two bytes-per-pixel image. I've been working with 8-bit data (this is 12-bits, NOT packed), which is much easier to manipulate.

I currently have this method to convert a byte[] of 8-bit data into a short (I do this because Java does not support signed types and I need that 8th bit):
``````private short[] convertBytesToShorts(byte[] data) {
short[] convertedData = new short[data.length];
for (int i = 0; i < data.length; i++) {
convertedData[i] = (short)((short)data[i] & 0xff);
}
return convertedData;
}``````
Is there a way to modify this to place two bytes into one short? Is it possible to just AND two bytes with 0xff and add them together to create one short? Surely it cannot be that simple. Also, remember that I must treat the values as unsigned.

Furthermore, the two bytes look like this:
`| xxxx xxxx | xxxx 0000 |`
So the last four bits in the second byte are 0s.

Message was edited by:
Djaunl
• ###### 1. Re: How to cram two bytes into a short?
You need to bit shift one of the bytes by 8 positions, then and the two bytes together.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
• ###### 2. Re: How to cram two bytes into a short?
Is there a way to modify this to place two bytes into
one short? Is it possible to just AND two bytes with
0xff and add them together to create one short?
Surely it cannot be that simple.
What happened when you wrote and tested code to do just that?

Also, remember that
I must treat the values as unsigned.
How is that relevant? Signed or unsigned the bytes are what they are and they'll be the same when you put them into the short.

You cannot "treat" bytes or shorts as unsigned, though, because they are signed. (Well, you can kind of treat them that way--but not if you care about the numerical values.)

So the last four bits in the second byte are 0s.
Not relevant.
• ###### 3. Re: How to cram two bytes into a short?
Untested:
``````    static short[] packBytesToShorts(byte[] data) {
short[] convertedData = new short[data.length/2];
for(int i = 0; i < convertedData.length; ++i) {
int result = (data[2*i] << 8) | (0xff & data[2*i+1]);
convertedData[i] = (short) result;
}
return convertedData;
}``````
• ###### 4. Re: How to cram two bytes into a short?
I've been working with 8-bit
data (this is 12-bits, NOT packed),
I currently have this method to convert a byte[] of
8-bit data into a short (I do this because Java does
not support signed types and I need that 8th bit):
Yes it does. The fact that it displays a byte as and integer and the conversion in that process produces a negative display value has nothing at all to do with the bits.
Is there a way to modify this to place two bytes into
one short?
short s = (short)(((b1 << 8) & 0x0ff) | (b2 & 0x0ff))

Of course you still have to deal with getting the order correct.

(You can probably get rid of that first mask but you would need to test that.)
• ###### 5. Re: How to cram two bytes into a short?
``````#! /usr/bin/groovy

b1 = 0xF0
b2 = 0x0F
s1 = ((b1 << 8) & 0xFF00) | (b2 & 0xFF)
assert s1 == 0xF00F``````
~
• ###### 6. Re: How to cram two bytes into a short?
assert s1 == 0xF00F
I asserted all kinds of things the last time I stepped in ox foof.
• ###### 7. Re: How to cram two bytes into a short?
> I asserted all kinds of things the last time I
stepped in ox foof.

It felt better than 0xFACE.

~
• ###### 8. Re: How to cram two bytes into a short?
assert s1 == 0xF00F
I asserted all kinds of things the last time I stepped in ox foof.
And that's usually the moment you meet that cafe babe.
• ###### 9. Re: How to cram two bytes into a short?
> And that's usually the moment you meet that cafe babe.

~
• ###### 10. Re: How to cram two bytes into a short?
d00d,

Why all the Groovy? Part of the current problem is getting the casting right, too.
• ###### 11. Re: How to cram two bytes into a short?
> Why all the Groovy?

Because it's quick, expressive, and easy to see what's going on. It writes like pseudocode, but actually runs.

> Part of the current problem is
getting the casting right, too.

I didn't see that as part of the problem, but that's really not that big of a deal, is it? Just cast the overall expression as a short.

~
• ###### 12. Re: How to cram two bytes into a short?
I didn't see that as part of the problem, but that's really not that big of a deal, is it? Just cast the overall expression as a short.
No, it's not a big deal, but some people may not realize that (expr1 | expr2) will never have type short.
• ###### 13. Re: How to cram two bytes into a short?
I havent been following closely but maybe there is a much better
way to do this.

If you have a byte array where every 2 bytes is a short why not
use the NIO (or even regular IO) library?
The NIO has the advantage that you can switich Endian mode (which isnt
applicable here).

Can you grab the byte[] as a ByteArrayInputStream and call
• ###### 14. Re: How to cram two bytes into a short?
``````import java.io.*;

public static void main(String[] args){

byte[] data = new byte[]{
1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16};

try{

ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);