我想將整數(其最大值可以達到99999999)轉換爲BCD並存儲到4個字符的數組中。 例如: 輸入爲:12345(整數) BCD中的輸出應爲=「00」,存儲在4個字符的數組中。 這裏0x00 0x01 0x23 0x45
以BCD格式存儲。 我試圖在以下方式,但沒有工作從Integer轉換爲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++;
}
任何幫助將理解實際上
我已經添加了一組代碼來做無符號整數轉換爲BCD在:http://stackoverflow.com/questions/1408361/unsigned-integer-to- BCD轉換/ 41598635#41598635 –