- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试将 short 转换为 2 个字节...然后从这 2 个字节尝试获得相同的 short 值。为此,我编写了这段代码:
short oldshort = 700;
byte 333= (byte) (oldshort);
byte byte2= (byte) ((oldshort >> 8) & 0xff);
short newshort = (short) ((byte2 << 8) + byte1);
System.out.println(oldshort);
System.out.println(newshort);
对于 700(oldshort)的值,newhosrt 是 444。经过一些测试,它看起来像\t这段代码只适用于某些值。就像...如果 oldshort=50,那么它会工作正常..但是如果它是 -200,或者大于 127 的值(我认为)它就不起作用。我猜是有符号字节、二进制补码等有问题...但我不知道如何解决。
有什么想法吗?在 Java 中执行此操作的任何 native 方法?提前致谢!
最佳答案
重组时,需要屏蔽byte1,防止其被符号扩展。
例如
short oldshort = 700;
byte byte1= (byte) (oldshort);
byte byte2= (byte) ((oldshort >> 8) & 0xff);
short newshort = (short) ((byte2 << 8) + (byte1&0xFF);
System.out.println(oldshort);
System.out.println(newshort);
编辑:java 中对字节和短裤的所有操作实际上都是作为整数完成的。所以当你写+byte1
,真正发生的是字节首先被转换为整数(符号扩展)。它仍然具有相同的值,但现在有更多的位。然后,我们可以屏蔽掉底部的 8 位,以从 short 中获得原始的 8 位 - 没有符号。
E.g. short =511 = 0x01FE
// lots of 0x000's because the operations are done on 32-bit int's
byte1 = (0x000001FE & 0x000000FF) = (0x01FE & 0xFF) = 0xFE = (byte)-2
byte2 = 0x1
newShort = (byte2 << 8) + (byte1 & 0xFF)
= (0x1 << 8) + (0xFE & 0xFF)
// since the ops are performed as int's
= (0x00000001 << 8) + (0xFFFFFFFE & 0x000000FF)
// 0xFFFFFFFE = -2
= (0x00000100) + (0x000000FE)
= 0x000001FE
= 511
关于java - 在 Java 中从 short 转换为 byte,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3114935/
我有一个问题,我有一个数据结构和多个线程试图对其进行操作。说得越简单越好:我有线程A、B和C,线程A只能做它的操作,只要B和C不对数据集做任何改变。 B 和 C 大部分时间都可以同时在设备上操作。所以
我有不寻常格式的数据。变量名/列标题应该是当前行值,行值应该是变量名/列标题。 也就是说,我有这样一个数据框: id % gather(key, val, -id) %>% filter(
我需要在字符串 foo bar foo bar bar foo 中将所有 foo 替换为 bar 并将所有 bar 替换为 foo 。所以结果应该看起来像 bar foo bar foo foo ba
我对 Azure 还不太熟悉。 问题是我正在开发 Azure Functions,有时我必须在本地工作(代码/测试等),有时则在 Azure 上工作。每次切换时,我都必须手动比较和更改应用程序设置。
如何在 php 中从一年中的第 n 天到这样的日期: getdatefromday(275, 2012) 它输出一个日期(如果是一个对象更好)。 我也想做相反的事情,比如getdayoftheyear
我有一个字符串 var str = "か22222"; 我想这样搜索 str.indexOf("か2");其中“か2”都是多字节字符,而“str”只有“か”是多字节 但我仍然应该得到indexOf的结
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我正在编写一个C程序,从标准输入中逐一读取字符,将所有大写字符转换为小写,将所有小写字符转换为大写,并将结果写入标准输出。我还想计算我读了多少个字符,以及其中每个方向上转换了多少个字符,并在最后输出总
如何在“动态 C”中将整数转换为字符,反之亦然。 使用VB.NET如下: Dim i As Integer Dim c As Char ' Integer to Character i = 302 c
我有一个包含以下文本的文本文件,没有换行符... Hello World 我想将小写字符转换为大写字符,反之亦然,这样同一个文本文件将以以下文本结束...... hELLOW wORLD 不幸的是,当
我有这个 C 代码,它试图在 signed char 中存储一个 signed short。signed char 范围是 [-128,127],signed short 范围是 [-32768, 3
这个问题类似于Sorted list to complete BST array representation但也许更专注。这个问题可以用来解决Inserting node dynamically i
我有 java 三个字符串。举个例子。 String test = "Hi, "; String test1 = "this is "; String test2 = "Java programmin
问题的最后一部分是什么让我失望。我需要使用 List Comprehension 在一行中执行此操作,到目前为止我已经尝试过: def Function(string): new_string
如何将十进制转换为二进制,反之亦然 我在solaris10平台上工作 谁能帮我一个命令 Decimal to Binary 4000000002-100000000000000000000000000
这个问题在这里已经有了答案: Assigning pointers to atomic type to pointers to non atomic type (2 个答案) 关闭去年。 给定以下代
我有一个 class A 作为 class B 的基类。 我在我的虚拟函数xyz()非虚拟函数abc(),如下所述。 由于运行时多态性,B:xyz 被调用——我理解这一点。 但是我不明白,为什么后面是
我想知道蓝牙 4.0(低功耗)手机是否可以发现经典的蓝牙设备(3.0 及更低版本),反之亦然。我感兴趣的只是发现“友好名称”。 编辑:据我了解这篇文章的回复,蓝牙 4.0 可以发现经典的蓝牙设备,但不
我的数据框如下: df = pd.DataFrame({'a': {'d': 1, 'e': 0, 'f': 1, 'g': 1}, 'b': {'d': 0,
我正在尝试使用 VBA,以便我可以在 sheet2 的单元格 B7 中输入一个值,然后它会自动填充到 sheet3 的 C7 中,反之亦然。我尝试了下面的代码,但无法正常工作,有什么建议吗?对于一串数
我是一名优秀的程序员,十分优秀!