| | |
Integer To ASCII convert problem
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 22
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2009
Posts: 18
Reputation:
Solved Threads: 1
For instance
This will print your port value (as an ASCII string.)
Is it what you want to do?
C Syntax (Toggle Plain Text)
printf ( "My port = %d \n", my_port );
Is it what you want to do?
>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:
Then further in your code you can set the port number (and print the representing ASCII characters) like this:
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.
You could use a union for this purpose:
C Syntax (Toggle Plain Text)
union foo { int port; char asc[ sizeof(int) ]; } a;
C Syntax (Toggle Plain Text)
a.port = 5230; // set port number printf("In ASCII: %s", a.asc); // will most likely be unreadable
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."
•
•
Join Date: Mar 2008
Posts: 1,490
Reputation:
Solved Threads: 123
•
•
•
•
You could use a union for this purpose
•
•
•
•
Don't use itoa
I need pageviews! most fun profile ever :)
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.
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.
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."
•
•
Join Date: Mar 2008
Posts: 1,490
Reputation:
Solved Threads: 123
•
•
•
•
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.
C Syntax (Toggle Plain Text)
#define itoa my_itoa char *my_itoa(int _Val, char *_DstBuf, int radix) { static const char vals[] = "0123456789abcdef"; int c = _Val, d = 1, dif, i; int _signed = _Val < 0; while (c /= radix) ++d; i = d + _signed; if ( _signed ) _DstBuf[0] = '-'; for (; i - _signed; --i) { dif = (_Val % radix); _DstBuf[i - 1] = (_Val /= radix, vals[(dif < 0 ? -dif : dif)]); } _DstBuf[d + _signed] = 0; return _DstBuf; }
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 :)
•
•
Join Date: Jul 2009
Posts: 18
Reputation:
Solved Threads: 1
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
![]() |
Similar Threads
- How to convert string contain Hex data into integer (C)
- how to convert string array in integer array (C)
- convert integer to array of character??? (C++)
- Integer to string then 2 listbox (Visual Basic 4 / 5 / 6)
- vb.net NEWBIE!! Triangle problem!! (VB.NET)
- String to integer to ascii (C)
- Access Violation (Segmentation Fault) + atol (C++)
- help neede about converting characters (C++)
Other Threads in the C Forum
- Previous Thread: Finding prime factors and distinct prime factors
- Next Thread: How to use graphics effectively?
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windowsapi






