我有两个字节,字节 b1
、字节 b2
。
b1
十进制值为 44。
b2
十进制值为1。
我想将字节连接在一起(但不是求和),以创建一个像 441 这样的数字。
我如何实现这一目标?
最佳答案
数学方法
double digitCount = Math.floor(Math.log10(b2) + 1.0);
int result = b1 * (int) Math.pow(10, digitCount) + b2;
<小时/>
迭代方法(从右到左一次分割一位数字)
int result = 0;
int digit, multiplier = 1;
int base = b2;
while (base > 0) {
digit = base % 10;
base /= 10;
result += digit * multiplier;
multiplier *= 10;
}
base = b1;
while (base > 0) {
digit = base % 10;
base /= 10;
result += digit * multiplier;
multiplier *= 10;
}
<小时/>
直接方法(连接为字符串并解析)
int result = Integer.parseInt("" + b1 + b2);
关于java - 将两个字节合并为int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52457855/