gpt4 book ai didi

c# - Java代码-字节数组中的负字节到C#

转载 作者:太空狗 更新时间:2023-10-29 23:58:09 24 4
gpt4 key购买 nike

在 Java 中,我有以下行:

new byte[]{59, 55, 79, 1, 0, 64, -32, -3};

但是,在 C# 中,我不能在字节数组中使用负字节。我尝试将其转换为字节但失败了。我能做什么?谢谢!

最佳答案

因为在 C# 中字节是无符号。在 Java 中,字节是有符号的

来自 Byte Structure

Byte is an immutable value type that represents unsigned integers withvalues that range from 0 (which is represented by the Byte.MinValueconstant) to 255 (which is represented by the Byte.MaxValue constant)

来自 Primitive Data Types

The byte data type is an 8-bit signed two's complement integer. It hasa minimum value of -128 and a maximum value of 127

我能做什么?

您可以使用 sbyte在 C# 中表示 8 位有符号整数。

The SByte value type represents integers with values ranging fromnegative 128 to positive 127.

喜欢;

sbyte[] sb = new sbyte[] {59, 55, 79, 1, 0, 64, -32, -3};

关于c# - Java代码-字节数组中的负字节到C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18428891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com