0

I'm receiving a JSON object that has a byte array in it in a string format because of limitations of my system. It's only for testing something.

I get the string in question "[ 1, 2, 3, 4]" for example in android and want to transform it so I end up with byte[] data = [ 1, 2, 3, 4] .

Using getBytes obviously doesn't return what I'm looking for and I'm kinda clueless as to what can be done here. Help would be greatly appreciated!

Edit : I just parsed the string to get the each integer in an array of ints. Is there a way to put that into a byte array easily?

Thanks

5
  • **I'm receiving a JSON object that has a byte array in it **, please specify the structure of your json format in code, and since we are talking android here, hope you are familiar with org.json package in android that helps in Json parsing? More clarification would produce a better and cleaner answer for you Commented Feb 14, 2017 at 20:08
  • Hello, the JSON portion is not really what I'm having trouble with. Unless you know of a way for JSON to send a byte array. My question is really just about transforming a string that has the format of a byte array like [1,2,3,4] to the actual byte array [1,2,3,4] Commented Feb 14, 2017 at 20:12
  • so in other words, what you mean is you have something like, {"strNotBytes": [ "1" , "2" , "3" , "4" ] } , and you expect to convert this into byte[] array or any Byte output stream, if I am not wrong? Commented Feb 14, 2017 at 20:19
  • Not exactly, I'm receiving {"strNotBytes": "[ 1 , 2 ,3 , 4 ]" } (note the quote position, not sure it changes something) and I want the byte[] array to be filled with [ 1 , 2 , 3 , 4 ] . I already know the JSON portion is right, I'm recovering {"strNotBytes": "[ 1,2,3,4]" }and putting it in a string just fine. Is that doable? Or is there a way to make the JSON sent already be bytes directly? Commented Feb 14, 2017 at 20:25
  • Answer given by Dave Friedman would be worth noting if, I already know the JSON portion is right, I'm recovering {"strNotBytes": "[ 1,2,3,4]" }and putting it in a string just fine means converting String someString = "[1,2,3,4]" , to byte[] array Commented Feb 14, 2017 at 20:31

1 Answer 1

2

Try this:

private static byte[] doConvert(String inStr) {
    String opStr = inStr.substring(1);
    opStr = opStr.substring(0, opStr.length()-1);
    int val;
    String[] arr = opStr.split("[,]");
    byte[] retVal = new byte[arr.length];
    for (int i=0; i<arr.length; i++) {
        opStr = arr[i].trim();
        try {
            val = Integer.parseInt(opStr);
        } catch (Exception e) {
            System.out.println("bad input at item #"+i+": "+opStr);
            return null;
        }
        if (val > 255) {
            System.out.println("bad input value at item #"+i+": "+val+" > 255");
            return null;                
        }
        retVal[i] = (byte) val;
    }
    return retVal;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, this definitely got me closer to what I need. I was already doing something similiar but transforming string to integer using Integer.valueOf() instead of parseInt. Maybe that was causing problems. A problem I'm getting now is if I'm getting the string [2,182,28,82,251,154,70,149] when I transform it to bytes I'm getting [ 2, -74, 28,82,-5,-102,70,-107]. I'm guessing this has to do with signed vs unsigned bytes. Is there a way to transform this? Thanks a lot for the help so far!
Never mind actually, since I only need the bits to be at the right value to transform them later I don't really care if it's using them as signed or unsigned so thanks for your help!

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.