| | |
String to integer to ascii
![]() |
One way:
C Syntax (Toggle Plain Text)
#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 */
Another way:
C Syntax (Toggle Plain Text)
#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 */
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).
Assuming your system is ASCII, it's quite easy to convert the integer value to ASCII (do nothing).
C Syntax (Toggle Plain Text)
#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' */
>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.
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
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
•
•
Join Date: Jun 2005
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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
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?
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
![]() |
Similar Threads
- String/Integer C++ Question (C++)
- to convert string to integer value (Legacy and Other Languages)
- String to integer conversion (C)
- Convert a string to an integer (PHP)
- Converting String to Integer help (C++)
Other Threads in the C Forum
- Previous Thread: Global Buffer Help !!!!!!!!!
- Next Thread: for loop error
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic feet fgets file fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue mysql number odf opendocumentformat owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






