A UUID in the form of "b2f0da40ec2c11e00000242d50cf1fbf" has been transformed (see the following code segment) into a hex string as 6232663064613430656332633131653030303030323432643530636631666266. I want to code a reverse routine and get it back to the original format as in "b2f0...", but had a hard time to do so, any help?
byte[] bytes = uuid.getBytes("UTF-8");
StringBuilder hex = new StringBuilder(bytes.length* 2);
Formatter fmt = new Formatter(hex);
for (byte b : bytes)
fmt.format("%x", b);
final String input = "6232663064613430656332633131653030303030323432643530636631666266";
System.out.println("input: " + input);
final StringBuilder result = new StringBuilder();
for (int i = 0; i < input.length(); i += 2) {
final String code = input.substring(i, i + 2);
final int code2 = Integer.parseInt(code, 16);
result.append((char)code2);
}
System.out.println("result: " + result);
It prints:
input: 6232663064613430656332633131653030303030323432643530636631666266
result: b2f0da40ec2c11e00000242d50cf1fbf
Here you go:
import java.util.Formatter;
class Test {
public static void main(String[] args) {
String uuid = "b2f0da40ec2c11e00000242d50cf1fbf";
byte[] bytes = uuid.getBytes();
StringBuilder hex = new StringBuilder(bytes.length * 2);
Formatter fmt = new Formatter(hex);
for (byte b : bytes) {
fmt.format("%x", b);
}
System.out.println(hex);
/******** reverse the process *******/
/**
* group the bytes in couples
* convert them to integers (base16)
* and store them as bytes
*/
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
}
/**
* build a string from the bytes
*/
String original = new String(bytes);
System.out.println(original);
}
}
Related
How to convert hex string to ansi (window 1252) and ansi (window 1252)to hex string in Java.
python (Works perfectly)
q = "hex string value"
x = bytes.fromhex(q).decode('ANSI')
a = x.encode("ANSI")
a = a.hex()
if q==a:
print("Correct")
Java (This code has a problem)
String hexOri = "hex string value";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hexOri.length(); i+=2) {
String str = hexOri.substring(i, i+2);
output.append((char)Integer.parseInt(str, 16));
}
System.out.println("ANSI = " + output);
char [] chars = output.toString().toCharArray();
StringBuffer hexOutput = new StringBuffer();
for(int i = 0; i < chars.length; i++){
hexOutput.append(Integer.toHexString((int)chars[i]));
}
System.out.println("HexOutput = " + hexOutput.toString());
System.out.println(hexOri.equals(hexOutput.toString()));
Output from Python
Correct
Expected Output from Python
Correct
Output from Java
False
Expected Output from Java
Correct
In java the strings are encoded in UTF-16, so you can't read simply read/write the bytes of a string to get the encoding representation you desire.
You should use String#getBytes(String str, String charset) to get the string converted in the encoding you need and serialized to a byte array.
The same thing must be done to decode a byte array, using new String(buffer,encoding).
In both cases if you use the method without the charset it will use the default encoding for the JVM instance (which should be the system charset).
public static void main(String[] args) {
String str = "\tSome text [à]";
try {
System.out.println(str); // Some text [à]
String windowsLatin1 = "Cp1252";
String hexString = toHex(windowsLatin1, str);
System.out.println(hexString); // 09536f6d652074657874205be05d
String winString = toString(windowsLatin1, hexString);
System.out.println(winString); // Some text [à]
} catch (UnsupportedEncodingException e) {
// Should not happen.
}
}
public static String toString(String encoding, String hexString) throws UnsupportedEncodingException {
int length = hexString.length();
byte [] buffer = new byte[length/2];
for (int i = 0; i < length ; i+=2) {
String hexVal = hexString.substring(i,i+2);
byte code = (byte) Integer.parseInt(hexVal,16);
buffer[i/2]=code;
}
String winString = new String(buffer,encoding);
return winString;
}
public static String toHex(String encoding, String str) throws UnsupportedEncodingException {
byte[] bytes = str.getBytes(encoding);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
String hexChar = Integer.toHexString(b & 0xff);
if(hexChar.length()<2) {
builder.append('0');
}
builder.append(hexChar);
}
String hexString = builder.toString(); // 09536f6d652074657874205be05d
return hexString;
}
I have converted regional language word to hex value and saved to DB. But How can i decode that hexa value back to regional language word.
Here is my Kannada/Telugu word to Hex value conversion
public String toHex(String b){
String s="";
for (int i=0; i<b.length(); ++i) s+=String.format("%04X",b.charAt(i)&0xffff);
System.out.println("Converted value:::"+s); //0C1C0C3E0C350C3E
return s;
}
Word i have saved is జావా
Hex value saved in database is 0C1C0C3E0C350C3E
Decoded output am getting is : >5>
Is there any way to decode the hex value back to జావా
Code used to decode is
byte[] bytes = DatatypeConverter.parseHexBinary(itemName);
String s= new String(bytes, "UTF-8");
System.out.println("Utf..."+s);
Please help...
public String fromHex(String b) {
char[] cs = new char[b.length() / 4];
for (int i = 0; i < cs.length; ++i) {
int c = Integer.parseInt(b.substring(4 * i, 4 * i + 4), 16) & 0xFFFF;
cs[i] = (char) c;
}
return new String(cs);
}
This assumes that the conversion did not meddle with negative hex values.
Or exploiting that char is UTF-16BE:
byte[] bytes = DatatypeConverter.parseHexBinary(itemName);
return new String(bytes, StandardCharsets.UTF_16);
char[] data = hexData.toCharArray();
byte[] bytes = new byte[data.length/2];
for (int i = 0; i < data.length; i += 2) {
String val = new String(data, i, 2);
bytes[i/2] = Integer.valueOf(val, 16).byteValue();
}
String text = new String(bytes, "UTF8");
You might add sanity checks, e.g. that the length of the input-array is even, etc.
I have the following 3 byte encoded Base64 string.
String base64_str = "MDQw";
System.out.println("base64:" + base64_str);
String hex = DatatypeConverter.printHexBinary(DatatypeConverter.parseBase64Binary(base64_str));
for (int i = 0; i < hex.length(); i+=6) {
String bytes = hex.substring(i, i+6);
System.out.println("hex: " + bytes);
StringBuilder binary = new StringBuilder();
int byte3_int = Integer.parseInt(bytes.substring(4, 6), 16);
String byte3_str = Integer.toBinaryString(byte3_int);
byte3_int = Integer.valueOf(byte3_str);
binary.append(String.format("%08d", byte3_int));
int byte2_int = Integer.parseInt(bytes.substring(2, 4), 16);
String byte2_str = Integer.toBinaryString(byte2_int);
byte2_int = Integer.valueOf(byte2_str);
binary.append(String.format("%08d", byte2_int));
int byte1_int = Integer.parseInt(bytes.substring(0, 2), 16);
String byte1_str = Integer.toBinaryString(byte1_int);
byte1_int = Integer.valueOf(byte1_str);
binary.append(String.format("%08d", byte1_int));
System.out.println("binary: " + binary);
}
}
My Output is:
base64:MDQw
hex: 303430
binary: 001100000011010000110000
The above output is correct, but is there a more efficient way on converting a base64 string to binary string?
Thanks in advance.
You can use BigInteger (import java.math.BigInteger;) to convert a base64 string to binary string.
byte[] decode = Base64.getDecoder().decode(base64_str);
String binaryStr = new BigInteger(1, decode).toString(2);
Here is a small code to perform your operation. The only flaw is the use of replace for padding the 0.
import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
public class main {
public static void main(String [] args) throws UnsupportedEncodingException {
String base64_str = "MDQw";
byte[] decode = DatatypeConverter.parseBase64Binary(base64_str);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < decode.length; i++){
String temp = Integer.toBinaryString(decode[i]);
sb.append(String.format("%8s", temp).replace(" ", "0"));
}
System.out.println(sb.toString());
}
}
Hi Team,Firstly I don't want a byte[] array made from the actual String/char[]
//NO!
String s = "abc";
byte[] bytes = s.getBytes();
I want a byte[] array constructed by the contents and representation of the String, like so.
byte[] b = "new byte[]{1,2,3}"
//Again I don't want >> byte[] b = new String("new byte[]{1,2,3}").getBytes();
thanks Team.
This worked for me -
/**
* Parse a properly formatted String into a byte array.
*
* #param in
* The string to parse - must be formatted
* "new byte[]{1,2,n}"
* #return The byte array parsed from the input string.
*/
public static byte[] parseByteArrayFromString(
String in) {
in = (in != null) ? in.trim() : "";
// Opening stanza.
if (in.startsWith("new byte[]{")) {
// Correct closing brace?
if (in.endsWith("}")) {
// substring the input.
in = in.substring(11, in.length() - 1);
// Create a list of Byte(s).
List<Byte> al = new ArrayList<Byte>();
// Tokenize the input.
StringTokenizer st = new StringTokenizer(in,
",");
while (st.hasMoreTokens()) {
String token = st.nextToken();
// Add a Byte.
al.add(Byte.valueOf(token.trim()));
}
// Convert from the List to an Array.
byte[] ret = new byte[al.size()];
for (int i = 0; i < ret.length; i++) {
ret[i] = al.get(i);
}
return ret;
}
}
return new byte[] {};
}
public static void main(String[] args) {
byte[] vals = parseByteArrayFromString("new byte[]{1,2,3}");
System.out.println(Arrays.toString(vals));
}
Well, you could always just traverse through the array and put those values in a string, then put those in a byte array.
String d = "new byte[]{";
for(int i = 0; i < s.length() - 1; i++)
d += s.charAt(i) +",";
d += s.charAt(s.length() - 1) + "}";
byte[] b = d.getBytes();
You can extract bytes by using regular expression, such as:
Pattern pattern = Pattern.compile("(\\d+)");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
Byte.parseByte(matcher.group(0)).byteValue(); // Use this
}
In the while loop, use can add them to an array to use it later or print it to console, or any else. It's up to you.
For sure that input string is correct, add another pattern to check that string if necessary. For example:
Pattern.compile("new byte\\[\\] ?\\{((\\d+),? *)+\\}");
I needed a method that would convert hex to ascii, and most seem to be a variation of the following:
public String hexToAscii(String hex) {
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
for(int i = 0; i < hex.length() - 1; i += 2){
String output = hex.substring(i, (i + 2));
int decimal = Integer.parseInt(output, 16);
sb.append((char)decimal);
temp.append(decimal);
}
return sb.toString();
}
The idea is to look at
hexToAscii("51d37bdd871c9e1f4d5541be67a6ab625e32028744d7d4609d0c37747b40cd2d");
If I print the result out, I get
-Í#{t7?`Ô×D?2^b«¦g¾AUM??Ý{ÓQ.
This is not the result I am needing though. A friend got the correct result in PHP which was the string reverse of the following:
QÓ{݇žMUA¾g¦«b^2‡D×Ô`7t{#Í-
There are clearly characters that his hexToAscii function is encoding whereas mine is not.
Not really sure why this is the case, but how can I implement this version in Java?
Assuming your input string is in, I would use a method like this
public static byte[] decode(String in) {
if (in != null) {
in = in.trim();
List<Byte> bytes = new ArrayList<Byte>();
char[] chArr = in.toCharArray();
int t = 0;
while (t + 1 < chArr.length) {
String token = "" + chArr[t] + chArr[t + 1];
// This subtracts 128 from the byte value.
int b = Byte.MIN_VALUE
+ Integer.valueOf(token, 16);
bytes.add((byte) b);
t += 2;
}
byte[] out = new byte[bytes.size()];
for (int i = 0; i < bytes.size(); ++i) {
out[i] = bytes.get(i);
}
return out;
}
return new byte[] {};
}
And then you could use it like this
new String(decode("51d37bdd871c9e1f4d5541be67a6ab625e"
+"32028744d7d4609d0c37747b40cd2d"))
How about trying like this:
public static void main(String[] args) {
String hex = "51d37bdd871c9e1f4d5541be67a6ab625e32028744d7d4609d0c37747b40cd2d";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
String str = hex.substring(i, i+2);
output.append((char)Integer.parseInt(str, 16));
}
System.out.println(output);
}