Thread: Decimal to binary unsigned char

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Decimal to binary unsigned char

    Hello,
    I'm trying to write a recursive function to convert a decimal unsigned char to binary notation. Only problem is that I don't know what stopping condition I could implement.
    It can't be when my number is 0, because it's an unsigned char, it goes automatically to 255 when it reaches zero. Anyone got an idea?

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    No. zero is not 255.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    0 would be the stopping condition to look for, divide the number by 2 until nothing is left.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    One way to do a loop for an unsigned variable from 255 down to zero:
    Code:
    for (unsigned int i=256; i-- > 0;)
    The concept is similar to that of a reverse iterator in C++, where it internally actually points at the item after the one you want. Here it appears to start at 256 and go down until 1, but due to the i-- that is excuted at the startt of each loop, the valu inside the loop is one less than that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Pass in a "bits left to convert" parameter. Decrement it each time, until it's zero:
    Code:
    print_binary(unsigned char x, int bits_left)
    {
        if bits_left > 0
            print bit
            print_binary(x/2, bits_left-1)
    }
    ...
    print_binary(42, 8)  // print the number 42 in binary, it has 8 bits

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure why you'd need to keep track of the bits left, since your constant dividing by 2 does that for you. Once x hits zero, you don't need to go again.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by quzah View Post
    I'm not sure why you'd need to keep track of the bits left, since your constant dividing by 2 does that for you. Once x hits zero, you don't need to go again.
    I was imagining the OP always wanted 8 digits printed, in which case, any number less than 128 would print fewer digits if the stop condition was simply x > 0. I suppose that wasn't specified, so it may not be necessary. In any case, I just realized my code will print them in reverse if printing the LSB. Oh well, the OP can sort that out for himself if need be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading blocks of binary data into a vector<unsigned char>?
    By RichSelian in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2011, 03:26 AM
  2. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  3. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  4. Decimal to 16 bit unsigned?
    By blurx in forum C Programming
    Replies: 5
    Last Post: 10-16-2008, 01:56 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM