[英]Convert a ushort[] array to a single ushort in C#
有了ushort數組, ushort[]
如何將ushort數組[44,55]
轉換為單個聯接的ushort 4455
? 數組始終有兩個元素。
ushort[] test = new ushort[2];
test[0] = 44;
test[1] = 55;
// Here I want to convert the ushort[] to a unique ushort value 4455
usortValue = ?
return usortValue;
我原本期望一個.join
函數像String
一樣,但是使用ushort
是不可能的。
提前致謝
您可以聚合,謂詞為“轉換為字符串,連接並解析回ushort”,但是遲早(或早! )您將溢出。
ushort[] test = new ushort[2];
test[0] = 44;
test[1] = 55;
var result = test.Aggregate((p,c) => ushort.Parse((p.ToString() + c.ToString())));
實時示例: http : //rextester.com/VKV9570
將數字轉換為字符串並將其連接起來怎么樣?
var result =string.Join("",test.Select(x=>x.ToString()));
Convert.ToUInt16(result);
ushortValue = Convert.ToUInt16(test[0].ToString() + test[1].ToString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.