String to integer to ascii

Reply

Join Date: May 2004
Posts: 1
Reputation: Merrissey is an unknown quantity at this point 
Solved Threads: 0
Merrissey Merrissey is offline Offline
Newbie Poster

String to integer to ascii

 
0
  #1
May 13th, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: String to integer to ascii

 
0
  #2
May 13th, 2004
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. */
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: String to integer to ascii

 
0
  #3
May 13th, 2004
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. */
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: String to integer to ascii

 
0
  #4
May 13th, 2004
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. */
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 15
Reputation: tlee is an unknown quantity at this point 
Solved Threads: 0
tlee tlee is offline Offline
Newbie Poster

Re: String to integer to ascii

 
0
  #5
May 13th, 2004
Hi Dave,
What is the %n used for in sscanf(ptr, "%3d%n", &value, &i)? Thanks.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: String to integer to ascii

 
0
  #6
May 13th, 2004
>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.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: String to integer to ascii

 
0
  #7
May 24th, 2004
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
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1
Reputation: Msnart is an unknown quantity at this point 
Solved Threads: 0
Msnart Msnart is offline Offline
Newbie Poster

Re: String to integer to ascii

 
0
  #8
Jun 14th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: String to integer to ascii

 
0
  #9
Jun 14th, 2005
Don't cast, use atoi.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC