[英]How can I convert a Base64 string to a float array or int array?
I have some code that converts a float[]
to a Base64
string: 我有一些代码将
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);
I also have - basically - the same code that converts an int[]
to a Base64
string: 我基本上也有将
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);
Both of these produce Base64
strings as expected. 两者均产生预期的
Base64
字符串。 However, now I need to decode back to an array, and I'm running into trouble. 但是,现在我需要解码回数组,并且遇到了麻烦。
How can I go from my Base64
string(s), and get the original data array(s). 如何从我的
Base64
字符串中获取原始数据数组。 Before I decode the Base64
string, I will know if it is suppose to be an int[]
or float[]
, so I think that will help. 在解码
Base64
字符串之前,我会知道它应该是int[]
还是float[]
,所以我认为这会有所帮助。
Does anyone know how to do go from Base64
string to float[]
or int[]
? 有谁知道该怎么办从
Base64
字符串到float[]
或int[]
吗?
You can use BitConverter.ToInt32
or BitConverter.ToSingle
to convert part of an array: 您可以使用
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);
}
(And the equivalent for ToSingle
, of course.) (当然,与
ToSingle
等效)。
In my view, it's a shame that GetBytes
doesn't have an overload to write the bytes directly into an existing array, instead of creating a new array on each call... 在我看来,很遗憾
GetBytes
没有将字节直接写到现有数组中的重载,而不是在每次调用时都创建一个新数组。
Convert.FromBase64String
有什么问题吗?
byte[] i_feat = Convert.FromBase64String(i_sig)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.