How to regain the hex value of a byte in java? - java

private static byte[]theTestText ={0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,(byte) 0x88,(byte) 0x99,(byte) 0xAA,(byte) 0xBB,(byte) 0xCC,(byte) 0xDD,(byte) 0xEE,(byte) 0xFF};
String str= new String(theTestText); // converted the byte array to string
char ch = str.charAt(8); // extracting a specific character from string
I want to obtain the hex value from the character 'ch' as defined in the original byte array i.e. 0x88. Is there any way to get that?

To get the hexa value in a string:
String str= new String(theTestText); // converted the byte array to string
char ch = str.charAt(8); // extracting a specific character from string
String hex = String.format("%04x", (int) ch); // Converting the char to a string with its hexadecimal value

Related

Division of String

I want to know how to divide a String and seperate it into two variables, one as char and other as integer
Example:
If "C 365" Is my string then char variable will be 'C' and Integer variable will be 365
The char can be extracted with charAt. For the int, just use substring to take the string from the third character (the first is the char and the second is a space), and then parse it:
String str = "C 365";
char ch = str.charAt(0);
int i = Integer.parseInt(str.substring(2));

Convert numeric String to its ASCII number

I have a String containing an ASCII digit character. I'd like to convert it to its ASCII-code as an int, so the charAt-method does not work directly here.
E.g. I have String number = "4";, how do I get its ASCII code (52) into an int?
You need the typecast the character to integer.
String name = "a";
char character = name.charAt(0);
int ascii = (int) character;

Covert UTF-16 Character Code (number) to String in Java

How do I simply convert a UTF-16 character to String?
something Like
String str = TheMagicalFunction(0x25E6);
You can use Character.toString(char):
String str = Character.toString((char) 0x25E6);
You can omit the cast when first storing the character in a variable …
char whiteBullet = 0x25E6;
String whiteBulletString = Character.toString(whiteBullet);
… or when using a Unicode escape which in this case is easy since the character belongs to the Basic Multilingual Plane (BMP):
String str = Character.toString('\u25E6');
The method String.valueOf(char) is equivalent and/but has multiple overloads. Beware of this:
String str = String.valueOf(0x25E6); // "9702" (decimal value)
String str2 = String.valueOf((char) 0x25E6); // "◦"
String str3 = String.valueOf('\u25E6'); // "◦"
You need to:
If the character is an integer, cast it to char.
Put it in a 1-element char[].
Pass it the the String constructor.
So,
String str = new String(new char[] {(char) 0x25E6});

Java: Convert a hexadecimal encoded String to a hexadecimal byte

An Item-ID in hexadecimal and the amount in decimal has to be entered in two JTextFields.
Now I have to convert the Item ID hexadecimal encoded in a String to a byte hexadecimal.
String str = itemIdField.getText(); // Would be, for example, "5e"
byte b = // Should be 0x5e then.
So if str = "5e", b = 0x5e
if str = "6b" b = 0x6b and so on.
Does anybody now, what the code to convert that would be then?
Google doesn't know, it thinks, I want to convert the text to a byte[]
Thank you, Richie
You can use Byte.parseByte(str, 16), that will return the byte value represented by the hexadecimal value in str.

How to convert hex strings to byte values in Java

I have a String array.
I want to convert it to byte array.
I use the Java program.
For example:
String str[] = {"aa", "55"};
convert to:
byte new[] = {(byte)0xaa, (byte)0x55};
What can I do?
String str = "Your string";
byte[] array = str.getBytes();
Looking at the sample I guess you mean that a string array is actually an array of HEX representation of bytes, don't you?
If yes, then for each string item I would do the following:
check that a string consists only of 2 characters
these chars are in '0'..'9' or 'a'..'f' interval (take their case into account
as well)
convert each character to a corresponding number, subtracting code value of '0' or 'a'
build a byte value, where first char is higher bits and second char is lower ones. E.g.
int byteVal = (firstCharNumber << 4) | secondCharNumber;
Convert string to Byte-Array:
byte[] theByteArray = stringToConvert.getBytes();
Convert String to Byte:
String str = "aa";
byte b = Byte.valueOf(str);
You can try something similar to this :
String s = "65";
byte value = Byte.valueOf(s);
Use the Byte.ValueOf() method for all the elements in the String array to convert them into Byte values.
A long way to go :). I am not aware of methods to get rid of long for statements
ArrayList<Byte> bList = new ArrayList<Byte>();
for(String ss : str) {
byte[] bArr = ss.getBytes();
for(Byte b : bArr) {
bList.add(b);
}
}
//if you still need an array
byte[] bArr = new byte[bList.size()];
for(int i=0; i<bList.size(); i++) {
bArr[i] = bList.get(i);
}
Since there was no answer for hex string to single byte conversion, here is mine:
private static byte hexStringToByte(String data) {
return (byte) ((Character.digit(data.charAt(0), 16) << 4)
| Character.digit(data.charAt(1), 16));
}
Sample usage:
hexStringToByte("aa"); // 170
hexStringToByte("ff"); // 255
hexStringToByte("10"); // 16
Or you can also try the Integer.parseInt(String number, int radix) imo, is way better than others.
// first parameter is a number represented in string
// second is the radix or the base number system to be use
Integer.parseInt("de", 16); // 222
Integer.parseInt("ad", 16); // 173
Integer.parseInt("c9", 16); // 201
String source = "testString";
byte[] byteArray = source.getBytes(encoding);
You can foreach and do the same with all the strings in the array.
The simplest way (using Apache Common Codec):
byte[] bytes = Hex.decodeHex(str.toCharArray());
String str[] = {"aa", "55"};
byte b[] = new byte[str.length];
for (int i = 0; i < str.length; i++) {
b[i] = (byte) Integer.parseInt(str[i], 16);
}
Integer.parseInt(string, radix) converts a string into an integer, the radix paramter specifies the numeral system.
Use a radix of 16 if the string represents a hexadecimal number.
Use a radix of 2 if the string represents a binary number.
Use a radix of 10 (or omit the radix paramter) if the string represents a decimal number.
For further details check the Java docs: https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int)
Here, if you are converting string into byte[].There is a utility code :
String[] str = result.replaceAll("\\[", "").replaceAll("\\]","").split(", ");
byte[] dataCopy = new byte[str.length] ;
int i=0;
for(String s:str ) {
dataCopy[i]=Byte.valueOf(s);
i++;
}
return dataCopy;

Categories

Resources