我有以下C#代碼將我的數組數組轉換爲字節數組,然後將其保存爲base64字符串,反之亦然,但它不適用於長因爲長是8字節,我的代碼僅適用於4字節數字。將數組數組轉換爲字節,反之亦然C#
private static int _endianDiff1;
private static int _endianDiff2;
private static int _idx;
private static byte[] _byteBlock;
enum ArrayType { Float, Int32, UInt32, Int64, UInt64 }
public static bool SetIntArray(string key, int[] intArray)
{
return SetValue(key, intArray, ArrayType.Int32, 1, ConvertFromInt);
}
public static bool SetLongArray(string key, long[] longArray)
{
return SetValue(key, longArray, ArrayType.Int64, 1, ConvertFromLong);
}
private static bool SetValue<T>(string key, T array, ArrayType arrayType, int vectorNumber, Action<T, byte[], int> convert) where T : IList
{
var bytes = new byte[(4 * array.Count) * vectorNumber + 1];
bytes[0] = Convert.ToByte(arrayType); // Identifier
Initialize();
for (var i = 0; i < array.Count; i++)
{
convert(array, bytes, i);
}
return SaveBytes(key, bytes);
}
private static void ConvertFromInt(int[] array, byte[] bytes, int i)
{
ConvertInt32ToBytes(array[i], bytes);
}
private static void ConvertFromLong(long[] array, byte[] bytes, int i)
{
ConvertInt64ToBytes(array[i], bytes);
}
public static int[] GetIntArray(string key)
{
var intList = new List<int>();
GetValue(key, intList, ArrayType.Int32, 1, ConvertToInt);
return intList.ToArray();
}
public static long[] GetLongArray(string key)
{
var longList = new List<long>();
GetValue(key, longList, ArrayType.Int64, 1, ConvertToLong);
return longList.ToArray();
}
private static void GetValue<T>(string key, T list, ArrayType arrayType, int vectorNumber, Action<T, byte[]> convert) where T : IList
{
if (!PlayerPrefs.HasKey(key))
return;
var bytes = Convert.FromBase64String(PlayerPrefs.GetString(key));
if ((bytes.Length - 1) % (vectorNumber * 4) != 0)
{
Debug.LogError("Corrupt preference file for " + key);
return;
}
if ((ArrayType)bytes[0] != arrayType)
{
Debug.LogError(key + " is not a " + arrayType + " array");
return;
}
Initialize();
var end = (bytes.Length - 1)/(vectorNumber * 4);
for (var i = 0; i < end; i++)
{
convert(list, bytes);
}
}
private static void ConvertToInt(List<int> list, byte[] bytes)
{
list.Add(ConvertBytesToInt32(bytes));
}
private static void ConvertToLong(List<long> list, byte[] bytes)
{
list.Add(ConvertBytesToInt64(bytes));
}
private static void Initialize()
{
if (BitConverter.IsLittleEndian)
{
_endianDiff1 = 0;
_endianDiff2 = 0;
}
else
{
_endianDiff1 = 3;
_endianDiff2 = 1;
}
if (_byteBlock == null)
{
_byteBlock = new byte[4];
}
_idx = 1;
}
private static bool SaveBytes(string key, byte[] bytes)
{
try
{
PlayerPrefs.SetString(key, Convert.ToBase64String(bytes));
}
catch
{
return false;
}
return true;
}
private static void ConvertInt32ToBytes(int i, byte[] bytes)
{
_byteBlock = BitConverter.GetBytes(i);
ConvertTo4Bytes(bytes);
}
private static void ConvertInt64ToBytes(long i, byte[] bytes)
{
_byteBlock = BitConverter.GetBytes(i);
ConvertTo8Bytes(bytes);
}
private static int ConvertBytesToInt32(byte[] bytes)
{
ConvertFrom4Bytes(bytes);
return BitConverter.ToInt32(_byteBlock, 0);
}
private static long ConvertBytesToInt64(byte[] bytes)
{
ConvertFrom8Bytes(bytes);
return BitConverter.ToInt64(_byteBlock, 0);
}
private static void ConvertTo4Bytes(byte[] bytes)
{
bytes[_idx] = _byteBlock[_endianDiff1];
bytes[_idx + 1] = _byteBlock[1 + _endianDiff2];
bytes[_idx + 2] = _byteBlock[2 - _endianDiff2];
bytes[_idx + 3] = _byteBlock[3 - _endianDiff1];
_idx += 4;
}
private static void ConvertFrom4Bytes(byte[] bytes)
{
_byteBlock[_endianDiff1] = bytes[_idx];
_byteBlock[1 + _endianDiff2] = bytes[_idx + 1];
_byteBlock[2 - _endianDiff2] = bytes[_idx + 2];
_byteBlock[3 - _endianDiff1] = bytes[_idx + 3];
_idx += 4;
}
private static void ConvertTo8Bytes(byte[] bytes)
{
}
private static void ConvertFrom8Bytes(byte[] bytes)
{
}
到目前爲止,我有工作了INT,UINT和浮動因爲他們都是4字節和我的問題是這樣它的工作原理基礎上,通過型式尺寸改變我的初始化函數。
我想也應該有ConvertTo8Bytes和ConvertFrom8Bytes功能,我不知道該怎麼做,因爲我設置_endianDiff和_byteBlock只有4個字節。我知道_byteBlock應該有動態大小而不是4,但我不知道在這種情況下如何處理字節序。
在側面說明,我已經通過拆分長到2個INT S和剛剛將其存儲爲兩個INT陣列解決了這個問題,但我沒用分配內存喜歡這個只是因爲我不能使這個算法工作。
在[BitConverter類](https://msdn.microsoft.com/en-us/library/system.bitconverter(v = vs.110).aspx)上採取一些措施。 – Abion47
'string ConvertToBase64(IEnumerable list) { var bytes = list.Select(x => BitConverter.GetBytes(x))。SelectMany(x => x).ToArray(); return Convert.ToBase64String(bytes); }' –