高效地将字节数组解析为数字

时间:2015-02-02 08:01:48

标签: java

不是将字节数组解析为ASCII字符串,而是将字符串转换为整数,将字节数组直接解析为整数应该更有效。

byte[] token = "24000".getBytes(Charset.forName("US-ASCII"));

以下代码可以执行此操作:

int n = 0;
for (byte b : token)
  n = 10*n + (b-'0');

与常用方法相对应:

int n = Integer.parseInt(new String(token));

参考:Dave的回答>> Converting US-ASCII encoded byte to integer and back

是否有一个全面的解决方案可以跳过字符串创建并直接导致结果?

  

由于这个问题,请停止标记关闭的问题:   Convert a byte array to integer in java and vice versa

     

它处理非编码字节。

     

它没有回答我的问题。

1 个答案:

答案 0 :(得分:1)

Java库似乎没有专门的工具,但它确实有足够的工具来自己编写。

在我看来,如果您担心性能,因为将字节数组转换为int是代码中的瓶颈,那么我建议您根据自己提供的代码编写自己的解决方案。如果不是,那么只需使用parseInt以便于阅读。

在任何情况下,如果Java有一个工具来执行此操作,它将使用几乎相同的代码。这几乎是Integer.parseInt()所做的(除了它涵盖其他基数,负数,并且更安全):

 public static int parseInt(String s, int radix)
                throws NumberFormatException
    {
        /*
         * WARNING: This method may be invoked early during VM initialization
         * before IntegerCache is initialized. Care must be taken to not use
         * the valueOf method.
         */

        if (s == null) {
            throw new NumberFormatException("null");
        }

        if (radix < Character.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " less than Character.MIN_RADIX");
        }

        if (radix > Character.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " greater than Character.MAX_RADIX");
        }

        int result = 0;
        boolean negative = false;
        int i = 0, len = s.length();
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;

        if (len > 0) {
            char firstChar = s.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                    negative = true;
                    limit = Integer.MIN_VALUE;
                } else if (firstChar != '+')
                    throw NumberFormatException.forInputString(s);

                if (len == 1) // Cannot have lone "+" or "-"
                    throw NumberFormatException.forInputString(s);
                i++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(s.charAt(i++),radix);
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                if (result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        return negative ? result : -result;
    }