为啥这段代码不能在 Java 中运行? [复制]

Posted

技术标签:

【中文标题】为啥这段代码不能在 Java 中运行? [复制]【英文标题】:why this Code can't run in Java? [duplicate]为什么这段代码不能在 Java 中运行? [复制] 【发布时间】:2013-01-18 02:27:43 【问题描述】:

可能重复:unsigned short in javaConversion of Audio Format in JAVA

我有一个将 ulaw 音乐转换为 pcm 格式的代码

代码是由 C 编写的,但现在,我需要它在 java 中运行

代码是这样的

short ALG_ulawDecode(unsigned short input)

unsigned short isNegative;

short nOut;

isNegative = ((input & 0x80) == 0);
if (isNegative)
    nOut = 127 - input;
else
    nOut = 255 - input;
if (nOut < 2)
    nOut *= 2;
else if (nOut < 16)
    nOut = ((nOut - 1) << 1) + 1 + 1;
else if (nOut < 32)
    nOut = ((nOut - 16) << 2) + 2 + 31;
else if (nOut < 48)
    nOut = ((nOut - 32) << 3) + 4 + 95;
else if (nOut < 64)
    nOut = ((nOut - 48) << 4) + 8 + 223;
else if (nOut < 80)
    nOut = ((nOut - 64) << 5) + 16 + 479;
else if (nOut < 96)
    nOut = ((nOut - 80) << 6) + 32 + 991;
else if (nOut < 112)
    nOut = ((nOut - 96) << 7) + 64 + 2015;
else
    nOut = ((nOut - 112) << 8) + 128 + 4063;
if (isNegative)
    nOut = -nOut;
nOut <<= 2;
return nOut;

Java 不支持『unsigned short』导致无法运行

有人给点建议吗?

感谢您的帮助!

【问题讨论】:

编写 C 或 C++。在 Java 中不能有无符号短。 【参考方案1】:

任何人都可以给您的最佳建议是阅读 Java 简介教程或书籍。

如错误所述,unsigned short 不是 Java 中的原语。

【讨论】:

【参考方案2】:

Java 没有有符号和无符号类型。删除未签名和已使用的已签名短裤,其余代码应该仍然有效。

【讨论】:

【参考方案3】:

char 在语法上等同于 Java 中的 unsigned short。但是,您最好不要将它用于任何公共方法,因为它在语义上不是整数类型。

如果你想使用unsigned short,我认为int 适合。你可以只使用它的最低 16 位。

【讨论】:

以上是关于为啥这段代码不能在 Java 中运行? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

为啥这段代码不能用 clang 构建,用 gcc 崩溃,但用 VC++ 运行良好? [复制]

有人能告诉我为啥这段代码似乎可以工作,但不能……拜托?

Arduino IDE:“没有命名类型”,为啥我不能写这段代码? [复制]

为啥这段代码不能在 Visual Studio 2010 中编译和运行?

为啥这段代码可以在 Linux 上运行,但不能在 Windows 上运行?

为啥我不能从我的 java 程序运行批处理命令? [复制]