954,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String to integer to ascii

Lets say I have the following as a string:

097102099105110

What I want to do is find the ascii character represented by every set of 3 digits. So the ascii character of 97, then 102, 99, 105 and then 110.

Could anybody point me in the right direction here?

Merrissey
Newbie Poster
1 post since May 2004
Reputation Points: 10
Solved Threads: 0
 

One way:

#include <stdio.h>

int main(void)
{
   const char text[] = "097102099105110";
   int i, value[5];
   if ( sscanf(text, "%3d%3d%3d%3d%3d", &value[0],
			   &value[1], &value[2], &value[3], &value[4]) == 5 )
   {
	  for ( i = 0; i < 5; ++i )
	  {
		 printf("value[%d] = %d\n", i, value[i]);
	  }
   }
   return 0;
}

/* my output
value[0] = 97
value[1] = 102
value[2] = 99
value[3] = 105
value[4] = 110
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Another way:

#include <stdio.h>

int main(void)
{
   const char text[] = "097102099105110", *ptr = text;
   int i, value;
   while ( sscanf(ptr, "%3d%n", &value, &i) == 1 )
   {
	  printf("value = %d\n", value);
	  ptr += i;
   }
   return 0;
}

/* my output
value = 97
value = 102
value = 99
value = 105
value = 110
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Sorry for multiple posts -- I just now noticed the ASCII part.

Assuming your system is ASCII, it's quite easy to convert the integer value to ASCII (do nothing).

#include <stdio.h>
 
int main(void)
{
   const char text[] = "097102099105110", *ptr = text;
   int i, value;
   while ( sscanf(ptr, "%3d%n", &value, &i) == 1 )
   {
   printf("value = %3d = '%c'\n", value, value);
   ptr += i;
   }
   return 0;
}
 
/* my output
value =  97 = 'a'
value = 102 = 'f'
value =  99 = 'c'
value = 105 = 'i'
value = 110 = 'n'
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Hi Dave,
What is the %n used for in sscanf(ptr, "%3d%n", &value, &i)? Thanks.

tlee
Newbie Poster
15 posts since May 2004
Reputation Points: 21
Solved Threads: 0
 

>What is the %n used for in sscanf(ptr, "%3d%n", &value, &i)? No input is consumed. The corresponding argument shall be a pointer to signed integer into which is to be written the number of characters read from the input stream so far by this call to the fscanf function. Execution of a %n directive does not increment the assignment count returned at the completion of execution of the fscanf function. No argument is converted, but one is consumed. If the conversion specification includes an assignment suppressing character or a field width, the behavior is undefined.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

char chr = 'a';

cout<

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

char chr = 'a';

cout<

So if i had a character array (string):

tempChar[10] = '44\0abcdefg';

How do I convert that string into the integer 44? I've tried:

anInt = (int)tempChar;

hoping that it would take the string up to the null character \0 and turn it into an int, but no dice. Any suggestions?

Msnart
Newbie Poster
1 post since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Don't cast, use atoi .

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You