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?

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
}
}

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?

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

For instance

printf ( "My port = %d \n", my_port );

This will print your port value (as an ASCII string.)
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:

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.

Edited 7 Years Ago by mvmalderen: n/a

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

Comments

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 of sprintf and functions like that so much, which cause me more trouble altogether than itoa does.

Comments
Heh, nice code :)
This article has been dead for over six months. Start a new discussion instead.