home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Easy c program (Binary -> decimal digits)

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2339
Discussions: 200,940, Posts: 2,379,262, Members: 246,303
Old April 24th, 2008, 01:34 AM   Digg it!   #1 (permalink)
Ultimate Member
 
joker_927's Avatar
 
Join Date: May 2002
Location: California, USA
Posts: 2,385
Easy c program (Binary -> decimal digits)

I have failed to find anything on the internet that helps me with this very uninteresting program that I am trying to write for a much bigger overall program.

All I'm trying to do is take 1 unsigned 8-bit binary value (so far I have just used the char data type to store it) and I need to either:

split that binary number into its 3 respectful decimal digits (still in binary form for each digit)

OR

split it into its three decimal digits and have it be in BCD form.

Anyone know of an equation to do this or have or can make a simple c program that does it?

Thanks for any help
__________________
Abit AW9D-Max | E6300 | XP-120 | Panaflow 120mm | 2x 1GB G.Skill DDR2-800 | BFG 8800GT | Tagan 480w
joker_927 is offline   Reply With Quote
Old April 24th, 2008, 04:49 AM     #2 (permalink)
It's the cheese guy! ¬_¬;
 
paul9's Avatar
 
Join Date: Aug 2003
Location: Gateshead U.K.
Posts: 9,167
Send a message via MSN to paul9 Send a message via Yahoo to paul9
This page may be of help to you. http://www2.hawaii.edu/~wes/ICS212/Notes/BDHA.html
__________________
No man's life, liberty, or property are safe while the legislature is in session. --Mark Twain (1866)
paul9 is offline   Reply With Quote
Old April 24th, 2008, 01:45 PM     #3 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,083
Send a message via AIM to sr71000
This homework?

Code:
//returns unsigned int value of binary string myString
//returns -1 on error
int binCharToInt(char* myString)
{
    int length = strlen(myString);
    int myInt = 0;

    for(int iterator = 0; iterator < length; iterator++)
    {
        myInt *= 2;

        //if 0 do nothing
        //if 1 add one to your int
        //if anything else, return 0
        if(myString[iterator] == '1')
            myInt++;
        else if(myString[iterator] != '0')
            return -1;
    }
    return myInt;
}

//returns first 3 least significant numbers in the int
//index 0 -> least significant figure
int[] split(int myInt)
{
    int[3] myIntArray;
    myIntArray[0] = myInt % 10;
    myIntArray[1] = (myInt / 10) % 10;
    myIntArray[2] = (myInt / 100) % 10;

    return myIntArray;
}
haven't compiled, tested, or run the code but if i'm thinking clearly (and right now i'm BAKING at work so i might not be thinking too clearly) that should do something along the lines of what you're looking for.

-Kevin
sr71000 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
binary to decimal in java jgargac Webmastering and Programming 10 February 14th, 2009 10:48 PM
binary to decimal in java omcmb Webmastering and Programming 6 September 21st, 2007 07:20 PM
Converting binary to decimal Toasted Onions Webmastering and Programming 16 October 13th, 2005 08:14 AM
yo what program can make PHP websites easy? Chooco Webmastering and Programming 3 March 29th, 2002 06:18 AM
Need Fraction to Decimal conversion program mad1 IMO Community 5 February 15th, 2002 02:44 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2910)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Wireless Televisions. (8)
California Passes Anti-Flat-HDTV Le.. (43)
Obama the Muslim (14)
Regular Build (5)
Is the PSU I received dead? (11)
windows vista security holes (9)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
Print spooler problem (13)
Foreign voltage (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
Recent Discussions
Open With ..... Win7 (0)
Regular Build (5)
java code for fibonacci (1)
screen resolution vs monitor size (0)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (35)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Partition Magic caused HDD problem (3)
Is the PSU I received dead? (11)
Have you switched yet? (85)
Point and Shoot Camera Suggestions. (2)
Modern Warfare 2 freeze (13)
Wireless Televisions. (8)
wireless user (1)
World's largest Monopoly Game using G.. (332)
Ideal cheap graph card for PC-Gaming? (17)
BIOS won't read disk when I try to fl.. (0)
Install XP pro and a Vista laptop ?? (11)
Graphics Card Upgrade Question (1)
favorit (1)
solutions for virtical white lines on.. (1)
Fire in DVD (2)
Modern Warfare For the PC (33)
radeon x850xt platinum & shader 3 (3)
Wireless Router+Cable Modems and Much.. (0)


All times are GMT -4. The time now is 01:25 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28