-1

I have:

String key = "0x64, 0xC, -90, 0x77, 0x2B, -113, 0xD, 0x69, -111, 0x76, 0x11, 0x35, -68, -110, -106, -81"

It is already bytes array in hex which was saved in shared preferences. I just need to put like below.

byte[] input_key = new byte[]{ key }

Note that there are negative numbers.

Daimonium
  • 29
  • 8
  • You need to write a "mini parser" of sorts to do this. Good luck. --- But seriously, do you have an actual question? – byxor Jan 18 '17 at 13:55
  • 2
    Possible duplicate of [Convert a string representation of a hex dump to a byte array using Java?](http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java) – Manuel Spigolon Jan 18 '17 at 13:56
  • @BoristheSpider `Integer.valueOf(s, 16)` I doubt this will work. Look at the input: Base 10 and Hex is mixed. – Fildor Jan 18 '17 at 13:59
  • 1
    What I would do: 1. Split the String by "," into String[]. 2. Declare byte[] of same size. 3. Iterate String[] and for each element n with index i : Decide if it is Hex or Base10 and parse it accordingly writing the result to byte[] at index i. – Fildor Jan 18 '17 at 14:03
  • @BoristheSpider, it is a very hard form of code to understanding. AIDE wrote that something wrong with it. Sorry, but could you elaborate? And as mentioned below, it won't work with negative bytes. – Daimonium Jan 18 '17 at 14:25

1 Answers1

0

Leaving the actual parsing for you to find out, this is what I described in comment:

String[] sNums = key.split(","); // Mind that the elements will still contain whitespace!

byte[] bNums = new byte[sNums.length];
for( int i = 0; i < bNums.length; i++ )
{
    if( sNums[i].trim().startsWith("0x") )
    {
       bNums[i] = [insert hex parsing here]
    }
    else
    {
       bNums[i] = [insert decimal parsing here]
    }
}

Not very efficient nor elegant, but will probably work if parsing is done right.

Spoiler : Boris in fact already spoiled the Hex-Parsing (point your mouse on yellow field vv )

Integer.valueOf(s,16).byteValue()

Going from there, you surely can figure out decimal parsing.

Fildor
  • 14,510
  • 4
  • 35
  • 67