开发者

Is unsafe normal in .net C#?

开发者 https://www.devze.com 2023-03-10 20:57 出处:网络
I need do create a new instance of String from the array of sbytes (sbyte[]). For that I need to convert sbyte[] into sbyte*

I need do create a new instance of String from the array of sbytes (sbyte[]). For that I need to convert sbyte[] into sbyte*

It is possible only usin开发者_开发技巧g unsafe keyword. is that okay or is there any other ways to create a String from array of sbytes?


First: How to convert a sbyte[] to byte[] in C#?

sbyte[] signed = { -2, -1, 0, 1, 2 };
byte[] unsigned = (byte[]) (Array)signed;

Then:

string yourstring = UTF8Encoding.UTF8.GetString(unsigned);


Why are you using sbyte?

Encoding.Default.GetString() (and any other encoding) takes a byte[] Array as argument, so you could convert the sbyte[] Array using LINQ if all values are non-negative: array.Cast<byte>().ToArray().

0

精彩评论

暂无评论...
验证码 换一张
取 消