1

Thanks for your answer... I have 31 32 2E 30 31 33 6byte hexadecimal number. I want to convert 31 32 2E 30 31 33 this 6 byte hexadecimal number into 12.013 ASCII number in java.

1
  • Why do you think that these numbers are equal? For the first look your first (hex) number is much much bigger then the second one. Commented Apr 26, 2010 at 7:17

3 Answers 3

5

Something like this?

byte[] bytes = {0x31, 0x32, 0x2E, 0x30, 0x31, 0x33};
String result = new String(bytes, "ASCII");
System.out.println(result);
Sign up to request clarification or add additional context in comments.

Comments

0

Probably not the most elegant method but try this:

char[6] string = new char[6];
string[0] = 0x31;
string[1] = 0x32;
string[2] = 0x2E;
string[3] = 0x30;
string[4] = 0x31;
string[5] = 0x33;

String s = new String(string);

int result = Integer.parseInt(s);

Comments

0

Assuming that your input is an array of strings representing the digits in hex, you can do the following:

public static String convert(String[] hexDigits){
    byte[] bytes = new byte[hexDigits.length];

    for(int i=0;i<bytes.length;i++)
        bytes[i] = Integer.decode("0x"+hexDigits[i]).byteValue();

    String result;
    try {
        result = new String(bytes, "ASCII");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    return result;
}

Note that the code assumes that the digits are given as valid ASCII values, with no radix specifier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.