Java 将 2 位十进制转换为 int 数组中的 BCD
Java conversion from 2 digists decimal into BCD in an int array
我需要将一个两位数的十进制数转换成一个整型数组。例如,如果我们假设我有数字 32
,我想将其转换为 int[] a = new int[2]
,其中 int[0] = 3
和 int[1] = 2
.
您可以试试:
int x = 32;
int[] array = { x / 10, x % 10 };
试试这个:
int x, temp, i=0, j;
int[] a, b;
//Get the integer first and store in x
//Also allocate the size of a and b with the number of digits that integer has
while(x > 0)
{
temp = x%10;//gives remainder
a[i] = temp;//store that value in array
x = x/10;//to get the remaining digits
}
//the array will have the numbers from last digit
例如:
我有 x = 322
数组 a 为 { 2, 2, 3 }
//to get the actual array try this
j = a.length;
while(i<j)
{
b[i]=a[j];
i++;
j--;
}
您将得到数组 b 作为 { 3, 2, 2 }
通用版本:
假设您的号码存储在变量 x 中。
final int x = 345421;
final String serialized = String.valueOf(x);
final int[] array = new int[serialized.length()];
for (int i = 0; i < serialized.length(); i++) {
array[i] = Integer.valueOf(String.valueOf(serialized.charAt(i)));
}
我需要将一个两位数的十进制数转换成一个整型数组。例如,如果我们假设我有数字 32
,我想将其转换为 int[] a = new int[2]
,其中 int[0] = 3
和 int[1] = 2
.
您可以试试:
int x = 32;
int[] array = { x / 10, x % 10 };
试试这个:
int x, temp, i=0, j;
int[] a, b;
//Get the integer first and store in x
//Also allocate the size of a and b with the number of digits that integer has
while(x > 0)
{
temp = x%10;//gives remainder
a[i] = temp;//store that value in array
x = x/10;//to get the remaining digits
}
//the array will have the numbers from last digit
例如:
我有 x = 322
数组 a 为 { 2, 2, 3 }
//to get the actual array try this
j = a.length;
while(i<j)
{
b[i]=a[j];
i++;
j--;
}
您将得到数组 b 作为 { 3, 2, 2 }
通用版本:
假设您的号码存储在变量 x 中。
final int x = 345421;
final String serialized = String.valueOf(x);
final int[] array = new int[serialized.length()];
for (int i = 0; i < serialized.length(); i++) {
array[i] = Integer.valueOf(String.valueOf(serialized.charAt(i)));
}