sbyte[] 转byte【】
时间: 2023-04-04 13:03:33 浏览: 94
可以使用以下代码将 sbyte[] 转换为 byte[]:
sbyte[] sbytes = new sbyte[] { -1, 2, -3, 4, -5 };
byte[] bytes = new byte[sbytes.Length];
Buffer.BlockCopy(sbytes, 0, bytes, 0, sbytes.Length);
相关问题
c# sbyte[] 转String
可以使用Encoding类的GetString方法将sbyte[]转换为String,示例代码如下:
```
sbyte[] bytes = new sbyte[] { 72, 101, 108, 108, 111 };
string str = Encoding.Default.GetString((byte[])(Array)bytes);
Console.WriteLine(str); // 输出 "Hello"
```
需要注意的是,sbyte是有符号的byte类型,而GetString方法需要的是无符号的byte数组,因此需要将sbyte[]强制转换为byte[]。
c#的byte[] 怎么转成sbyte* ?
在 C# 中,byte[] 数组可以通过以下方式转换为 sbyte* 指针:
```csharp
byte[] byteArray = { 1, 2, 3 };
sbyte[] sbyteArray = Array.ConvertAll(byteArray, b => (sbyte)b);
fixed (sbyte* p = sbyteArray)
{
// 使用指针 p
}
```
这里的关键是使用 `fixed` 语句固定 sbyte[] 数组的地址,然后获取指向它的指针。在 `fixed` 语句块中,可以使用指针 `p` 来操作 sbyte 数组的内容。