字符串二进制转十六进制 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13677121/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:33:54  来源:igfitidea点击:

String binary to Hex Java

javastringbinaryhex

提问by Dominic J. Lucenario

i have code that looks like this

我有看起来像这样的代码

             public static void main(String[] args) {
    String string= "11011100010000010001000000000000";
   String string1= "00000000010000110000100000101100";

    System.out.println(Integer.toHexString(Integer.parseInt(string1,2)));

    System.out.println(Integer.toHexString(Integer.parseInt(string,2)));


}

the first string convert just fine but the second one has an error of java.lang.NumberFormatException dont know what the problem is

第一个字符串转换得很好,但第二个字符串有 java.lang.NumberFormatException 错误不知道是什么问题

采纳答案by dasblinkenlight

When the most significant bit of a 32-character binary number is set to 1, the resultant value exceeds the range of positive numbers supported by int, and can no longer be interpreted as a valid integer number. This causes the exception according to the documentation:

当 32 个字符的二进制数的最高有效位设置为 时1,结果值超出 支持的正数范围,int不能再解释为有效整数。根据文档,这会导致异常:

An exception of type NumberFormatException is thrown if any of the following situations occurs:

如果发生以下任何一种情况,则会抛出 NumberFormatException 类型的异常:

  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  • Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1.
  • The value represented by the string is not a value of type int.(emphasis is mine)
  • 第一个参数为空或长度为零的字符串。
  • 基数小于 Character.MIN_RADIX 或大于 Character.MAX_RADIX。
  • 字符串的任何字符都不是指定基数的数字,但第一个字符可以是减号“-”('\u002D'),前提是该字符串的长度大于长度 1。
  • 字符串表示的值不是 int 类型的值。(重点是我的)

In order to enter this negative binary value, use -sign in front of your number, and convert the remaining bits to 2-s complement representation.

为了输入这个负二进制值,-在你的数字前面使用符号,并将剩余的位转换为 2-s 补码表示。

If you need numbers that are longer than 32 bits, or if you would like the value to continue being interpreted as a positive number, you would need to switch to the 64-bit integer data type.

如果您需要长度超过 32 位的数字,或者您希望该值继续被解释为正数,则需要切换到 64 位整数数据类型。

回答by Ravindra Bagale

try this:

试试这个:

Long.toHexString(Long.parseLong(string,2))

(edited from parsLong to parseLong)

(从 parsLong 编辑为 parseLong)

回答by Yanick Rochon

For what's worth, you can also use the BigIntegerclass :

值得一提的是,您还可以使用BigInteger该类:

String string  = "11011100010000010001000000000000";
String string1 = "00000000010000110000100000101100";

System.out.println(new BigInteger(string1, 2).toString(16));
System.out.println(new BigInteger(string, 2).toString(16));

回答by Bhesh Gurung

You can use Longinstead of Integer, (Long.parseLongand Long.toHexStringmethods).

您可以使用Long代替Integer,(Long.parseLongLong.toHexString方法)。

回答by bhuang3

If you want to parse to integer, the range should be

如果你想解析为整数,范围应该是

10000000000000000000000000000000to 01111111111111111111111111111111

1000000000000000000000000000000001111111111111111111111111111111