| | |
converting int to char in C
Thread Solved |
•
•
Join Date: Sep 2006
Posts: 2
Reputation:
Solved Threads: 0
Hi,
I am trying to convert a string length to a char and store it in an array. For some reason, even though its not giving me any error, when I try to print the array out, the place where I store the strlen is blank. Here's my code:
The output I get is as follows:
15, wwwgooglecom
where as I am expecting something like:
15, 3www6google3com
Please note that the strlen(buf) gives the expected result. It's just that the numbers are not printed. Any help would be appreciated. Thanks.
-lilpro
I am trying to convert a string length to a char and store it in an array. For some reason, even though its not giving me any error, when I try to print the array out, the place where I store the strlen is blank. Here's my code:
C Syntax (Toggle Plain Text)
int main () { char host[] = "<a rel="nofollow" class="t" href="http://www.google.com" target="_blank">www.google.com</a>"; char delim[]="."; int i=0, index=0; char *token; char buf[100]; unsigned char len_char; int length; token = strtok(host, delim); while (token != NULL) { length = strlen(token); len_char = (unsigned char) length; printf("%c", len_char); //<----this does not print anything too!! buf[index++] = length; //add the length as the beginning of a string //printf("here%c", buf[index++]); for(i=0; i<length; i++) { buf[index++] = token[i]; //printf("%c", buf[index++]); } token = strtok(NULL, delim); } printf("%d, %s\n", strlen(buf), buf); return 0; }
The output I get is as follows:
15, wwwgooglecom
where as I am expecting something like:
15, 3www6google3com
Please note that the strlen(buf) gives the expected result. It's just that the numbers are not printed. Any help would be appreciated. Thanks.
-lilpro
you can't just convert the int by typcasting because (1) an int is a non-printable value and (2) the value of the integer may not be in the range '0' to '9' (single digit value). The safest way is to use sprintf() to format the string with an integer. I would suggest creating another temp char buffer to format the integer.
Of course, if you absolutely know the integer is a single digit, then just add '0' to it.
C Syntax (Toggle Plain Text)
char tmpbuf[40]; sprintf(tmpbuf,"%d", length); // now copy tmpbuf into buffer just like you // do the other strings
Of course, if you absolutely know the integer is a single digit, then just add '0' to it.
C Syntax (Toggle Plain Text)
len_char = (unsigned char) (length + '0');
Last edited by Ancient Dragon; Sep 20th, 2006 at 8:12 pm.
>printf("%c", len_char); //<----this does not print anything too!!
What did you expect it to print? You realize that the %c format modifier takes the integer value you supply and prints the character representation of that integer, right? In other words, it's not going to print '3'...ever. As long as the lengths are single digits, you can say len_char + '0' and get the correct representation, but once the length exceeds 9, you're SOL with this method.
What did you expect it to print? You realize that the %c format modifier takes the integer value you supply and prints the character representation of that integer, right? In other words, it's not going to print '3'...ever. As long as the lengths are single digits, you can say len_char + '0' and get the correct representation, but once the length exceeds 9, you're SOL with this method.
I'm here to prove you wrong.
•
•
Join Date: Aug 2006
Posts: 29
Reputation:
Solved Threads: 1
You can refer the following code if you would like to....
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<stdlib.h> #include<string.h> int main () { char host[] = "<a rel="nofollow" class="t" href="http://www.google.com" target="_blank">www.google.com</a>"; char delim[]="."; int i=0, index=0; char *token; char buf[100]; char len_char; int length; token = strtok(host, delim); buf[0]='\0'; while (token != NULL) { length = strlen(token); sprintf(buf+strlen(buf), "%d", length); sprintf(buf+strlen(buf), "%s", token); token = strtok(NULL, delim); } printf("%d, %s\n", strlen(buf), buf); return 0; }
![]() |
Similar Threads
- how convert int to char? (C++)
- Reading CSV in VC++ (C++)
- Converting Int to Const Char (C)
Other Threads in the C Forum
- Previous Thread: Converting a String into an integer array
- Next Thread: Regarding getpid function
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi







