java 短到十六进制字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13356984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Short toHexString
提问by Oleg Pavliv
There are methods Integer.toHexString()
and Long.toHexString()
. For some reason they didn't implement Short.toHexString()
.
有方法Integer.toHexString()
和Long.toHexString()
。出于某种原因,他们没有实现Short.toHexString()
.
What is the canonical method converting Short to a hex string?
将 Short 转换为十六进制字符串的规范方法是什么?
It's not possible to use Integer.toHexString()
because Integer.toHexString(-33)
equals ffffffdf
which is not a short value.
无法使用,Integer.toHexString()
因为Integer.toHexString(-33)
equalsffffffdf
不是一个短值。
回答by David Julitz
If in your system short
is represented as 16Bit you can also simply do the following.
如果在您的系统short
中表示为 16Bit,您也可以简单地执行以下操作。
String hex = Integer.toHexString(-33 & 0xffff);
回答by emesx
Yes, you can simply take the two least-significant bytes.
是的,您可以简单地取两个最低有效字节。
This is a basic feature of the Two's Complementrepresentation.
这是Two's Complement表示的一个基本特征。
回答by Rohit Jain
You can convert your Integer.toHexString
in to a hex string for short value.
您可以将Integer.toHexString
in转换为短值的十六进制字符串。
Integer
is of 32 bit
, and Short
is of 16 bit
. So, you can just remove the 16 most significant bit from Hex String
for short value
converted to integer
, to get a Hex String
for Short
.
Integer
属于32 bit
,并且Short
属于16 bit
。因此,您只需从Hex String
forshort value
转换为 中删除 16 个最高有效位integer
即可获得Hex String
for Short
。
Integer -> -33 = 11111111 11111111 11111111 11011111 == Hex = ffffffdf
Short -> -33 = 11111111 11011111 == Hex = ffdf
So, just take the last 4
characters of Hex String
to get what you want.
因此,只需取最后一个4
字符Hex String
即可获得您想要的内容。
So, what you want is: -
所以,你想要的是:-
Short sh = -33;
String intHexString = Integer.toHexString(sh.intValue());
String shortHexString = intHexString.substring(4);
I think that would work.
我认为那会奏效。
回答by Java Addict
This should also work for a short
这也应该适用于短期
UnicodeFormatter.charToHex((char)c);
You can download UniocdeFormatter.java here: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/i18n/text/examples/UnicodeFormatter.java
您可以在此处下载 UniocdeFormatter.java:http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/i18n/text/examples/UnicodeFormatter 。爪哇
回答by evanmcdonnal
Not the simplest way to do this, but I you can get it done by converting to a byte array then converting that to a hex string.
这不是最简单的方法,但我可以通过转换为字节数组然后将其转换为十六进制字符串来完成它。
short a = 233;
byte[] ret = new byte[2];
ret[0] = (byte)(a & 0xff);
ret[1] = (byte)((a >> 8) & 0xff);
StringBuilder sb = new StringBuilder();
for (byte b : ret) {
sb.append(String.format("%02X ", b));
}
I think that will pretty much do it.
我认为这几乎可以做到。