简体   繁体   English

如何将Base64字符串转换为float数组或int数组?

[英]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.ToInt32BitConverter.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.

相关问题 可以将byte []数组转换为Base64字符串吗? - Can a byte[] array ever not convert to a Base64 string? 如何将逐个字节数组将chunck转换为base64字符串? - How to convert chunck by chunck byte array into base64 string? 如何将字符串转换为base64字节数组,这是否有效? - How to convert string to base64 byte array, would this be valid? 如何将sbyte []转换为base64字符串? - How can I convert sbyte[] to base64 string? 我在前端有字符串base64数组。 我如何在后端发送此字符串数组? - I have string base64 array in frontend. How can i send backend side this string array? 将Java字节数组转换为C#base64字符串 - Convert java byte array to C# base64 string 如何将base64字节字符串转换为Float32或Float64? - How to convert a base64 string of bytes to Float32 or Float64? 如何在JavaScript中使用字节数组将字符串转换为base64编码? - How to convert a string to base64 encoding using byte array in JavaScript? 将base64字符串转换为字节数组,如C#方法Convert.FromBase64String - convert base64 string to byte array like C# method Convert.FromBase64String 在将表单发送到后端之前,如何将密码字符串转换为Base64字符串? - How can I convert password string to Base64 string before sending form to backend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM