Integer To ASCII convert problem

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 22
Reputation: shahab.burki is an unknown quantity at this point 
Solved Threads: 0
shahab.burki shahab.burki is offline Offline
Newbie Poster

Integer To ASCII convert problem

 
0
  #1
Sep 30th, 2009
Hi,

I am doing network programming in C on Ubuntu using gcc. I am facing the problem how to convert an Integer Type to ASCII Type. I have used itoa() and sprintf(), but wasn't that fritful. Can any one suggest a suitable solution to it?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 59
Reputation: Quick_Silver69 is an unknown quantity at this point 
Solved Threads: 2
Quick_Silver69 Quick_Silver69 is offline Offline
Banned

Re: Integer To ASCII convert problem

 
-2
  #2
Sep 30th, 2009
Hi there,

Try this & check...


public class Main {
public static void main(String[] args) throws Exception {

char c = 'A';
int i = (int) c; // i == 65 DECIMAL
}
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 18
Reputation: manutm is an unknown quantity at this point 
Solved Threads: 1
manutm manutm is offline Offline
Newbie Poster

Re: Integer To ASCII convert problem

 
0
  #3
Sep 30th, 2009
ascii range is 1 to 127, whereas int can be (depending on system and compiler) up to 4 bytes, that is (256)power4. So any ascii char can fit in an int but the inverse is false. But what do you want to do exactly?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 22
Reputation: shahab.burki is an unknown quantity at this point 
Solved Threads: 0
shahab.burki shahab.burki is offline Offline
Newbie Poster

Re: Integer To ASCII convert problem

 
0
  #4
Oct 1st, 2009
Well "Quick Silver", I am asking about the solution in C language not in JAVA.. Dear MANUTM, I want to translate a TCP port into ASCII. The code for example is,

int my_port=ntohs(my_addr.sinport);

it will return a number for example 9999, which will be an Integer. Now how to convert this 9999 into ASCII?

Shahab
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 18
Reputation: manutm is an unknown quantity at this point 
Solved Threads: 1
manutm manutm is offline Offline
Newbie Poster

Re: Integer To ASCII convert problem

 
0
  #5
Oct 1st, 2009
For instance
  1. printf ( "My port = %d \n", my_port );
This will print your port value (as an ASCII string.)
Is it what you want to do?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Integer To ASCII convert problem

 
0
  #6
Oct 2nd, 2009
>it will return a number for example 9999, which will be an Integer. Now how to convert this 9999 into ASCII?

You could use a union for this purpose:
  1. union foo {
  2. int port;
  3. char asc[ sizeof(int) ];
  4. } a;
Then further in your code you can set the port number (and print the representing ASCII characters) like this:
  1. a.port = 5230; // set port number
  2. printf("In ASCII: %s", a.asc); // will most likely be unreadable
Or do you just want to convert the port number to a string, like this:
5000 -> "5000" ?
In that case you'll probably want to use sprintf (don't forget to check out the example at the bottom of the page).

Don't use itoa, though I've never come across a compiler which didn't support it, it's still an unportable function according to the standard.
Last edited by tux4life; Oct 2nd, 2009 at 8:56 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,490
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Integer To ASCII convert problem

 
0
  #7
Oct 4th, 2009
You could use a union for this purpose
Using a union like that wouldn't technically be making it an ASCII string, ASCII represents text, and there asc is just pointing to some bytes of another data type.

Don't use itoa
It's doubtful that any decent compiler wouldn't have this included, so I personally wouldn't hesitate to use it, but it's the OP's choice if he decides to use it or not.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Integer To ASCII convert problem

 
2
  #8
Oct 5th, 2009
I've to admit that the union approach was just a bad and clunky advice.
But I just can't get this:
It's doubtful that any decent compiler wouldn't have this included, so I personally wouldn't hesitate to use it, but it's the OP's choice if he decides to use it or not.
Why would someone use an unportable function to do the job when there's a portable function which will also help you to achieve the same job?
It's of course his choice, but I'd know which one to choose.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,490
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Integer To ASCII convert problem

 
2
  #9
Oct 5th, 2009
Originally Posted by tux4life View Post
I've to admit that the union approach was just a bad and clunky advice.
But I just can't get this:
Why would someone use an unportable function to do the job when there's a portable function which will also help you to achieve the same job?
It's of course his choice, but I'd know which one to choose.
Because, I like it, it's a simple function which has one purpose, to convert an integer to string (with a radix). If anybody complained that they couldn't compile my code on a crap compiler, I wouldn't care. Also, if portability was a big issue for me, I could just add this to the start of my code.
  1. #define itoa my_itoa
  2. char *my_itoa(int _Val, char *_DstBuf, int radix) {
  3. static const char vals[] = "0123456789abcdef";
  4. int c = _Val, d = 1, dif, i;
  5. int _signed = _Val < 0;
  6.  
  7. while (c /= radix) ++d;
  8.  
  9. i = d + _signed;
  10. if ( _signed ) _DstBuf[0] = '-';
  11.  
  12. for (; i - _signed; --i) {
  13. dif = (_Val % radix);
  14. _DstBuf[i - 1] = (_Val /= radix, vals[(dif < 0 ? -dif : dif)]);
  15. }
  16.  
  17. _DstBuf[d + _signed] = 0;
  18. return _DstBuf;
  19. }
Which - I would never have to It's just a matter of personal preference in the end, I've never liked the format of sprintf and functions like that so much, which cause me more trouble altogether than itoa does.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 18
Reputation: manutm is an unknown quantity at this point 
Solved Threads: 1
manutm manutm is offline Offline
Newbie Poster

Re: Integer To ASCII convert problem

 
1
  #10
Oct 5th, 2009
So you can use sprintf or snprintf which prevents overflow or itoa which is valid in c++ but is not ansii c, or your own stuff. But I don't know any other function that do the job. Seems this have been discussed already: http://www.daniweb.com/forums/thread11049.html
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC