2

In an android app calling Ethereum contract method, I have a function which returns me a HEX value in return (Ethereum Contract ABI), in this case the string is yoooooooo and HEX is:

 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000009796f6f6f6f6f6f6f6f0000000000000000000000000000000000000000000000

enter image description here

The contract API adds padding to the HEX as explained in the link. how do i get the original value from HEX value above without padding?

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
ke3pup
  • 1,835
  • 4
  • 36
  • 66
  • is that hex represented as a `String` or a `BigInteger`? – Debosmit Ray Apr 05 '16 at 03:59
  • @DebosmitRay added a screenshot of response data object . `String` it seems. – ke3pup Apr 05 '16 at 04:02
  • @3kings I don't know if OP checked that out. But that post really doesn't have anything good using standard libs (`guava` solution is cool). – Debosmit Ray Apr 05 '16 at 04:17
  • @JohnYounan Hey. I am trying to develop a wallet app for ethereum for my studies at the university. Could you please let me know, How you got it working ? I am not finding any light clients for android. Any directions please. – jgm Aug 01 '16 at 12:17

3 Answers3

4

Please try this one

String hex = "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000009796f6f6f6f6f6f6f6f0000000000000000000000000000000000000000000000";
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.toString().trim());
anas p a
  • 383
  • 5
  • 20
  • tried it , it gives `�������������������������������������������������������������� �������������������������������������������������������������� yoooooooo����������������������������������������������` . possible to get just the string? – ke3pup Apr 05 '16 at 04:12
  • Please look in to the answer edit – anas p a Apr 05 '16 at 04:15
2

try this one.

public static String convertHexToStringValue(String hex) {
    StringBuilder stringbuilder = new StringBuilder();
    char[] hexData = hex.toCharArray();
    for (int count = 0; count < hexData.length - 1; count += 2) {
        int firstDigit = Character.digit(hexData[count], 16);
        int lastDigit = Character.digit(hexData[count + 1], 16);
        int decimal = firstDigit * 16 + lastDigit;
        stringbuilder.append((char)decimal);
    }
    return stringbuilder.toString();
}
Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
Vishwa
  • 1,112
  • 1
  • 11
  • 23
-1

(I think) there are possible multiple ways of approaching this. I will list here an intuitive way of approaching a problem like this.

You can go from String (hex) to byte array to String as UTF-8(?). Make sure your hex string does not have leading spaces and stuff.

public static byte[] hexStringToByteArray(String hex) {
    int l = hex.length();
    byte[] data = new byte[l/2];
    for (int i = 0; i < l; i += 2) {
        data[i/2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                             + Character.digit(hex.charAt(i+1), 16));
    }
    return data;
}

Usage:

String b = "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000009796f6f6f6f6f6f6f6f0000000000000000000000000000000000000000000000";
byte[] bytes = hexStringToByteArray(b);
String st = new String(bytes,StandardCharsets.UTF_8);
System.out.println(st);

Gives:

? yoooooooo

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
  • I will need to find a way to fix that leading `?`. I will update when I do. If you find the way first, please let me know. – Debosmit Ray Apr 05 '16 at 04:15
  • I'll keep it open til there is an answer with just the string value but thanks for your help @Debosmit Ray – ke3pup Apr 05 '16 at 04:19
  • @JohnYounan I think it is something to do with padding in the returned data. You know what function you have called, and [this link](https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#use-of-dynamic-types) should be able to tell you how to parse the hex data. Based on your post, the `?` **should** be there due to the presence of `0x2` in the initial part of the string. So, you would need to either provide more info about how the data should be read, because clearly, there is a bunch of offset in this string. – Debosmit Ray Apr 05 '16 at 04:27
  • @DebosmitRay would you please suggest that leading "?" you find any solution for that?? – Asmi Jul 11 '18 at 07:18