+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 1,223 views

## 2 answers to this question.

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

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"))```
• 26,740 points

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);
}```
• 69,190 points

## Javascript mBTC to BTC conversion not working

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

## How to decrypt result of query when using the Hyperledger Client SDK for Node.js

In this case, it is just a ...READ MORE

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

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

+1 vote

## How to perform a simple 1-to-1 Bitcoin Transaction with Bitcore?

What is the argument utxos int the ...READ MORE

+1 vote

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

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

## Truffle tests not running after truffle init

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

## Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

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