Hex to ASCII conversion shows wrong result

+1 vote

I am using the following code to convert Hex to ASCII:

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();

}

Input:

hexToAscii("51d37bdd871c9e1f4d5541be67a6ab625e32028744d7d4609d0c37747b40cd2d");

Expected output:

QÓ{݇žMUA¾g¦«b^2‡D×Ô`7t{@Í-

Present output:

-Í@{t7?`Ô×D?2^b«¦g¾AUM??Ý{ÓQ. 

Basically, the output I am getting is the reverse of the output that is expected. Any solutions?

Aug 21, 2018 in Blockchain by slayer
• 29,370 points
1,896 views

2 answers to this question.

0 votes

Assuming your input string is in_str, I would use a method like this

public static byte[] decode(String in_str) {

  if (in_str != null) {

    in_str = in_str.trim();

    List<Byte> bytes = new ArrayList<Byte>();

    char[] chArr = in_str.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"))
answered Aug 21, 2018 by digger
• 26,740 points
0 votes

Hey, I use the following code:

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);
}
answered Aug 21, 2018 by Omkar
• 69,230 points

Related Questions In Blockchain

0 votes
1 answer

How to convert byte array to hex string?

You are missing the padding in the ...READ MORE

answered Aug 17, 2018 in Blockchain by slayer
• 29,370 points
2,966 views
0 votes
1 answer

Javascript mBTC to BTC conversion not working

At the beginning of bitcoinConversion() you have: if (currentUnit != ...READ MORE

answered Sep 5, 2018 in Blockchain by slayer
• 29,370 points
997 views
0 votes
1 answer

How to set the hex-encoded data field in a Web3j Ethereum transaction?

You can use the "data" field of ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,230 points
2,965 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,335 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,885 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,401 views
0 votes
1 answer

Getting wrong result from SHA256 than what is expected

It's just an encoding issue - don't ...READ MORE

answered Sep 6, 2018 in Blockchain by digger
• 26,740 points
1,643 views
0 votes
3 answers

String conversion to Array in Solidity

There is no built-in method/function for this ...READ MORE

answered Oct 3, 2018 in Blockchain by Charlie
3,227 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP