簡體   English   中英

在C#中將ushort []數組轉換為單個ushort

[英]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);

Demo

ushortValue = Convert.ToUInt16(test[0].ToString() + test[1].ToString());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM