converting int to char in C

Thread Solved

Join Date: Sep 2006
Posts: 2
Reputation: lilprogrammer is an unknown quantity at this point 
Solved Threads: 0
lilprogrammer lilprogrammer is offline Offline
Newbie Poster

converting int to char in C

 
1
  #1
Sep 20th, 2006
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:

  1. int main () {
  2. char host[] = "<a rel="nofollow" class="t" href="http://www.google.com" target="_blank">www.google.com</a>";
  3. char delim[]=".";
  4. int i=0, index=0;
  5. char *token;
  6. char buf[100];
  7. unsigned char len_char;
  8. int length;
  9.  
  10. token = strtok(host, delim);
  11. while (token != NULL) {
  12. length = strlen(token);
  13. len_char = (unsigned char) length;
  14. printf("%c", len_char); //<----this does not print anything too!!
  15. buf[index++] = length; //add the length as the beginning of a string
  16. //printf("here%c", buf[index++]);
  17. for(i=0; i<length; i++) {
  18. buf[index++] = token[i];
  19. //printf("%c", buf[index++]);
  20. }
  21. token = strtok(NULL, delim);
  22. }
  23. printf("%d, %s\n", strlen(buf), buf);
  24. return 0;
  25. }

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,398
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: converting int to char in C

 
0
  #2
Sep 20th, 2006
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.
  1. char tmpbuf[40];
  2. sprintf(tmpbuf,"%d", length);
  3. // now copy tmpbuf into buffer just like you
  4. // do the other strings

Of course, if you absolutely know the integer is a single digit, then just add '0' to it.
  1. len_char = (unsigned char) (length + '0');
Last edited by Ancient Dragon; Sep 20th, 2006 at 8:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: converting int to char in C

 
3
  #3
Sep 20th, 2006
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 2
Reputation: lilprogrammer is an unknown quantity at this point 
Solved Threads: 0
lilprogrammer lilprogrammer is offline Offline
Newbie Poster

Re: converting int to char in C

 
1
  #4
Sep 21st, 2006
Thanks a lot. That helped. Its been a long time since I programmed in C and my concepts are a bit rusty now...but I am learning it again

-lilpro
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 29
Reputation: Iqbal_h_a is an unknown quantity at this point 
Solved Threads: 1
Iqbal_h_a Iqbal_h_a is offline Offline
Light Poster

Re: converting int to char in C

 
1
  #5
Sep 21st, 2006
You can refer the following code if you would like to....


  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5.  
  6. int main () {
  7. char host[] = "<a rel="nofollow" class="t" href="http://www.google.com" target="_blank">www.google.com</a>";
  8. char delim[]=".";
  9. int i=0, index=0;
  10. char *token;
  11. char buf[100];
  12. char len_char;
  13. int length;
  14. token = strtok(host, delim);
  15. buf[0]='\0';
  16. while (token != NULL) {
  17. length = strlen(token);
  18. sprintf(buf+strlen(buf), "%d", length);
  19. sprintf(buf+strlen(buf), "%s", token);
  20. token = strtok(NULL, delim);
  21. }
  22. printf("%d, %s\n", strlen(buf), buf);
  23. return 0;
  24. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: converting int to char in C

 
0
  #6
Sep 21st, 2006
Originally Posted by Iqbal_h_a View Post
You can refer the following code if you would like to....
For what you are using #include<stdlib.h>? You dont need that.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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