byte z[] = new byte[5]; - What does this mean? [duplicate] - java

This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 7 years ago.
I'm currently trying to get my head around a piece of code that I found. Firstly, I'm not sure why the code appears like:
byte z[] = new byte[5];
instead of:
byte[] z = new byte[5];
I mean, is byte z[] not there to declare an array of bytes? Or is this code doing something else?
Secondly, why would someone choose bytes over doubles or ints. It seems that a byte is any number from -128 to 127. What's the point in choosing that over a double?

byte[] z is equivalent to byte z[] are just two different methods of represent the same.
Note that the java code conventions of sun (before becoming Oracle) propose to use the first over the second. So it preferreable to use
byte[] z = new byte[5];
instead of
byte z[] = new byte[5];
For the second part of your question.
byte uses 1 byte
char uses 2 bytes
int uses 4 bytes
long uses 8 bytes
So the best is to use the smallest numeric type to avoid memory occupancy.

For the array declaration: Java supports c-style array declaration aswell as it's own way that is usually used. byte[] z is equivalent to byte z[]. byte takes less memory than double and int and provides better performance than bigger datatypes (especially better than double).

Related

Migrate a int conversion from byte from C++ to Java

I have the following piece of code in C++
int magic;
stream.read(&magic, sizeof(magic));
Which stores the value of magic from an array of bytes.
I want to migrate it to Java, so far I have this:
int magic = stream[0];
But it is not working. I think that it is due to the length of the ints in Java and C++. Shall I use two bytes in the Java part to retrieve the proper magic number?
byte[] stream = ...
ByteBuffer buf = ByteBuffer.wrap(stream);
buf.order(ByteOrder.LITTLE_ENDIAN);
int magic = buf.readInt();
See ByteBuffer. A java int is always 4 bytes signed, and per default java uses a BIG_ENDIAN byte order, so you might want to set a reversed order.

Converting int to byte in java [duplicate]

This question already has answers here:
Assigning int to byte in java?
(5 answers)
Closed 8 years ago.
I have an int variable in java whose value is less than 256. So Can I store it in a Single byte variable.
I performed the following code.
int i =247;
byte b = (byte) i ;
But When I print it,
System.out.println(" i = "+(int)b);
It outputs
i = -9
Is it possible to convert int values less than 256 to single byte variables.
Pls. Help..
This isn't going to work. The MAX_VALUE for byte is 127 because bytes are signed in Java. (Consequently, the MIN_VALUE is -128. This is your typical Two's Compliment pattern.)
That being said, you have a couple options:
Leave it as an int and just use an int for your calculations. This might even be faster for you.
Leave it as a byte even if the number isn't correct. The numbers might not be correct in Java, but if all you care about is the bits being correct, they will be correct. So you can send that bite (which is a -9 in Java) across a webservice to another app that reads it as an unsigned byte, and it will read it as 247 correctly.

Can one force String.valueOf() to return unsigned value in Java? [duplicate]

This question already has answers here:
Can we make unsigned byte in Java
(17 answers)
Closed 9 years ago.
I have a byte array:
byte[] a = new byte[3];
which I have added some bytes. For this example, let's say 3, 4, and 210.
I would like to print this string of bytes to look like 3 4 210, but instead I get 3 4 -46
I am using String.valueOf(a[i]) to do my conversion. Is there any way to force this conversion to give unsigned values?
Thanks in advance,
EDIT: Thanks to the various feedback on this question. I had not realized Java Bytes were signed values by default, and so was suspecting the String.valueOf() method as being the issue. It turns out just simply using
String.valueOf(a[i]&0xFF)
takes care of the signed formatting issue.
Again, thank you for your feedback!
Guava provides a UnsignedBytes class that can make that conversion. The static toString(byte) method
Returns a string representation of x, where x is treated as unsigned.
For example
System.out.println(UnsignedBytes.toString(a[i]));
where a[i] = -46 would print
210
Internally, all this does is call
public static int toInt(byte value) {
return value & UNSIGNED_MASK; // UNSIGNED_MASK = 0xFF
}
and convert the int to a String which it returns.
For an explanation
With
someByte & 0xFF
since OxFF is an integer literal, the someByte value is widened to an int. Let's take for example the value -46. Its binary representation is
11111111111111111111111111010010
The binary representation of 0xFF is
11111111 // ie 255
if you and & the two
11111111111111111111111111010010
00000000000000000000000011111111
--------------------------------
00000000000000000000000011010010
which is equal to
210
Basically you only keep the lower 8 bits of the int.
Java byte data type range is minimum value of -128 and a maximum value of 127 (inclusive). String.valueOf(a[i]) doesn't do this conversion. Use int type instead.
byte
byte range limit is within -128 to 127,
so for 210 it gives -46. so convert it using int type
You've run into Java's famous problem of bytes treated as signed even though most of the real world prefers these unsigned. Try this:
int[] signedArr = new int[a.length];
for (int i=0; i<a.length; ++i) {
signedArr[i] = a[i] & 0xff;
}
Then you can work with signedArr.

long value to byte[] - Java [duplicate]

This question already has answers here:
How do I convert Long to byte[] and back in java
(18 answers)
Closed 9 years ago.
I have a decimal number and I would convert it in an array buffer of byte (little endian notation).
I try this but I am not sure it is working:
ByteBuffer a = ByteBuffer.allocate(4);
a.putInt( (int) number);
return a.array();
If number is 125 I have returned:
[0,0,0,128];
Is it correct? I think that the correct conversion would be:
[0,0,0,10000000]
How can I do with Java?
Thanks in advance
1) from ByteBuffer API
The initial order of a byte buffer is always BIG_ENDIAN.
so, you have to change the byte buffer order to little endian in the first place
2) there is only one representation of decimal number in Java, it is BigDecimal, and cannot cast it to int. Thus, this cannot work
a.putInt( (int) number);

How do I convert the contents of a byte[] array from hex to decimal? [duplicate]

This question already has answers here:
Performance question: Fastest way to convert hexadecimal char to its number value in Java?
(13 answers)
Closed 9 years ago.
I have a targetDataLine open...
byte[] bytes = new byte[line.getBufferSize() / 5];
line.read(bytes, 0, bytes.length);
The bytes array is in hex, how do I get it's contents to be represented as decimal values?
int decValue = Integer.parseInt(hexString, 16);
Resources: http://www.tutorialspoint.com/java/number_parseint.htm
Unsure of the OP's posting language.
In Python, if you have an array and you want to get the decimal values of the bytes, you could loop through and use ord() to get the decimal representation.
If you had to convert from an actual array containing hex values, then you can do the conversion as per this StackOverflow posting:
Convert hex string to int in Python

Categories

Resources