Convert byte[] to short[]
Howdy,
I have a byte[] that I need to read in from an external program as an unsigned byte[]. Java doesn't support unsigned types, so I found an article that explained to convert the datatype to the next largest in Java to work around the issue; in this case, I must convert it to short[].
How would I go about doing this? I've tried casting it, but that won't work; I assume I need to do some type of bit shifting.
Can anyone point me in the right direction?
Thanks,
Dan
[512 byte] By [
Djaunla] at [2007-11-27 7:28:00]

Bits are bits.
The type that you need in java depends on the usage in java.
Your real problem is probably that you are displaying them and do not realize that display is not the same as bits.
In terms of int you can produce a 'normal' value with the following code.
byte b = ...
int i = 0x0ff & b;
Well the reason I need this is because I'm sending the byte[] into MATLAB, which then should display it as an image. Because there is no unsigned type, some of the values are appearing as negative, and the image does not display properly.
The reason I wish to convert it to short[] is because short[] is mapped to an int16 in MATLAB, which should theoretically display the image better.
As of now, for example, the byte data in MATLAB cannot go past 128 without being represented as negative, which I do not want.
> Well the reason I need this is because I'm sending
> the byte[] into MATLAB, which then should display it
> as an image. Because there is no unsigned type, some
> of the values are appearing as negative, and the
> image does not display properly.
That is a description of requirements, not a description of code.
>
> The reason I wish to convert it to short[] is because
> short[] is mapped to an int16 in MATLAB, which should
> theoretically display the image better.
>
> As of now, for example, the byte data in MATLAB
> cannot go past 128 without being represented as
> negative, which I do not want.
And still not code.
1. You are using an interface - what does that interface consist of (file, socket, java api, etc)?
2. short is either a java type or a MATLAB type. Just because they have the same name doesn't make them the same. You need to determine bit size.
3. In java if you start with a byte (8 bits) then it certainly will fit in a short (16 bits). You can also ignore sign extension and end up with the 'wrong' value (which my previous code sample demonstrates how to preclude.) But then that actually depends on how it is being used.
> 1. You are using an interface - what does that
> interface consist of (file, socket, java api, etc)?
If you mean the interface to retrieve the byte[] data, that is just a simple service that sends me a byte[] of raw image data taken from a camera feed.
> 2. short is either a java type or a MATLAB type.
> Just because they have the same name doesn't make
> them the same. You need to determine bit size.
> . In java if you start with a byte (8 bits) then it
> certainly will fit in a short (16 bits). You can
> also ignore sign extension and end up with the
> 'wrong' value (which my previous code sample
> demonstrates how to preclude.) But then that
> actually depends on how it is being used.
Each byte in the array has 8 bits. What I'm trying to do is feed the bytes into MATLAB so I can use MATLAB to convert it to an image. However, because there are no unsigned types and I need 8 unsigned bits, the image does not display properly in MATLAB; any value above 128 is negative.
I have tried converting the byte[] to an int[] based on the code you provided, but it does not seem to work; I must be doing something wrong:
public int[] getInput() {
int[] arr = new int[in.length]; // in is the byte[]; this byte[] is already populated with data when this function is called
for (int i = 0; i<in.length; i++) {
arr[i] = 0x0ff & in[i];
System.out.print(arr[i] + " ");
}
return arr;
}
When I try to return the int[], I get a NullPointerException in MATLAB; it works fine if I simply return the original byte[], in.
I have not done much work with stuff like this, so I'm not really too sure how to proceed.
Thanks for the help so far.>
Hi,
Thanks again for the help. I found that the NullPointerException was being thrown because the byte[] data was not being returned from the service fast enough, so I just made the current Thread sleep for a few seconds.
I used this code to convert to short[]:
byte[] in = client.getInput();
short[] read = new short[in.length];
for (int i = 0; i < in.length; i++) {
read[i] = (short)((short)in[i] & 0xff);
//System.err.print(read[i] + " ");
}
I found a great article on byte conversion [url=http://www.crazysquirrel.com/computing/java/basics/how-to-convert-bytes.jspx]here[/url].
Thanks again for the help; your replies helped me learn some of the concepts better, which is what I believe I really needed.
> > 1. You are using an interface - what does that
> > interface consist of (file, socket, java api,
> etc)?
>
> If you mean the interface to retrieve the byte[]
> data, that is just a simple service that sends me a
> byte[] of raw image data taken from a camera feed.
>
That isn't it.
Your question is how you get info into MATLAB, so the question is what interface are you using to MATLAB.
> When I try to return the int[], I get a
> NullPointerException in MATLAB; it works fine if I
> simply return the original byte[], in.
Ok. But that has nothing to do with the code that you posted.
The array returned from the method certainly isn't null.
This suggests that either the code from the method to the MATLAB interface is wrong or you as using a MATLAB interface incorrectly.
> That isn't it.
>
> Your question is how you get info into MATLAB, so the
> question is what interface are you using to MATLAB.
Oh, sorry about that. I'm simply using MATLAB's methods to call Java; I put my JAR file on the static classpath, and just instantiate the classes and call their methods in MATLAB like I would normally do in a Java application. For example, this is code from my M-file:
driver = vigra.matlab.LOTFDriver();
data = driver.getImageData();
> Ok. But that has nothing to do with the code that
> you posted.
> The array returned from the method certainly isn't
> null.
>
> This suggests that either the code from the method to
> the MATLAB interface is wrong or you as using a
> MATLAB interface incorrectly.
The NPE actually turned out to be a threading issue that was me being stupid; the data was returning in another Thread, and I was trying to retrieve the data in the main Thread -- no luck there.
And so you have solved your problem?
Yea. I thought I said so in reply #5, but I guess I wasn't really clear about it.