Monday, March 13, 2006

C++: Using ATOI for ASCII to Integer, and sprintf for Integer to String Conversions

There was someone who asked this question on a forum I read (students looking for help with their homework… what slackers, but I can relate since I was there once too):

I have a function X that needs to take in two parameters, and integer and a character. The function needs to replace the hundredths place in the integer with the value in the character. How can I do this?

The solution is actually pretty simple and requires a little math. I was convinced that there was some slick way to do this with bit masking, but I couldn’t figure it out. (Note: if someone has a solution for that, don’t hesitate to post it).

//Convert hundredths place to whatever single character digit
//is passed in
long hundredthsConversion(long n, char c)
{
//create an array of 2 characters, and initialize the first
//character to the value passed in to the function, and the
//second one to NULL
char temp[2] = {c, 0};

//integers to hold the converted ATOI result, and the value of
//N after stripping out the hundredths place
long digitConversion, n_strip_hundreds_digit;

//Convert this single digit numeral to a number, then multiply
//by 100. This will be added to n with the hundred digit striped
//out
digitConversion = (atoi(temp) * 100);

//Divide n by 100, than divide that result by 10, giving the number. Multiple that by 100 to give the value in the hundredths without
//values in the > 100 or the < 100 places
n_strip_hundreds_digit = (n - (( (n / 100) % 10) * 100));

//return the value without the hundredths and add in the single
//character for the hundredths place.
return n_strip_hundreds_digit + digitConversion;
}

I originally had the above as a single line equation, but separated it for clarity.

Alternatively, you could change the value in N to a string, and replace the 3rd to last value with the value in C, but for no good technical reason I don’t really like the idea off converting from one type back and forth. Also, Linux and *BSD do not have a native itoa function, so you would need to use sprintf to convert from an integer to a string. If I did use this method, however, it might look something like this (Note: comments striped for brevity, and function elongated for clarity of steps):

long hundredthsConversion(long n, char c)
{
char buffer[255];
int I;

for (I = 0; I < 255; I++)
buffer[I] = 0;
sprintf(buffer, “%d”, n);

I = strlen(buffer);

switch(i)
{
case 1:
buffer[2] = buffer[0];
buffer[0] = c;
buffer[1] = ‘0’;
break;
case 2:
buffer[2] = buffer[1];
buffer[1] = buffer[0];
buffer[0] = c;
break;
default:
buffer[I – 3] = c;
break;
}

return atol(buffer);
}


Both functions compiled under the GNU C++ Compiler using only iostream and the standard namespace. Under C, you would need to include the appropriate headers for atoi and sprintf.

2 comments:

John Ward said...

Dear Anonymous,

Thank you for your words of encouragement. Its readers like you that motivate me to wake up every morning and write articles. Kind words like yours keep me going strong. I am glad I could help, and keep on coding buddy.

John

Anonymous said...

Why the hell do you even bother answer.. You shud thak john for take some of hes time to help us with learning... Im learning c++ at the moment and without the help from people like john, i would not learn much... Thaks for the article.