我有一个具有值的字符串数组
string[] words = {"0B", "00", " 00", "00", "00", "07", "3F", "14", "1D"};
我需要将其转换为ulong数组
ulong[] words1;
我应该如何在c#中进行操作
我想我应该增加一些背景。
字符串中的数据来自文本框,我需要在hexUpDown.Value参数中写入此文本框的内容。
答案 0 :(得分:2)
var ulongs = words.Select(x => ulong.Parse(x, NumberStyles.HexNumber)).ToArray();
答案 1 :(得分:0)
如果您需要将字节合并为64位值,请尝试执行此操作(假定正确的字节序)。
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 }