JAVA: get UTF-8 Hex values from a string? - java

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.

Related

Convert from hex to int and vice versa in java

I have next method for converting to int from hex:
public static byte[] convertsHexStringToByteArray2(String hexString) {
int hexLength = hexString.length() / 2;
byte[] bytes = new byte[hexLength];
for (int i = 0; i < hexLength; i++) {
int hex = Integer.parseInt(hexString.substring(2 * i, 2 * i + 2), 16);
bytes[i] = (byte) hex;
}
return bytes;
}
when I use it for "80" hex string I get strange, not expected result:
public static void main(String[] args) {
System.out.println(Arrays.toString(convertsHexStringToByteArray2("80"))); // gives -128
System.out.println(Integer.toHexString(-128));
System.out.println(Integer.toHexString(128));
}
the output:
[-128]
ffffff80
80
I expect that "80" will be 128. What is wrong with my method?
I have next method for converting to int from hex
The method you posted converts a hex String to a byte array, and not to an int. That's why it is messing with its sign.
Converting from hex to int is easy:
Integer.parseInt("80", 16)
$1 ==> 128
But if you want to get a byte array for further processing by just casting:
(byte) Integer.parseInt("80", 16)
$2 ==> -128
It "changes" its sign. For further information on primitives and signed variable types take a look at Primitive Data Types, where it says:
The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.
One could easily invert the sign by just increasing the value to convert:
(byte) Integer.parseInt("80", 16) & 0xFF
$3 ==> 128
That gets you a byte with the value you expect. Technically that result isn't correct and you must to switch the sign again, if you want to get an int or a hex string back again. I'd suggest you to don't use a byte array if you only want to convert between hex and dec.
A byte in Java stores numbers from -128 to 127. 80 in hex is 128 as an integer, which is too large to be stored in a byte. So, the value wraps around. Use a different type to store your value (such as a short).

Getting individual bytes from hexadecimal value

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]; `

Compiler error "Incompatible types" on byte literal

I have seen many cases where a byte is declared but where the value from a method like
intToByte or StringToByte is casted to a byte because the programmer is provideing i.e. a hexadecimal- value, an Integer- or a String-value.
I am trying to assign an actual byte value to the variable without any casting or methods to parse, like so:
public class ByteTest {
/**
* This array will be used to hold three characters, together forming a string.
*/
private static byte[] string;
/**
* The main method of the program, where the byte-array is coming to use.
*/
public static void main(String args[]) {
//Construct the array with a limit to three bytes.
string = new byte[3];
/*
* Fill the three bytes with the binary values to create "O", "l" and "e".
*/
string[0] = 01001111;
string[1] = 01101100;
string[2] = 01100101;
//Print out "Ole".
System.out.println(string[0] + string[1] + string[2]);
}
}
But I get the following error in the compiler:
java\ByteTest.java:8: error: incompatible types: possible lossy conversion from int to byte
string[0] = 01001111;
^
java\ByteTest.java:9: error: incompatible types: possible lossy conversion from int to byte
string[1] = 01101100;
^
java\ByteTest.java:10: error: incompatible types: possible lossy conversion from int to byte
string[2] = 01100101;
^
Appearently, what I think of as eight bits, the compiler thinks of as eight integers.
Is there any other solution to this, where I can provide actually bits directly to the variables/array?
Indicate binary
string[0] = 0b01001111;
string[1] = 0b01101100;
string[2] = 0b01100101;
This reminds me of the joke: there are 10 kinds of programmers: those that understand binary and those that do not.
As bytes are signed there still is a problem with 0b1xxxxxxx which would need to be a negative number. In that case use the following trick:
string[2] = 0b11100101 - 256;
string[2] = (byte) 0b11100101; // Or simply cast the int range value.
Also binary is ideal for an underscore usage:
string[2] = 0b0110_0101; // 0x65
And is commented by #BackSlash: bytes are binary data. To interprete them as text they have to be associated with some Charset/encoding.
String s = new String(string, StandardCharsets.US_ASCII);
System.out.println(s);
This converts the bytes, interpreting them as ASCII to the Unicode that String uses (to combine all scripts of the world).
Adding 0 in front of constant number ( like 01101100 ) is interpreted as octal value
What do you need to do to fix this?
The simplest solution which will use the least memory (code and data) is also the simplest.
private static final String string = "Ole";
System.out.println(string);
otherwise you can do this
private static final char[] chars = {
(char) 0b01001111,
(char) 0b01101100,
(char) 0b01100101 };
String s = new String(chars);
System.out.println(s);
Note: characters in Java are 16-bit unsigned char, not 8 bit byte
To get an idea of why the class file is bigger you can dump the class file with
java -c -v -cp target/classes mypackage.MyClass
To start with 01001111 is in octal, not binary. To write a binary number, you need 0b01001111
Numbers don't "remember" how many leading zeros you gave it, and generally speaking, leading zeros are dropped when printed.
The default format for a number is decimal, not binary.
When you add two, or three numbers, you get another number. Assuming you got this to compile it would print something like
288
or whatever the sum of the values are.
BTW it is really confusing to name an int called "string" because this could be assumed to be a String
Assign Actual value :-
String a ="100101";
System.out.println(""+a);
Output :- 100101
Binary to integer conversion and then assign value to string variable :-
String a=""+0b100101
System.out.println(""+a);
Output: 37

Java byte Insert value error string to byte

I have class that have one member myByte as follows.
public class ByteClass {
private Byte myByte;
public Byte getMyByte() {
return myByte;
}
public void setMyByte(Byte myByte) {
this.myByte = myByte;
}
}
I have string value which is FF and I need to assign it to the class member,
how should I do that since when I try it ass follows I got error in the compile time
Type mismatch: cannot convert from byte[] to byte I understand that I cant use Array of byte for the string but I have tried to do that in several of ways without any success .any Idea how can I set the value FF to the class ?
public class ByteHanlder {
public static void main(String[] args) {
String str = "FF";
byte temp = str.getBytes();
Byte write_data = new Byte(temp);
ByteClass byteClass = new ByteClass();
byteClass.setMyByte(temp);
System.out.println(byteClass.getMyByte());
}
}
Assuming you really do just want to store a byte, you can use:
int value = Integer.parseInt("FF", 16);
x.setMyByte((byte) value);
Now that will give you a value of -1 if you look at it in the debugger - but that's just because bytes are signed, as other answerers have noted. If you want to see the unsigned value at any time, you can just use:
// Only keep the bottom 8 bits within a 32-bit integer, which ends up
// treating the original byte as an unsigned value.
int value = x.getMyByte() & 0xff;
You can still just store a byte - and then interpret it in an unsigned way rather than a signed way. If you really just want to store a single byte - 8 bits - I suggest you don't change to using a short.
Having said all this, it's somewhat odd to need a mutable wrapper type just for a byte... perhaps you could give us more context as to why you want this, and we may be able to suggest cleaner alternatives.
Your String "FF" seems to be a hex value, right? So you actually want to convert it to the byte value of 255.
You can use a method like described here to convert a hex string to a byte array: Convert a string representation of a hex dump to a byte array using Java?.
In your case, you could adapt that method to expect a 2-letter string and return a single byte instead of a byte array.
String.getBytes return a bytearray so you can not assign a bytearray to a byte.
If you want to set FF hex value then you could use
setMyByte((byte)0xff);
But there is still a problem 0xff is of 2 byte size but byte type is of 1 byte. In that case you can use short instead of byte

android java - converting string to byte variable

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

Categories

Resources