I have string and there is 0x80 in it. string presentation is : serialno� and hex presentation is 73 65 72 69 61 6C 6E 6F 80. I want to remove 0x80 from string without convert string to hex string. is it possible in java ? I tried lastIndexOf(0x80). but it returns -1.
my code is (also you can find on https://ideone.com/3p8wKT) :
public static void main(String[] args) {
String hexValue = "73657269616C6E6F80";
String binValue = hexStringToBin(hexValue);
System.out.println("binValue : " + binValue);
int index = binValue.lastIndexOf(0x80);
System.out.println("index : " + index);
}
public static String hexStringToBin(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
}
return new String(data);
}
new String(data);You have some bytes and they'll get translated into characters which could have a different value than the raw byte value.hexStringToBinmethod to return abyte[], not aString.