如何将字符串(字节数组作为字符串)转换为短字符串
How to convert String (byte array as string) to short
你好,我想将字节数组即 0x3eb 转换为短格式,所以我将 0x3eb 视为一个字符串并尝试转换为短格式但它抛出 Numberformat 异常...有人请帮助我
import java.io.UnsupportedEncodingException;
public class mmmain
{
public static void main(String[] args) throws UnsupportedEncodingException
{
String ss="0x03eb";
Short value = Short.parseShort(ss);
System.out.println("value--->"+value);
}
}
Exception what im getting is
Exception in thread "main" java.lang.NumberFormatException:
For input string: "0x3eb" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:491)
at java.lang.Short.parseShort(Short.java:117)
at java.lang.Short.parseShort(Short.java:143)
at mmmain.main(mmmain.java:14)
我什至尝试通过
将 0x3eb 转换为字节
byte[] bytes = ss.getBytes();
但我没有找到任何将字节解析为短的实现。
提前致谢
参见parseShort
的doc:
Parses the string argument as a signed decimal short. The characters
in the string must all be decimal digits, except that the first
character may be an ASCII minus sign '-' ('\u002D') to indicate a
negative value or an ASCII plus sign '+' ('\u002B') to indicate a
positive value.
要解析的字符串只能包含小数点和符号字符,不能包含0x
前缀。
尝试:
String ss="3eb";
Short value = Short.parseShort(ss, 16);
由于您使用的字符串值是十六进制值,要将其转换为短值,您需要使用子字符串删除 0x
并按如下方式传递基数:
Short.parseShort(yourHexString.substring(2), 16)
这里16是基数。文档中的更多信息 here.
更新
由于 OP 要求进行更多说明,因此添加以下信息。
short 数据类型的值只能介于 -32,768 和 32,767 之间。它不能直接存放0x3eb,但可以存放它的等价十进制值。这就是为什么当您将它解析为 short 变量并打印时,它显示 1003,这是 0x3eb 的十进制等效值。
你必须从头开始剪掉“0x”:
short.parseShort(yourHexString.Substring(2), 16)
按照此文档进行操作,这可能对您有所帮助 String to byte array, byte array to String in Java
你好,我想将字节数组即 0x3eb 转换为短格式,所以我将 0x3eb 视为一个字符串并尝试转换为短格式但它抛出 Numberformat 异常...有人请帮助我
import java.io.UnsupportedEncodingException;
public class mmmain
{
public static void main(String[] args) throws UnsupportedEncodingException
{
String ss="0x03eb";
Short value = Short.parseShort(ss);
System.out.println("value--->"+value);
}
}
Exception what im getting is
Exception in thread "main" java.lang.NumberFormatException:
For input string: "0x3eb" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:491)
at java.lang.Short.parseShort(Short.java:117)
at java.lang.Short.parseShort(Short.java:143)
at mmmain.main(mmmain.java:14)
我什至尝试通过
将 0x3eb 转换为字节byte[] bytes = ss.getBytes();
但我没有找到任何将字节解析为短的实现。
提前致谢
参见parseShort
的doc:
Parses the string argument as a signed decimal short. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.
要解析的字符串只能包含小数点和符号字符,不能包含0x
前缀。
尝试:
String ss="3eb";
Short value = Short.parseShort(ss, 16);
由于您使用的字符串值是十六进制值,要将其转换为短值,您需要使用子字符串删除 0x
并按如下方式传递基数:
Short.parseShort(yourHexString.substring(2), 16)
这里16是基数。文档中的更多信息 here.
更新
由于 OP 要求进行更多说明,因此添加以下信息。
short 数据类型的值只能介于 -32,768 和 32,767 之间。它不能直接存放0x3eb,但可以存放它的等价十进制值。这就是为什么当您将它解析为 short 变量并打印时,它显示 1003,这是 0x3eb 的十进制等效值。
你必须从头开始剪掉“0x”:
short.parseShort(yourHexString.Substring(2), 16)
按照此文档进行操作,这可能对您有所帮助 String to byte array, byte array to String in Java