【问题标题】:Java to C# conversion - int uint typesJava 到 C# 的转换 - int uint 类型
【发布时间】:2016-02-13 14:07:17
【问题描述】:

我正在将代码从 java 转换为 c#,但遇到了 int 和 uint。 Java 代码(摘录):

public static final int SCALE_PROTO4_TBL    = 15;
public static final int [] sbc_proto_4 = { 0xec1f5e60 >> SCALE_PROTO4_TBL };

转换后的 c# 代码(摘录):

public const int SCALE_PROTO4_TBL = 15;
int[] sbc_proto_4 = new int[] { 0xec1f5e60 >> SCALE_PROTO4_TBL };

这会导致以下编译错误: 错误 CS0266 无法将类型“uint”隐式转换为“int”。存在显式转换(您是否缺少演员表?)

存在很多类似上面的代码,但我不确定是否应该将所有 Java int 转换为 C# uint?我记得 C# uint 不会存储负值,如果 java int 是负数,那么我遇到了问题。

关于我应该如何解决问题的任何意见?

【问题讨论】:

  • 您需要将 uint 显式转换为 int。但考虑到这可能会导致数据丢失。

标签: java c# int type-conversion uint


【解决方案1】:

你可以这样试试:

int[] sbc_proto_4 = new int[] { unchecked((int)0xec1f5e60) >> SCALE_PROTO4_TBL };

即,您必须明确地转换 uint(0xec1f5e60)。

【讨论】:

  • 我知道未选中的关键字,但我不能使用它。例如。如果你打印出 0xec1f5e60 的值,你会看到 3961478752。如果你使用 unchecked 关键字转换它,那么值将是 -333488544
  • 猜我应该只是 long 而不是 int
  • @user1005448:- 关键是您需要对 uint 值进行显式转换。我不确定你想在你的上下文中使用哪个值。希望您现在可以了解错误原因以及解决方法
  • 不能使用 long 作为数据类型,因为 >> 指定第二个必须是 int 类型
  • 我试图查看相同的十六进制值,所以我会接受你的答案以使用未选中的。感谢您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 2014-07-31
  • 1970-01-01
  • 2018-03-30
  • 2012-08-27
  • 1970-01-01
相关资源
最近更新 更多