944,127 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3438
  • C RSS
Sep 30th, 2009
0

Integer To ASCII convert problem

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
shahab.burki is offline Offline
41 posts
since Feb 2009
Sep 30th, 2009
-2

Re: Integer To ASCII convert problem

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
}
}
Reputation Points: 19
Solved Threads: 2
Junior Poster in Training
Quick_Silver69 is offline Offline
60 posts
since Aug 2009
Sep 30th, 2009
0

Re: Integer To ASCII convert problem

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?
Reputation Points: 11
Solved Threads: 1
Light Poster
manutm is offline Offline
30 posts
since Jul 2009
Oct 1st, 2009
0

Re: Integer To ASCII convert problem

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
Reputation Points: 10
Solved Threads: 0
Light Poster
shahab.burki is offline Offline
41 posts
since Feb 2009
Oct 1st, 2009
0

Re: Integer To ASCII convert problem

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?
Reputation Points: 11
Solved Threads: 1
Light Poster
manutm is offline Offline
30 posts
since Jul 2009
Oct 2nd, 2009
0

Re: Integer To ASCII convert problem

>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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Oct 4th, 2009
0

Re: Integer To ASCII convert problem

Quote ...
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.

Quote ...
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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Oct 5th, 2009
2

Re: Integer To ASCII convert problem

I've to admit that the union approach was just a bad and clunky advice.
But I just can't get this:
Quote ...
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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Oct 5th, 2009
2

Re: Integer To ASCII convert problem

Click to Expand / Collapse  Quote originally posted by tux4life ...
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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Oct 5th, 2009
1

Re: Integer To ASCII convert problem

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
Reputation Points: 11
Solved Threads: 1
Light Poster
manutm is offline Offline
30 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: bnary search
Next Thread in C Forum Timeline: How to use graphics effectively?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC