Is there is a way I can get individual Bytes from a hexa decimal value in java
If I have a hexadecimal value 0x190(400), I want to get 0x90 and 0x01
If I have a hexadecimal value 0x89(137), I want to get 0x89 and 0x00
I am new to these and unable to find a way to get them individually.
Thank you for your help in advance
Thanks
R
If your number is represented as the integral value, you can use bit mask to isolate particular byte.
int value = 0x190;
byte byteValue = (byte) ((value >>> i*8) & 0xff);
String byteAsString = String.format("0x%02x", byteValue);
where i represents i-th byte (starting at 0)
If is string value
like this:
let hexDecimal = "0x89(137)";
let splitValue = hexDecimal .split("(");
let response = splitValue[0]; `
Related
After reading a ByteBuffer into b_array I am trying to convert the ascii values to int.
Output I am expecting is 129 after executing the code as (b_array[] has the decimal equivalents of ascii codes 49,50,59)
Can some one please tell me where am I doing wrong here. I am doing a 0xFF to make it a unsigned value in java and then OR operation to move the bytes.
byte[] b_array=new byte[3];
buffer.get(b_array,0,3);
// Contents inside the b_array in ascii code
// b_array[0]=49;
// b_array[1]=50;
// b_array[2]=57;
int value= b_array[2] & 0xFF | (b_array[1] & 0xFF) << 8 | (b_array[0] & 0xFF) << 16;
System.out.println(value);
Your current approach is effectively treating the three values as a 24-bit number - effectively 0x313239.
It sounds like you should be converting it into a string, then parsing that:
String text = new String(b_array, StandardCharsets.US_ASCII); // "129"
int value = Integer.parseInt(text);
I have binary string String A = "1000000110101110". I want to convert this string into byte array of length 2 in java
I have taken the help of this link
I have tried to convert it into byte by various ways
I have converted that string into decimal first and then apply the code to store into the byte array
int aInt = Integer.parseInt(A, 2);
byte[] xByte = new byte[2];
xByte[0] = (byte) ((aInt >> 8) & 0XFF);
xByte[1] = (byte) (aInt & 0XFF);
System.arraycopy(xByte, 0, record, 0,
xByte.length);
But the values get store into the byte array are negative
xByte[0] :-127
xByte[1] :-82
Which are wrong values.
2.I have also tried using
byte[] xByte = ByteBuffer.allocate(2).order(ByteOrder.BIG_ENDIAN).putInt(aInt).array();
But it throws the exception at the above line like
java.nio.Buffer.nextPutIndex(Buffer.java:519) at
java.nio.HeapByteBuffer.putInt(HeapByteBuffer.java:366) at
org.com.app.convert.generateTemplate(convert.java:266)
What should i do now to convert the binary string to byte array of 2 bytes?Is there any inbuilt function in java to get the byte array
The answer you are getting
xByte[0] :-127
xByte[1] :-82
is right.
This is called 2's compliment Represantation.
1st bit is used as signed bit.
0 for +ve
1 for -ve
if 1st bit is 0 than it calculates as regular.
but if 1st bit is 1 than it deduct the values of 7 bit from 128 and what ever the answer is presented in -ve form.
In your case
1st value is10000001
so 1(1st bit) for -ve and 128 - 1(last seven bits) = 127
so value is -127
For more detail read 2's complement representation.
Use putShort for putting a two byte value. int has four bytes.
// big endian is the default order
byte[] xByte = ByteBuffer.allocate(2).putShort((short)aInt).array();
By the way, your first attempt is perfect. You can’t change the negative sign of the bytes as the most significant bit of these bytes is set. That’s always interpreted as negative value.
10000001₂ == -127
10101110₂ == -82
try this
String s = "1000000110101110";
int i = Integer.parseInt(s, 2);
byte[] a = {(byte) ( i >> 8), (byte) i};
System.out.println(Arrays.toString(a));
System.out.print(Integer.toBinaryString(0xFF & a[0]) + " " + Integer.toBinaryString(0xFF & a[1]));
output
[-127, -82]
10000001 10101110
that is -127 == 0xb10000001 and -82 == 0xb10101110
Bytes are signed 8 bit integers. As such your result is completely correct.
That is: 01111111 is 127, but 10000000 is -128. If you want to get numbers in 0-255 range you need to use a bigger variable type like short.
You can print byte as unsigned like this:
public static String toString(byte b) {
return String.valueOf(((short)b) & 0xFF);
}
I'm writing a little program in android and in it I've a list of byte values in a string variable. something like this :
String src = "255216173005050";
Now, what i need to do is to extract the byte values from the source string and store them in a byte variables. (in above source string, i'll have 5 bytes to store)
For doing this i could successfully read source string and separate the byte values by 3 characters. (255, 216, 173, 005, 050)
The problem is that i failed to convert these strings to their byte values.
it is what I've already done :
String str = "255";
byte b = (byte) Integer.parseInt(str);
By running this, b will be -60 !
Is there
Please help me !
When you write
byte b = (byte) Integer.parseInt(str);
you will get a signed byte. If you look at your int that is discarded using something like
int i = Integer.parseInt(str);
System.out.println(i);
byte b = (byte) i;
you will probably see that i contains the value you want.
This should work. Then just access various indices of the byte array to get the individual pieces. If your text is an abnormal character set - then pass the character set into the getBytes() method.
byte[] bytes = src.getBytes();
Don't use parseInt when you want a byte; instead try Byte.parseByte. Also note that bytes have a range of -128 to 127 (inclusive).
I have a String that holds the string representation of a byte value.
String string = "0xOA";
how do I turn this into a byte with the value 0A?
You can use
byte b = (byte) Integer.decode("0x0A");
This works for strings in octal and decimal. The reason for using Integer is that 0xFF will fail for Byte (as 255 > 127)
You can use
Byte.parseByte(string.substring(2), 16)
The .substring(2) is to get rid of the 0x, and 16 is the radix for hexadecimals.
Did you try Byte.valueOf(String s) ?
Try to
Byte.valueOf(string, 16);
I would like to be able to convert a raw UTF-8 string to a Hex string.
In the example below I've created a sample UTF-8 string containing 2 letters.
Then I'm trying to get the Hex values but it gives me negative values.
How can I make it give me 05D0 and 05D1
String a = "\u05D0\u05D1";
byte[] xxx = a.getBytes("UTF-8");
for (byte x : xxx) {
System.out.println(Integer.toHexString(x));
}
Thank you.
Don't convert to an encoding like UTF-8 if you want the code point. Use Character.codePointAt.
For example:
Character.codePointAt("\u05D0\u05D1", 0) // returns 1488, or 0x5d0
Negative values occur because the range of byte is from -128 to 127. The following code will produce positive values:
String a = "\u05D0\u05D1";
byte[] xxx = a.getBytes("UTF-8");
for (byte x : xxx) {
System.out.println(Integer.toHexString(x & 0xFF));
}
The main difference is that it outputs x & 0xFF instead of just x, this operation converts byte to int, dropping the sign.