简体   繁体   English

从整数转换为BCD

[英]Conversion from Integer to BCD

I want to convert the integer (whose maximum value can reach to 99999999) in to BCD and store in to array of 4 characters. 我想将整数(其最大值可以达到99999999)转换为BCD并存储到4个字符的数组中。 Like for example: Input is : 12345 (Integer) Output should be = "00012345" in BCD which is stored in to array of 4 characters. 例如:输入为:12345(整数)在BCD中,输出应为=“ 00012345”,并存储在4个字符的数组中。 Here 0x00 0x01 0x23 0x45 stored in BCD format. 这里0x00 0x01 0x23 0x45以BCD格式存储。 I tried in the below manner but didnt work 我以下面的方式尝试了但没用

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++;
}

Any help will be appreciated 任何帮助将不胜感激

str points actually to a long (probably 4 bytes), but the iteration accesses 8 bytes. str实际上指向一个长字节(可能是4个字节),但是迭代访问了8个字节。 The operation str[len]%10 looks as if you are expecting digits, but there is only binary data. str[len]%10看起来像您期望数字,但是只有二进制数据。 In addition I suspect that i gets negative. 另外我怀疑i会变得消极。

First, don't use C-style casts (like (long)a or (char*) ). 首先,不要使用C样式的强制类型转换(例如(long)a(char*) )。 They are a bad smell. 他们是难闻的气味。 Instead, learn and use C++ style casts (like static_cast<long>(a) ), because they point out where you are doing things that are dangeruos, instead of just silently working and causing undefined behavior. 相反,学习并使用C ++样式强制转换(例如static_cast<long>(a) ),因为它们指出了您在做危险的事情,而不仅仅是默默地工作并导致未定义的行为。

char* str = (char*)& aux; gives you a pointer to the bytes of aux -- it is actually char* str = reinterpret_cast<char*>(&aux); 给您一个指向aux 字节的指针-实际上是char* str = reinterpret_cast<char*>(&aux); . It does not give you a traditional string with digits in it. 它不会为您提供带数字的传统字符串。 sizeof(char) is 1, sizeof(long) is almost certainly 4, so there are only 4 valid bytes in your aux variable. sizeof(char)为1, sizeof(long)几乎为4,因此aux变量中只有4个有效字节。 You proceed to try to read 8 of them. 您继续尝试阅读其中的8个。

I doubt this is doing what you want it to do. 我怀疑这正在做您想要做的事情。 If you want to print out a number into a string, you will have to run actual code, not just reinterpret bits in memory. 如果要将数字打印成字符串,则必须运行实际代码,而不仅仅是重新解释内存中的位。

std::string s; std::stringstream ss; ss << aux; ss >> s; will create a std::string with the base-10 digits of aux in it. 会创建一个std::string ,其中以aux为基数的10位数字。

Then you can look at the characters in s to build your BCD. 然后你可以看一下字符s建立自己的BCD。

This is far from the fastest method, but it at least is close to your original approach. 这不是最快的方法,但至少接近您的原始方法。

First of all sorry about the C code, I was deceived since this started as a C questions, porting to C++ should not really be such a big deal. 首先,对C代码感到抱歉,自从这作为C问题开始以来,我就被蒙骗了,移植到C ++应该不是什么大问题。

If you really want it to be in a char array I'll do something like following code, I find useful to still leave the result in a little endian format so I can just cast it to an int for printing out, however that is not strictly necessary: 如果您真的希望将其保存在char数组中,则可以执行以下代码,发现将结果保留为小端格式很有用,因此我可以将其转换为int以进行打印,但这不是严格必要的:

#include <stdio.h>

typedef struct
{
    char value[4];
} BCD_Number;

BCD_Number bin2bcd(int bin_number);

int main(int args, char **argv)
{
    BCD_Number bcd_result;

    bcd_result = bin2bcd(12345678);

    /* Assuming an int is 4 bytes */
    printf("result=0x%08x\n", *((int *)bcd_result.value));

}

BCD_Number bin2bcd(int bin_number)
{
    BCD_Number bcd_number;  

    for(int i = 0; i < sizeof(bcd_number.value); i++)
    {
        bcd_number.value[i] = bin_number % 10;
        bin_number /= 10;
        bcd_number.value[i] |= bin_number % 10 << 4;
        bin_number /= 10;
    }

    return bcd_number;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM