Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6008097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:46:35+00:00 2026-05-23T01:46:35+00:00

Where can I find a free or open source C++ library to do Binary

  • 0

Where can I find a free or open source C++ library to do Binary Coded Decimal math?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:46:36+00:00Added an answer on May 23, 2026 at 1:46 am

    Here you go. I just wrote this, and am making it public domain.

    It converts an unsigned bcd to an unsigned int and vice-versa. Use bcd2i() to convert your BCDs to unsigned ints, do whatever math you need, then use i2bcd() to bring the numbers back to BCD.

    unsigned int bcd2i(unsigned int bcd) {
        unsigned int decimalMultiplier = 1;
        unsigned int digit;
        unsigned int i = 0;
        while (bcd > 0) {
            digit = bcd & 0xF;
            i += digit * decimalMultiplier;
            decimalMultiplier *= 10;
            bcd >>= 4;
        }
        return i;
    }
    
    unsigned int i2bcd(unsigned int i) {
        unsigned int binaryShift = 0;  
        unsigned int digit;
        unsigned int bcd = 0;
        while (i > 0) {
            digit = i % 10;
            bcd += (digit << binaryShift);
            binaryShift += 4;
            i /= 10;
        }
        return bcd;
    }
    // Thanks to EmbeddedGuy for bug fix: changed init value to 0 from 1 
    
    
    #include <iostream>
    using namespace std;
    
    int main() {
    int tests[] = {81986, 3740, 103141, 27616, 1038, 
                   56975, 38083, 26722, 72358, 
                    2017, 34259};
    
    int testCount = sizeof(tests)/sizeof(tests[0]);
    
    cout << "Testing bcd2i(i2bcd(test)) on 10 cases" << endl;
    for (int testIndex=0; testIndex<testCount; testIndex++) {
        int bcd = i2bcd(tests[testIndex]);
        int i = bcd2i(bcd);
        if (i != tests[testIndex]) {
            cout << "Test failed: " << tests[testIndex] << " >> " << bcd << " >> " << i << endl;
            return 1;
        }
    }
    cout << "Test passed" << endl;
    return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://www.google.fr/search?q=yacy+OPENSOURCE YaCy is free software (GPL-licensed) and open source. but I can't seem to
Often you can use open source projects for free as long as you don't
Related to https://stackoverflow.com/questions/139944/where-can-one-find-free-software-icons-images I have a need for free weather-related icons. Specifically, I need
Where can I find a free, very quick, and reliable implementation of FFT in
Where can I find a free, lightweight YUI-like compressor for PHP? I am sure
Any ideas where can I find a free (trial or for educational purposes) WebSphere
Does anybody know of a free flash obfuscator(protector)? All I can find are commercial
I'm looking for a good, free C compiler for Windows. Where can I find
I'm just curious as to whether such as tool exists (free, open source, or
What are your criteria for selection a (open source) library (or framework) for enterprise

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.