Having a ushort array, ushort[]
how can I convert the ushort array [44,55]
to a single joined ushort 4455
? The array always has two elements.
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;
I was expecting a .join
function as String
does, but it's not possible with ushort
.
Thanks in advance
You can Aggregate, with the predicate being "convert to string, concatenate, and parse back to a ushort", but sooner or later ( sooner! ) you're going to overflow.
ushort[] test = new ushort[2];
test[0] = 44;
test[1] = 55;
var result = test.Aggregate((p,c) => ushort.Parse((p.ToString() + c.ToString())));
Live example: http://rextester.com/VKV9570
How about converting numbers to string and joining them?
var result =string.Join("",test.Select(x=>x.ToString()));
Convert.ToUInt16(result);
ushortValue = Convert.ToUInt16(test[0].ToString() + test[1].ToString());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.