April 24th, 2008, 01:45 PM
|
#3 (permalink)
|
| Super F@D Folder
Join Date: Jun 2004
Posts: 5,083
|
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 |
| |