我有一个具有值的字符串数组
string[] words = {"0B", "00", " 00", "00", "00", "07", "3F", "14", "1D"};
我需要它转换成 ulong 数组
ulong[] words1;
我应该如何在 c# 中做到这一点
我想我应该添加一些背景。
字符串中的数据来自文本框,我需要在 hexUpDown.Value 参数中写入该文本框的内容。
我有一个具有值的字符串数组
string[] words = {"0B", "00", " 00", "00", "00", "07", "3F", "14", "1D"};
我需要它转换成 ulong 数组
ulong[] words1;
我应该如何在 c# 中做到这一点
我想我应该添加一些背景。
字符串中的数据来自文本框,我需要在 hexUpDown.Value 参数中写入该文本框的内容。
var ulongs = words.Select(x => ulong.Parse(x, NumberStyles.HexNumber)).ToArray();
如果您需要将字节组合成 64 位值,请尝试此操作(假设正确的 endieness)。
string[] words = { "0B", "00", " 00", "00", "00", "07", "3F", "14", "1D" };
var words64 = new List<string>();
int wc = 0;
var s = string.Empty;
var results = new List<ulong>();
// Concat string to make 64 bit words
foreach (var word in words)
{
// remove extra whitespace
s += word.Trim();
wc++;
// Added the word when it's 64 bits
if (wc % 4 == 0)
{
words64.Add(s);
wc = 0;
s = string.Empty;
}
}
// If there are any leftover bits, append those
if (!string.IsNullOrEmpty(s))
{
words64.Add(s);
}
// Now attempt to convert each string to a ulong
foreach (var word in words64)
{
ulong r;
if (ulong.TryParse(word,
System.Globalization.NumberStyles.AllowHexSpecifier,
System.Globalization.CultureInfo.InvariantCulture,
out r))
{
results.Add(r);
}
}
结果:
List<ulong>(3) { 184549376, 474900, 29 }