• 0

[JAVA] Convert 1-2 bytes into a short


Question

Hi, I have a byte[] and I'm trying to get out 1-2 bytes depending on whether it's 8bit or 16bit and to convert this to a short value. I'm not really sure how to do this as obviously I can't just add the bytes together, and I need to make sure that I know whether it is positive/negative (and little/big endian).

Can anybody explain how to do this?

edit: I am presuming that you can get out the first byte and shift it left by 8, and then add it to the other byte. But then how do I know whether the two bytes are positive or negative.

This surely must be a rather standard problem, so I wonder if there is any sample code anywhere that I can look at?

Edited by lhnz
Link to comment
https://www.neowin.net/forum/topic/709892-java-convert-1-2-bytes-into-a-short/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Bit-shifting should be an easy way to do it, this is from my head and probably wrong, but:

short getValue(byte[] data) 
{	
	short value = data[1];
	value = (value << 8) | data[0];

	return value;
}

Also, please a Java-fellow correct me, but are all Java-based numbers signed and big-endian?

  • 0
  Antaris said:
Also, please a Java-fellow correct me, but are all Java-based numbers signed and big-endian?

Singed, yes, except for char which is 16 bit unsigned. I don't think the Java language definition has anything to say about the big/little endian-ness of the internal implementation (unless I missed it).

  • 0
  lhnz said:
Hi, I have a byte[] and I'm trying to get out 1-2 bytes depending on whether it's 8bit or 16bit and to convert this to a short value. I'm not really sure how to do this as obviously I can't just add the bytes together, and I need to make sure that I know whether it is positive/negative (and little/big endian).

Can anybody explain how to do this?

edit: I am presuming that you can get out the first byte and shift it left by 8, and then add it to the other byte. But then how do I know whether the two bytes are positive or negative.

This surely must be a rather standard problem, so I wonder if there is any sample code anywhere that I can look at?

how are you supposed to know if its 8 bit or 16 bit? is there a flag to tell u so?

and how do u know if its positive or negative? for example the binary representation of 255 is 11111111.. and if its signed it would be -127 isnt it? (something like that) so there is no way to tell if its negative or positive unless theres a flag that tells you so

  • 0

We need a bit more definition on this - how exactly do you need to "convert" two bytes to a short? If it's just concatenation then Anatris' solution is what you need (although maybe in the opposite order?), but if you want to interpret the byes as signed then you need to specify what result you would expect to see from pairs like, say 100,101 100,-101 -100,101 -100,-101

If you want to use the sign of both bytes (ie byte[0]*2^16 + byte[1]) then use byte[0]*2^16 + byte[1];

Edited by JamesCherrill
  • 0
  rejinderi said:
how are you supposed to know if its 8 bit or 16 bit? is there a flag to tell u so?

and how do u know if its positive or negative? for example the binary representation of 255 is 11111111.. and if its signed it would be -127 isnt it? (something like that) so there is no way to tell if its negative or positive unless theres a flag that tells you so

Flags that I am able to use for that are here: http://java.sun.com/j2se/1.4.2/docs/api/ja...udioFormat.html

  Antaris said:
short getValue(byte[] data)
{	
	short value = data[1];
	value = (value << 8) | data[0];

	return value;
}

This looks kind of like what I need. Is this for big-endian or little-endian data however? And what happens if the two bytes contain a negative or positive value?

I'll just give a little more information just in case others can help me further:

Okay, basically what I have is a byte array of the data inside a wav file. (Which I already know how to read in already etc...)

However in order to be able to easily calculate on this later on I want to convert the byte[] to a short[].

Obviously depending on the framesize (1 if 8bit, and 2 if 16bit) of the wav data each sample should either be 8 bytes or 16bytes long. So if the data is 16bit I will need to get 2 bytes from the byte[] and turn them into one short value. And if it is 8bit, I will need to get 1 byte and turn it into a short sample...

So what I'm asking in psuedo code is:

byte[] byteArray; // contains every single byte of the wav file in order.
short[] samples = new short[numberOfSamples];

short sample = 0;
if (frameSizeOfWavData = 1) { // Wav data is 8bits so get out one byte of sample and place it in the sample.
	sample = byteArray[0];
	samples[0] = sample;
} else { // Wav data is 16bits so get out 2 bytes of the byteArray
	sample = (byteArray[0] << 8) + byteArray[1]; // This is my guess of what I kind of need to do but I don't understand
	samples[0] = sample;
}

My issue is that I don't know how you can read a certain number of bytes into one short correctly.

Edited by lhnz
  • 0

I currently have:

	private short getSampleFromBytes(byte[] sampleBytes) {
		short sample = 0;
		for (int i = 0; i < sampleBytes.length; ) {
			int shift = 8 * (sampleBytes.length - (i + 1));
			sample |= (sampleBytes[i]) << shift;
		}
		return sample;
	}

Does that look right?

  • 0
  lhnz said:
I currently have:

	private short getSampleFromBytes(byte[] sampleBytes) {
		short sample = 0;
		for (int i = 0; i < sampleBytes.length; ) {
			int shift = 8 * (sampleBytes.length - (i + 1));
			sample |= (sampleBytes[i]) << shift;
		}
		return sample;
	}

Does that look right?

what does the sample bytes stand for?.. you dont need to convert everything into a short array?.. why are u ORing them this way.. i dont understand i thought u were supposed to like take the sample bytes and then convert them all into a short array depending on the framesize?

		private short GetShort(byte[] b)
		{
			short retVal;

			if (b.Length == 1)
				retVal = b[0];
			else 
				retVal = (short)(b[1] << 8 | b[0]); //for big endian
				// retVal = (short)(b[0] << 8 | b[1]);//for little endian

			return retVal;
		}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.