0

I want to modify this function to return 4 bit packedBCD instead of Unpacked anyone with any pointers thanks.

string dfc = ConvertDecimalToBCD((int)decimalNumber);
cout<<"BDCValue::"<<dfc<<endl;
cout<<endl;

For decimal number 2000 i want to get 0010 0000 instead of 0010 0000 0000 0000. if its possible

    string ConvertDecimalToBCD(int decimal)
    {
        int i = decimal;
        string s, temp, final;
        stringstream out;
        out << i;
        s = out.str();

        for (int i = 0; i < s.size(); i++)
        {
            temp = ReturnBCDFormat(s[i]);
            final = final+temp;
        }

        return final;
    }

function to return bcd format.

string ReturnBCDFormat(char num)
{
    switch (num)
    {
    case '0':
        return "0000";
        break;
    case '1':
        return "0001";
        break;
    case '2':
        return "0010";
        break;
    case '3':
        return "0011";
        break;
    case '4':
        return "0100";
        break;
    case '5':
        return "0101";
        break;
    case '6':
        return "0110";
        break;
    case '7':
        return "0111";
        break;
    case '8':
        return "1000";
        break;
    case '9':
        return "1001";
        break;
    default:
        return "2";
        break;
    }
}
Mel Fzllc
  • 39
  • 8
  • For code formatting, indent *each line* of the block with four spaces. The backticks (`\``) are only for inline code formatting (when you want to put code in the middle of words.) – Cody Gray - on strike Feb 26 '16 at 17:02
  • This seems to be OK. What's the problem? – ForceBru Feb 26 '16 at 17:07
  • @CodyGray thanks for helping me edit, the code section, am actually using the code above for serial port communication which requires sending any amount of number ranging from (1 -100000) into 4 bytes bcd format so i needed someone more experienced to assist in looking at the code and advising me. i will also want performance inprovement since it being deployed on an embedded device and lastly pointer on how to convert back to decimal. – Mel Fzllc Feb 26 '16 at 17:21
  • If performance is a problem, check that your compiler is converting your `switch` statement to a jump table. If it is not, help it out by creating the jump table yourself. Aside from that, I don't really know anything about your question, it just popped up because of the C++ tag. – Cody Gray - on strike Feb 26 '16 at 17:23
  • This code does not produce BCD format. It produces a string representation in binary format of some encoding, at best. It almostvceetsinoymisnt what you really need. – user207421 Feb 26 '16 at 19:36

2 Answers2

0

if you change your ReturnBCDFormat to accept an integer 0-9 rather than a character '0'-'9', or you change the code below to add '0' to the parameter passed into ReturnBCDFormat, this should work:

string ConvertDecimalToBCD(int decimal)
{
    if(decimal == 0)
       return "0";
    else
    {
      string result;
      while(decimal > 0)
      {
        result = ReturnBCDFormat(decimal % 10) + result;
        decimal /= 10;
      }
      return result;
    }
}

If performance is critical in this, you could pack a pair of digits into an unsigned char, possibly using a vector to hold it, but the above code should still be faster than the stringstream solution.

Matt Jordan
  • 2,133
  • 9
  • 10
  • thanks for the timely response, your code is ok, but the format required for the program is this format `0010 0000` and its targeted towards an embedded device serial port communication – Mel Fzllc Feb 26 '16 at 17:34
  • 0010 0000 is what it can produce; only the input to ReturnBCDFormat is changed, not the output. What part of my answer doesn't fit the requirement? I didn't include spaces between the 4-character output terms because your sample code didn't add them either. – Matt Jordan Feb 26 '16 at 17:54
  • kudos but there is nothing wrong with your answer and indeed it improve performance wise, my apologies i forgot to remove the `'` from the code it was returning another format,but i was targeting the possibilities of returning values in four bytes BCD format e.g `2000` outputs `0010000000000000` can it be packed futher to say `0010 0000` – Mel Fzllc Feb 26 '16 at 19:09
  • packing it to 0010 0000 would be the same result that you would get from 20; the only thing I think you could do to improve it is to pack numeric values, so 2 digits would exist within each char rather than 2 digits requiring an 8-character string. so, 20 would become 2*16 + 0 = 32, which encoded in hex is 0x20 (the original use for BCD!) 2000 would encode as ((((2*16) + 0)*16 + 0)*16 + 0)*16 == 8192, which encoded in hex is 0x2000. Then, displaying is as easy as displaying each byte (2 digits) with ReturnBCDFormat(digit>>4) and Ret...(digit&15). Not sure if that would fit your need tho – Matt Jordan Feb 26 '16 at 20:10
  • thanks for the pointer, it looks complicated but worth the try for a serial port communication on an embedded device with low memory, more so i will have to covert them back to the decimal values as they will be a part of the serial port response. – Mel Fzllc Feb 27 '16 at 12:13
  • can you please review this code and offer me some professional opinion or some performace improvement code being trying to convert bcd to i but bave being getting wrong values `char BcdToDecimal(char bcd){ return (char)(((((bcd) >> 4) & 0x0F)*10)+(((bcd) & 0x0F))); }` `int i_bin = BcdToDecimal('0010000000000000');` want to get 2000 but got 30 – Mel Fzllc Feb 29 '16 at 10:39
  • converting bcd to integer will require an int return value and probably a string as the bcd parameter (the reverse of ConvertDecimalToBCD). the sample code only operates on one character, treating it as a pair of BCD digits, but that doesn't match the parameter '0010000000000000', which appears to be a base-2 representation but with char encoding marks (single quotes) rather than string encoding marks (double quotes). To convert your base-2 format back into an integer, you will be operating on strings that contain only 0 and 1, so multiply by 2 rather than 10. – Matt Jordan Feb 29 '16 at 15:38
0

First convert to BCD binary encoding, then print as for hex.

    int raw = 7947;
    int encoded = 0;
    int i = 0; 

    while ( raw != 0 )
    {
        int tenth = raw % 10;
        encoded |= tenth << (i * 4);
        raw /= 10;
        ++i;
    }

    cout << hex << encoded << std::endl;

or to generate a string directly

    int raw = 7947;
    string encoded;


    do {
        int tenth = raw % 10;
        encoded.insert( 0, 1, '0' + tenth ); // depends on ASCII digit 
        raw /= 10;
    }
    while ( raw > 0 );

    cout << encoded << std::endl;

Except this won't give the binary representation you want. But referring to this question How to print (using cout) the way a number is stored in memory? yields

std::bitset< sizeof( int ) > binary( encoded );
cout << binary << std::endl;
Community
  • 1
  • 1
Rob K
  • 8,757
  • 2
  • 32
  • 36