binary to decimal in c – dead simple, innit
intro
Oi, you lot! New to C and fancy summat a bit nifty? We’re gonna sling together a lil program to turn a binary number—like all them 1s and 0s—into a proper decimal number ya can read. It’s dead easy but proper handy—like when I was tryna decode my mate’s binary tattoo after a sesh (took ages, too smashed). Great for getting yer head round loops and maths in C without a right faff. Let’s have a butcher’s!
code bit
Here’s the code—nowt posh, just a quick job I chucked together. Shoved it in a box so it don’t look like a total shambles on yer blog. take a gander:
#include
int binaryToDecimal(char bin[]) {
int decimal=0, power=0; // start from scratch, innit
int len=strlen(bin); // how long’s the string
for(int i=len-1;i>=0;i--) { // go backwards
if(bin[i]=='1') { // if it’s a 1
decimal+=1<
else if(bin[i]!='0') { // if it ain’t 0 or 1
return -1; // dodgy input!
}
power++; // bump the power
}
return decimal;
}
int main() { // nearly did void, ha!
char bin[32]; // big enough for a binary, innit
printf("gimme a binary number: ");
scanf("%s",bin); // grab it as a string
int result=binaryToDecimal(bin); // flip it
if(result==-1) {
printf("oi, that ain’t proper binary!\n");
}
else {
printf("%s in decimal is %d\n",bin,result); // forgot ; once, ugh!
}
return 0;
}
what ya get
Here’s what it chucks out when ya run it. I tested it with a couple binary numbers—worked bang on, mate!
1010 in decimal is 10
gimme a binary number: 1111
1111 in decimal is 15
gimme a binary number: 12ab
oi, that ain’t proper binary!
how it works, kinda
Right, let’s have a natter bout this like we’re slumped at the boozer with a pint—or a bag of pork scratchings, coz I’m Hank Marvin again. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a doddle once ya clock it, swear down!
1. them `#include` bits
This is me nicking my toolkit. `
2. `int main()` funtion
Gotta have a “main” to kick off—it’s like the door to my local. `int` means I’m lobbing a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a right numpty!
3. boxes: `char bin[32];`
`bin[32]` is my lil bucket—32 chars tops, plenty for a binary number. Taking it as a string coz it’s easier to nab 1s and 0s that way!
4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme a binary number: ")` is me barking, “Oi, binary, sharpish!” Then `scanf("%s",bin)` grabs it as a string and shoves it in `bin`. `%s` for strings, no faffy numbers here!
5. `binaryToDecimal` funtion 6. chucking it out: `printf` and check 7. `return 0` to scarper And that’s yer lot! A cracking lil program to turn binary into decimal. Faff with it—chuck in daft binaries, tweak it to say “proper number” if ya fancy. First time I ran this, I put “101” and got 5—bang on, eh! You’re a star already, mate—keep smashing it! 😊
Here’s the guts! `binaryToDecimal(char bin[])` flips it. `decimal=0` starts the tally, `power=0` tracks the position weight. `len=strlen(bin)` gets the length, then `for(int i=len-1;i>=0;i--)` goes right to left—binary’s read that way, innit! If `bin[i]=='1'`, `decimal+=1<
`int result=binaryToDecimal(bin)` does the flip, then if `result==-1`, `printf` yells “ain’t proper binary!” Else, `printf("%s in decimal is %d\n",bin,result)` shows it off. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!
That `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.