[英]How can I convert a Base64 string to a float array or int array?
我有一些代碼將float[]
轉換為Base64
字符串:
float[] f_elements = <from elsewhere in my code>;
byte[] f_vfeat = f_elements.SelectMany(value => BitConverter.GetBytes(value)).ToArray();
string f_sig = Convert.ToBase64String(f_vfeat);
我基本上也有將int[]
轉換為Base64
字符串的相同代碼:
int[] i_elements = <from elsewhere in my code>;
byte[] i_feat = i_elements.SelectMany(value => BitConverter.GetBytes(value)).ToArray();
string i_sig = Convert.ToBase64String(i_feat);
兩者均產生預期的Base64
字符串。 但是,現在我需要解碼回數組,並且遇到了麻煩。
如何從我的Base64
字符串中獲取原始數據數組。 在解碼Base64
字符串之前,我會知道它應該是int[]
還是float[]
,所以我認為這會有所幫助。
有誰知道該怎么辦從Base64
字符串到float[]
或int[]
嗎?
您可以使用BitConverter.ToInt32
或BitConverter.ToSingle
轉換數組的一部分:
byte[] bytes = Convert.FromBase64String();
int[] ints = new int[bytes.Length / 4];
for (int i = 0; i < ints.Length; i++)
{
ints[i] = BitConverter.ToInt32(bytes, i * 4);
}
(當然,與ToSingle
等效)。
在我看來,很遺憾GetBytes
沒有將字節直接寫到現有數組中的重載,而不是在每次調用時都創建一個新數組。
Convert.FromBase64String
有什么問題嗎?
byte[] i_feat = Convert.FromBase64String(i_sig)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.