<$BlogRSDURL$>
I rock, you suck
Donate to my Beer Fund


If you enjoyed/hated my blog/have money to burn/are crazy, why not give me your money?
All you have to do is click on the button above.
No? Well, go on to the posts below, then, you prick.


Thursday, October 21, 2004
 
Beer Fund and itoa

Although I do not feel the need to explain myself to you lesser beings, I shall deign to divulge the reasons behind my putting a "donate" button on my magnificent blog. Yes, I am that great and cool a guy. No, I am not selling out. How can I be selling out when, so far, none of the third-party stuff I put on my blog makes me money? I even removed enetation, haloscan and tagboard because one of them was lame enough to have popups. The only ad on my blog is from webcounter, which feeds my ego, and I'm sure we can all agree that feeding my ego is a Good Thing. Anyway, here are the reasons.

1) It's meant to be funny, you dumb fucks.
2) I do realise that in all probability, no one will actually give me any money, but hey, what the hell. There are all sorts of crazy people on the Internet.
3) I like money.
4) Beer costs money.
5) So do condoms.


And there you have it, 5 totally awesome reasons for me to put a "donate" button on my blog, you dickheads. Now, stop bitching about it and give me your money already.




Caution: Non-geeks, please stop reading this post.

On a totally unrelated note, recently I was writing a program in C++ and I needed a function that does the reverse of atoi. As any programmer who happens to be reading this probably already knows, atoi converts a character ("1", "2", "3", "4", etc) to an integer value (1, 2, 3, 4, etc). I needed a function that converts an integer to its string representation. I googled for it, of course, and apparently there was already an "itoa" function in the header file < cstdlib>. I included it, but unfortunately, the g++ compiler still told me that the function did not exist. I googled some more, and apparently I had neglected to stick this in the code:

using std::itoa;

I did that, but the compiler still said nay. I was getting pissed by then, and I did not have the time to putter around looking for the function. I therefore wrote my own "itoa" function. Of course, it's a really simple function that any programmer will probably be able to write, but why waste time if you don't have to? So here it is, in case anyone needs to use it.


#include < iostream>
#include < string>

using namespace std;
string itoa(string, int);


main(){
int someInt;
string str="";

cout<<"Please enter an integer value: ";
cin>>someInt;
cout<<"You have entered " + itoa(str, someInt)< < endl;

}

string itoa(string str, int ref)
{
int remainder;

bool negative=false;

if(ref<0){
ref = ref * -1;
negative = true;
}


while(ref>=10){
remainder = ref % 10;
switch(remainder){
case 1: str = "1" + str;
break;
case 2: str = "2" + str;
break;
case 3: str = "3" + str;
break;
case 4: str = "4" + str;
break;
case 5: str = "5" + str;
break;
case 6: str = "6" + str;
break;
case 7: str = "7" + str;
break;
case 8: str = "8" + str;
break;
case 9: str = "9" + str;
break;
default: if(ref>=10)
str = "0" + str;
break;
}
ref = ref/10;
}
switch(ref){
case 1: str = "1" + str;
break;
case 2: str = "2" + str;
break;
case 3: str = "3" + str;
break;
case 4: str = "4" + str;
break;
case 5: str = "5" + str;
break;
case 6: str = "6" + str;
break;
case 7: str = "7" + str;
break;
case 8: str = "8" + str;
break;
case 9: str = "9" + str;
break;
}

if(str.length() == 0)
str = "0";
if(negative)
str = "-" + str;

return str;
}


This program basically takes an integer you enter as the input and outputs the string representation of that integer.
 
Comments:
Here is a version of your function that takes a char * if you are interested:

void itoa(char * str, int ref)
{
int remainder;

int index = 0;
int tempIndex;

if(ref != 0)
{

if(ref<0)
{
ref = ref * -1;
str[index] = '-';
++index;
}

int temp = ref;
int power = 0;


while(temp >= 10)
{
++power;
temp /= 10;
}

index += power;
tempIndex = index + 1;

while(power >= 0)
{
remainder = ref % 10;
switch(remainder)
{
case 1: str[index] = '1';
break;
case 2: str[index] = '2';
break;
case 3: str[index] = '3';
break;
case 4: str[index] = '4';
break;
case 5: str[index] = '5';
break;
case 6: str[index] = '6';
break;
case 7: str[index] = '7';
break;
case 8: str[index] = '8';
break;
case 9: str[index] = '9';
break;
default: if(ref>=10)
str[index] = '0';
break;
}
ref = ref/10;
--index;
--power;
}
}
else
{
str[0] = 0;
tempIndex = 1;

}

str[tempIndex] = '\0';

}
I needed one that took a char * for my assignment, and I saw yours, so I thought I would convert it :P
 
Post a Comment
Back

Links to this post:

Create a Link

Laughing at the cosmic gag reel since March '04!

Links
L.E.W.D (click to know more):


Fred And Phil

Fiction

Hot Babe Blogs:

Other Blogs (that are not quite as good as mine):


Unforgettables:

Recent Posts:

ARCHIVES

To Those Who Wish To Link Me:

Due to the fact that my ego is a humongous, bloated monstrousity, it is not highly unlikely that I wouldn't say no to your linking my blog, so there is no need to ask me.


Winners of Adrian Coolness Points:

The Feisty Bitch: For reasons best known to ourselves. (1)
The Feisty Bitch: For getting featured on the Sunday Times (2)
Adri: For being geeky enough to write recursive prose. (1)
Sheena: For really, really liking my blog. (1)
Sheena: For the use of her finger. (2)
Sheena: For getting on the Straits Times. (3)
Ivan: For referring to me as one of "Singapore's leading bloggers". (1)
Ivan: For coming up with the PubicLicezilla idea. (2)
The Big Fuck: For being such a big fuck. (1)
The Big Fuck: For making the miniature Badge of Lewdness. (2)
Anonymous fan: For making a cool finger. (1)
Celly: For appreciating the genius behind the Pagan Bible here. (1)
Icebreeze: For being wise enough to flatter me. (1)
Barffie: For furthering the LEWD cause by appearing in the papers. (1)
Blinkymummy: For furthering the LEWD cause by appearing in TWO papers within the space of two days, fuckin' A! (2)
Jess: For being observant enough to spot the similarity between Lewdites and Luddites. You rock, babe. (1)
Jiameei: For being my champion against anonymous hecklers. (1)


Powered by Blogger

Ablewise.com Free Classifieds - The Online Classifieds Solutions (TM)




free dating sites

Get custom programming done at GetACoder.com!