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
}
}
Quick_Silver69
Junior Poster in Training
60 posts since Aug 2009
Reputation Points: 19
Solved Threads: 2
>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:
union foo {
int port;
char asc[ sizeof(int) ];
} a;
Then further in your code you can set the port number (and print the representing ASCII characters) like this:
a.port = 5230; // set port number
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.
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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 thereasc 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.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
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.
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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.
#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;
}
Which - I would never have to :P It's just a matter of personal preference in the end, I've never liked the format ofsprintf and functions like that so much, which cause me more trouble altogether than itoa does.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129