long value to byte[] - Java [duplicate] - java

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);

Related

Converting Integer to Byte in Java [duplicate]

This question already has an answer here:
Promotion of byte to int or long
(1 answer)
Closed 7 years ago.
I have the following code:
int i =128;
byte b = (byte) i;
System.out.println( Integer.toBinaryString(i)); //10000000
System.out.println( Integer.toBinaryString(b)); //11111111111111111111111110000000
could someone explain why 1's were added to the left when casting from Integer to Byte and how could a byte carry more than 8 bits !?
You are calling .toBinaryString on the Integer class, so your number is treated as an Integer in any case.
The reason the second call has so many 1 is because it is a negative number. In Java, bytes are signed, so the maximum positive value is 127. By casting 128 into a byte you are actually representing -128. When you cast that small negative number into an 32 bit signed integer as you were doing, all those 1s appear at the beginning.

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

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).

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.

Java two's complement binary to integer [duplicate]

This question already has answers here:
2's complement hex number to decimal in java
(3 answers)
Closed 9 years ago.
I know that converting a decimal to binary with Integer.toBinaryString(355) = 0000000101100011 and
Integer.toBinaryString(-355) = 1111111010011101 (where I take the lower 16 bits of the 32 bit result).
What I would like to do is the other way and take a 16-bit twos's complement binary string and to convert to decimal.
i.e.
0000000000110010 = 50
1111111111001110 = -50
Rather than 1111111111001110 = 65486
How would I do this?
You need to read the result into short.
short res = (short)Integer.parseInt("1111111111001110", 2);
System.out.println(res);
This prints -50.
Use a short? They occupy 16 bits.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

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