0

i want to convert string value that is get as json value.i want to convert this string to byte array but the problem is if i convert into bytearray it showing out in ascii format.

CODE

passenger_sign = assignedJobJson.getJSONObject(position).getString("passenger_sign");

Log.e(TAG, "passenger_sign: "+passenger_sign );

OUTPUT

[-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,2,-128,0,0,1,44,8,6,0,0,0,83,-5,2,43,0,0,0,4,115,66,73,84,8,8,8,8,124,8,100,-120,0,0,32,0,73,68,65,84,120,-100,-19,-35,119,124,20,117,-2,63,-16,-41,108,54,-101,77,33,100,67,-128,72,75,66,-17,1,-115,-107,34,-120,16,8,93,64,80,84,-70,-46,5,4,41,66,60,-123,-93,-120,-108,3,-63,66,40,-98,8,1,-60,59,77,65,56,79,74,-112,7,114,98,10,-100,119,-118,-102,13,-27,-108,-70,75,47,33,-103,-33,31,126,-15,71,-103,-39,-99,-103,-99,-19,-81,-25,-29,49,127,48,-27,-13,121,-49,102,-39,125,-17,103,62,69,16,69,81,4,17,17,17,17,5,13,-125,-73,3,32,34,34,34,34,-49,98,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,68,68,68,68,65,-122,9,32,17,17,17,81,-112,97,2,72,68,68,68,20,100,-104,0,18,17,17,17,5,25,38,-128,26,-39,-19,118,-40,-19,118,111,-121,65,68,68,68,-92,26,19,64,21,-84,86,43,122,-9,-18,13,-117,-59,114,-57,-42,-69,119,111,88,-83,86,111,-121,71,68,68,68,-92,-120,32,-118,-94,-24,-19,32,-4,-63,-70,117,-21,48,122,-12,104,92,-67,122,85,-14,120,76,76,12,-106,44,89,-126,-63,-125,7,123,54,48,34,34,34,34,-107,-104,0,42,-112,-99,-99,-115,-18,-35,-69,43,58,119,-25,-50,-99,104,-41,-82,-99,123,3,34,34,34,34,114,1,19,64,39,-20,118,59,42,87,-82,-116,-101,55,111,42,58,63,49,49,17,-7,-7,-7,-120,-119,-119,113,115,100,68,68,68,68,-38,-80,15,-96,19,-67,123,-9,86,-100,-4,1,-65,-9,19,-36,-75,107,-105,-5,2,34,34,34,34,114,17,19,64,7,-20,118,-69,-90,100,-82,-96,-96,64,-1,96,-120,-120,-120,-120,116,-62,4,-48,-127,-7,-13,-25,107,-70,-114,45,-128,68,68,68,-28,-53,-104,0,58,-16,-31,-121,31,106,-70,-82,69,-117,22,58,71,66,68,68,68,-92,31,38,-128,14,92,-68,120,81,-45,117,28,5,76,68,68,68,-66,-116,-93,-128,29,48,24,12,80,-5,-14,24,-115,70,-100,62,125,-102,-93,-128,-119,-120,-120,-56,103,-79,5,-48,1,45,-71,113,-97,62,125,-104,-4,17,17,17,-111,79,99,11,-96,-116,-44,-44,84,-20,-40,-79,67,-43,53,102,-77,89,118,-91,16,34,34,34,34,95,-63,22,64,9,69,69,69,-86,-109,63,0,-8,-17,127,-1,-21,-122,104,-120,-120,-120,-120,-12,-59,4,80,66,-113,30,61,84,95,-109,-105,-105,-121,-124,-124,4,55,68,67,68,68,68,-92,47,38,-128,119,89,-70,116,41,74,74,74,84,93,83,-67,122,117,-76,110,-35,-38,77,17,17,17,17,17,-23,-117,125,0,-17,98,50,-103,80,90,90,-86,-22,-102,78,-99,58,97,-5,-10,-19,110,-118,-120,-120,-120,-120,72,95,108,1,-68,77,106,106,-86,-22,-28,15,0,30,121,-28,17,55,68,67,68,68,68,-28,30,108,1,-4,63,69,69,69,72,78,78,-42,116,45,95,66,34,34,34,-14,39,76,0,-1,79,104,104,40,110,-34,-68,-87,-23,90,-66,-124,68,68,68,-28,79,-8,8,24,-64,-3,-9,-33,-81,57,-7,51,24,-8,18,18,17,17,-111,127,9,-6,-20,37,35,35,3,-7,-7,-7,-102,-81,-81,88,-79,-94,-114,-47,16,17,17,17,-71,95,80,63,2,-74,-37,-19,-120,-115,-115,117,-23,17,110,114,114,50,10,10,10,116,-116,-118,-120,-120,-120,-56,-67,-126,-70,5,-80,93,-69,118,14,-109,-65,90,-75,106,57,45,-93,125,-5

this is what i get as json value...its in a string format now. if i convert into byte array format its showing output in a ascii format.

this is the way i covert string to bytearray:

byte[] bytes = passenger_sign.getBytes();

OUTPUT

[B@37b5539

i want to convert string to bytearray and set it in bitmapfactory. pls anyone help me to get a solution for this.

  • Wouldn't you rather like the `WOW...` string this byte array encodes in ASCII ? – Aaron Dec 27 '18 at 09:55
  • it came in ascii format but i want the byte array format like "87, 79, 87, 46, 46, 46" – mohamed fazurullah Dec 27 '18 at 09:57
  • Study https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.htm to see how your existing code works. Then try using `getJSonArray()` and iterate its values, get as Json number and convert to byte. Or google JSonArray to byte array. – DodgyCodeException Dec 27 '18 at 16:06

3 Answers3

1

Try below:

byte[] byteArray = new byte[] {87, 79, 87, 46, 46, 46};
StringBuilder sb = new StringBuilder();
for (byte b : byteArray) {
  sb.append(b);
  sb.append(",");
}
sb.deleteCharAt(sb.length() - 1);
System.out.println(sb);
Aditya Narayan Dixit
  • 2,105
  • 11
  • 23
0

When you convert and print out byte[], byte[].toString() returns the hashcode of the byte[] and not how you would like to see it. Try printing out byte[0], byte[1]. It should be the same as what you are expecting.

Try running the below code

public static void main(String[] args) {
    byte[] byteArray = new byte[] {87, 79, 87, 46, 46, 46};
    String value = new String(byteArray);
    byte[] arr = value.getBytes();
    // System.out.println(arr) // this will return the hashcode for arr
    System.out.println(value);
    System.out.println(Arrays.toString(arr));
}

you should get the output of

WOW...
[87, 79, 87, 46, 46, 46]
mkjh
  • 1,634
  • 13
  • 25
  • 1
    A simpler way to print array use java.util.Arrays - Arrays.toString(arr) – Artem Botnev Dec 27 '18 at 10:08
  • @ArtemBotnev thanks for pointing that out – mkjh Dec 27 '18 at 10:10
  • i want to show image in image view...i get the byte array in Ascii format. suppose if i am using that Arrays.toString(arr) i get the proper byte array but i cant use this format in bitmap factory() this is my problem. Bitmap bitmap = BitmapFactory.decodeByteArray(data.toByteArray(), 0, bi.toByteArray().length); – mohamed fazurullah Dec 27 '18 at 10:15
0
Here is test example you can go for Converting String to Byte array
import java.util.Arrays;

public class Test3{
    public static void main(String[] afe) {
        String str = "Rohit";
        byte[] byteArr = str.getBytes();
        System.out.println("String to byte array: " + Arrays.toString(byteArr));
    }
}

output will be like - [ 82, 111, 104, 105, 116 ]
and this one for vice versa

import java.util.Arrays;

public class Test3{
    public static void main(String[] args) {
        byte[] byteArray1 = { 82, 111, 104, 105, 116 };

        String str1 = new String(byteArray1);

        System.out.println(str1);
    }
}

here output will be like string - Rohit
NPE
  • 429
  • 2
  • 16
  • thanks for yours reply, hope it will wokr for me. but if i show image in a bitmap format...i cant apply this Arrays.toString(byteArr) in bitmap factory method.it wont take this, do you have solution for this. – mohamed fazurullah Dec 27 '18 at 10:07
  • if you modify question with complete rquirement it will help to suggest answers – NPE Dec 27 '18 at 10:09