這是我正在嘗試做的。我有一個字節[],我需要在Redis中使用key存儲(比如key1),Redis會將其存儲爲String。我需要在經由KEY1字節[]再次字符串到字節[]
//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] < < readStr >> [ - 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] < <
我在做什麼錯?有沒有更好的解決方案?
謝謝,它解決了這個問題。 – scs075 2013-03-01 18:01:43
找到正確的方法:String storeStr = Base64.encodeBase64String(bArr); – scs075 2013-03-14 20:18:54
爲什麼停在B64? Redis是二進制安全的,因此它可以處理任何非打印的ASCII字符。那麼,爲什麼在使用256時爲每個字符使用64個可能的狀態呢?使用1/4的空間。 – 2015-03-25 15:25:33