我想将整数(其最大值可以达到 99999999)转换为 BCD 并存储到 4 个字符的数组中。例如:输入为:12345(整数)输出应为 BCD 格式的 =“00012345”,存储在 4 个字符的数组中。这里0x00 0x01 0x23 0x45
以 BCD 格式存储。我尝试了以下方式但没有奏效
int decNum = 12345;
long aux;
aux = (long)decNum;
cout<<" aux = "<<aux<<endl;
char* str = (char*)& aux;
char output[4];
int len = 0;
int i = 3;
while (len < 8)
{
cout <<"str: " << len << " " << (int)str[len] << endl;
unsigned char temp = str[len]%10;
len++;
cout <<"str: " << len << " " << (int)str[len] << endl;
output[i] = ((str[len]) << 4) | temp;
i--;
len++;
}
任何帮助将不胜感激