java - 将字节数组转换为相应整数的快速方法

标签 java string integer arrays

首先,这听起来像是这里的问题: How to convert a byte array to its numeric value (Java)?

但是我的字节数组的起源是一个字符串,如下所示:

byte[] foo = new byte[8];
foo = "12345678".getBytes();

有没有比
更快的方法(是的,确实是快速做到这一点) Integer.parseInt(new String(foo))? 字符串仅包含代表整数的数字。

最佳答案

试试这个

    int res = 0;
    for(int i = foo.length -1, m = 1; i >=0; i--, m *= 10) {
        res += (foo[i] & 0xF) * m; 
    }

关于java - 将字节数组转换为相应整数的快速方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21076399/

相关文章:

python - 将文件中的字符串转换为整数以在新代码中使用

java - 高效所有子串按排序顺序计数

c - 在 C 中的 malloc 字符串之前保留元数据是否安全?

java - 使用对象声明数组的数组

java - 以编程方式在 Spring 回滚事务

string - VHDL - 字符串索引 - RAM 使用量和总逻辑元素各增加 100% 以上

C - 一次读取单个 int

python - 解析 JSON 时字符串索引必须是整数 - python

java - Inet6Address.getByName() 对 0 :0:0:0:0:ffff:0:0 给出奇怪的响应

java - 如何在 Firebase 中始终存储/检索 double