将字符串转换为字节数组问题是字符串未转换为字节数组格式

时间:2018-12-27 09:51:48

标签: java android

我想转换作为json值获取的字符串值。我想将此字符串转换为字节数组,但问题是如果我将其转换为字节数组,它将以ascii格式显示。

代码

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

Log.e(TAG,“ passenger_sign:” + passenger_sign);

输出

[-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

这就是我作为json值得到的...它现在是字符串格式。 如果我将其转换为字节数组格式,则以ascii格式显示输出。

这是我将字符串转换为字节数组的方式:

byte[] bytes = passenger_sign.getBytes();

输出

[B@37b5539

我想将字符串转换为bytearray并将其设置为bitmapfactory。请任何人帮我解决这个问题。

3 个答案:

答案 0 :(得分:1)

尝试以下方法:

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

答案 1 :(得分:0)

当您转换并打印出byte []时,byte []。toString()返回byte []的哈希码,而不是您希望如何看待它。尝试打印出字节[0],字节[1]。它应该与您期望的相同。

尝试运行以下代码

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

您应该获得

的输出
WOW...
[87, 79, 87, 46, 46, 46]

答案 2 :(得分: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