【发布时间】:2013-02-15 07:13:45
【问题描述】:
这就是我想要做的。我有一个 byte[] 需要用键(比如 key1)存储在 Redis 中,Redis 会将其存储为字符串。我需要在通过 key1 检索值时重建 byte[]
//here is a byte array
byte[] bArr = new byte[] {83, 71, 86, 115, 98, 71, 56, 103, 84, 88, 73, 117, 73, 69, 104, 118, 100, 121, 66, 107, 98, 121, 66, 53, 98, 51, 85, 103, 90, 71, 56, 47}; //"Hello World";
// I will have to store this as a byte string into redis
//Base64 encoding
bArr = Base64.encodeBase64(bArr);
String storeStr = Arrays.toString(bArr) ;
// storeStr is what gets stored in redis
System.out.println("storeStr>>" + storeStr+ "<<");
// I will get this string back from redis
// now trying to reconstruct the byte[]
byte[] aArr = Base64.decodeBase64(storeStr);
System.out.println("readStr>>" + Arrays.toString(aArr)+ "<<");
但我得到以下输出:
storeStr>>[85, 48, 100, 87, 99, 50, 74, 72, 79, 71, 100, 85, 87, 69, 108、49、83、85、86、111、100、109、82、53、81、109、116、105、101、 85、73、49、89、106、78、86、90、49、112、72、79、67、56、61]>[-13, -98, 60, -41, 77, 60, -17, -33, 121, -45, -66, 59, -37, -65, 123, -41, 93, 52, -13、-97、59、-21、-35、116、-13、-113、124、-33、-50、124、-21、93、117、-41、77、53、-45、- 33、54、-25、127、53、-41、79、117、-41、-83、116、-25、93、53、-13、-98、-9、-29、-33、61、 -41、78、-69、-13、-50、-67、-45、-113、117、-41、110、-10、-17、-34、-69、-25、-82、-75 ]
我做错了什么?有没有更好的解决方案?
【问题讨论】: