943,469 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 63341
  • C RSS
May 13th, 2004
0

String to integer to ascii

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Merrissey is offline Offline
1 posts
since May 2004
May 13th, 2004
0

Re: String to integer to ascii

One way:
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. const char text[] = "097102099105110";
  6. int i, value[5];
  7. if ( sscanf(text, "%3d%3d%3d%3d%3d", &value[0],
  8. &value[1], &value[2], &value[3], &value[4]) == 5 )
  9. {
  10. for ( i = 0; i < 5; ++i )
  11. {
  12. printf("value[%d] = %d\n", i, value[i]);
  13. }
  14. }
  15. return 0;
  16. }
  17.  
  18. /* my output
  19. value[0] = 97
  20. value[1] = 102
  21. value[2] = 99
  22. value[3] = 105
  23. value[4] = 110
  24. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 13th, 2004
0

Re: String to integer to ascii

Another way:
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. const char text[] = "097102099105110", *ptr = text;
  6. int i, value;
  7. while ( sscanf(ptr, "%3d%n", &value, &i) == 1 )
  8. {
  9. printf("value = %d\n", value);
  10. ptr += i;
  11. }
  12. return 0;
  13. }
  14.  
  15. /* my output
  16. value = 97
  17. value = 102
  18. value = 99
  19. value = 105
  20. value = 110
  21. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 13th, 2004
0

Re: String to integer to ascii

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).
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. const char text[] = "097102099105110", *ptr = text;
  6. int i, value;
  7. while ( sscanf(ptr, "%3d%n", &value, &i) == 1 )
  8. {
  9. printf("value = %3d = '%c'\n", value, value);
  10. ptr += i;
  11. }
  12. return 0;
  13. }
  14.  
  15. /* my output
  16. value = 97 = 'a'
  17. value = 102 = 'f'
  18. value = 99 = 'c'
  19. value = 105 = 'i'
  20. value = 110 = 'n'
  21. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 13th, 2004
0

Re: String to integer to ascii

Hi Dave,
What is the %n used for in sscanf(ptr, "%3d%n", &value, &i)? Thanks.
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004
May 13th, 2004
0

Re: String to integer to ascii

>What is the %n used for in sscanf(ptr, "%3d%n", &value, &i)?
Quote ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 24th, 2004
0

Re: String to integer to ascii

char chr = 'a';

cout<<chr; //will show the char
cout<<(int)chr; //will show the ancii code

chr = 232;

cout<<chr; //will show what ever 232 repesents
cout<<(int)chr; //will show the ancii code i.e 232
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 14th, 2005
0

Re: String to integer to ascii

Quote originally posted by FireNet ...
char chr = 'a';

cout<<chr; //will show the char
cout<<(int)chr; //will show the ancii code

chr = 232;

cout<<chr; //will show what ever 232 repesents
cout<<(int)chr; //will show the ancii code i.e 232
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Msnart is offline Offline
1 posts
since Jun 2005
Jun 14th, 2005
0

Re: String to integer to ascii

Don't cast, use atoi.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Global Buffer Help !!!!!!!!!
Next Thread in C Forum Timeline: for loop error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC