byte[] 的字节码表示
ByteCode representation to byte[]
如果我这样做:
img.getImageBytes(); /*returns byte[] from image */
我怎样才能对表示该 byte[] 的字符串进行逆运算?
BufferedImage yourImage = ImageIO.read(new ByteArrayInputStream(bytes_array_of_image));
我不知道你是如何打印字节数组的。但假设您使用的是 toString()
方法或 Arrays.toString()
方法。
然后你使用 byte[]
的字符串表示形式,去掉前导 [
和后面的 ]
并将其按 ,
拆分为 String[]
.现在创建一个与 String[]
大小相同的新 byte[]
。然后迭代 String[]
并转换每个元素并添加到 byte[]
。范例
byte[] b = {1, 2, 3, 4, 5}; //Initial byte array also can be gotten from any other source
String s = b.toString(); //[1, 2, 3, 4, 5]
String[] sa = s.substring(1, s.length() - 1).split(",");
byte[] ba = new byte[sa.length];
for(int i = 0; i < ba.length; i++) {
ba[i] = Byte.valueOf(sa[i].trim());
}
//Now ba should contain 1, 2, 3, 4, 5
如果我这样做:
img.getImageBytes(); /*returns byte[] from image */
我怎样才能对表示该 byte[] 的字符串进行逆运算?
BufferedImage yourImage = ImageIO.read(new ByteArrayInputStream(bytes_array_of_image));
我不知道你是如何打印字节数组的。但假设您使用的是 toString()
方法或 Arrays.toString()
方法。
然后你使用 byte[]
的字符串表示形式,去掉前导 [
和后面的 ]
并将其按 ,
拆分为 String[]
.现在创建一个与 String[]
大小相同的新 byte[]
。然后迭代 String[]
并转换每个元素并添加到 byte[]
。范例
byte[] b = {1, 2, 3, 4, 5}; //Initial byte array also can be gotten from any other source
String s = b.toString(); //[1, 2, 3, 4, 5]
String[] sa = s.substring(1, s.length() - 1).split(",");
byte[] ba = new byte[sa.length];
for(int i = 0; i < ba.length; i++) {
ba[i] = Byte.valueOf(sa[i].trim());
}
//Now ba should contain 1, 2, 3, 4, 5