Convert an integer value to ASCII character

X

Xianwen Chen

Hi there,

I'm trying to convert an integer valune to ASCII character. I have
found that I can print an integer value to a character by
printf("%c", intA);
What I am looking for is something like
charA = somefunction(intA)

Some hints, please?

Regards,

Xianwen
 
S

Stefan Ram

Xianwen Chen said:
I'm trying to convert an integer valune to ASCII character.

This is not specific enough. Do you want to convert
a value of type »int« into a value of type »char«?

Or, otherwise, what else?
charA = somefunction(intA)

What about

char somefunction( int const i ){ return i; }
int main( void ){ return somefunction( 0 ); }

?

Do you refer to ASCII 1963 or ASCII 1968, and what does
ASCII has to do with converting from char to int?
 
D

Dr Nick

Xianwen Chen said:
Hi there,

I'm trying to convert an integer valune to ASCII character. I have
found that I can print an integer value to a character by
printf("%c", intA);
What I am looking for is something like
charA = somefunction(intA)

Some hints, please?

If you have the right value in there (so the printf is working) you just
need to assign the value.

charA = intA will do the job for you.

Note that there's no actual guarantee that C uses ASCII (and I've
programmed in C on machines that don't) but if you're prepared to live
with this while you're learning it will work.

The reason this works is that in C, characters are just small integers
holding the code (not necessarily, but often ASCII) for the character.
 
S

Stefan Ram

Do you refer to ASCII 1963 or ASCII 1968, and what does
ASCII has to do with converting from char to int?

(this should have been: »... from int to char?«)

I have thought again about this:

First, I assume an execution environment whose execution
character (5.2.1p1) set is ASCII:

6.4.4.4p10: »The value of an integer character constant
containing a single character that maps to a single-byte
execution character is the numerical value of the
representation of the mapped character interpreted as an
integer.«

7.19.2p1 ... p2: »Input and output (...) are mapped into
logical data streams. A text stream is an ordered sequence
of characters (...)«

7.19.7.3p2: »The fputc function writes the character
specified (...) to the output stream pointed to by stream«.

So, when fputc is used to write to a text stream on an
execution environment whose execution character (5.2.1p1)
set is ASCII, the value written should be converted to ASCII
using an implementation-defined mapping (that should be
ASCII in this case).

On a system without another execution character set than
ASCII, one can implement the mapping from the execution
character set to ASCII oneself by a table and then write the
ASCII characters into a binary stream out as a sequence of
octets.
 
G

Geoff

Hi there,

I'm trying to convert an integer valune to ASCII character. I have
found that I can print an integer value to a character by
printf("%c", intA);
What I am looking for is something like
charA = somefunction(intA)

Some hints, please?

Regards,

Xianwen

This sounds like a homework problem.

It seems that what you are looking for is the reverse of the atoi()
function found in stdlib.h, however atoi() converts an ASCII string
into an integer, not just a character. Standard C doesn't provide the
reverse for some reason. One would have thought it would have been
included for orthogonality sake. Microsoft provides the _itoa()
function in their libraries but this is an extension.

K&R implemented itoa() in both the 1st and 2nd edition of "The C
Programming Language" but they were both flawed in some respects.

The standard-conformant method these days is to use sprintf().
 
S

Stefan Ram

6.4.4.4p10: »The value of an integer character constant
containing a single character that maps to a single-byte
execution character is the numerical value of the
representation of the mapped character interpreted as an
integer.«

6.4.4.4p2: »An integer character constant is a sequence of
one or more multibyte characters enclosed in single-quotes,
as in 'x'.«

Thus, 'x' is an integer character constant. It contains a
single character.

I guess, it maps to the single-byte execution character »x«.
But why?

5.1.1.2p1,5. »Each source character set member and escape
sequence in character constants and string literals is
converted to the corresponding member of the execution
character set«

But where is this correspondence being defined? This seems
to be implied by the text, but not explictly specified.

So, it seems to be safe to assume that 'x' is being mapped
to the execution character »x«. Actually, to the numerical
value of its representation.

In ASCII 1968 this representation is: 1111000, the numerical
value of which is 120.

Thus, the value of 'x' is 120 on a C implementation with
ASCII as its execution character set.
7.19.7.3p2: »The fputc function writes the character
specified (...) to the output stream pointed to by stream«.

In »fputc( 'x', textstream )«, fputc or the text stream do
not have to do any more work to convert to the execution
character set, because 'x' already is the numerical value of
the proper representation.

The first parameter of fputc is int, so it can accept int or
char as argument type and the question whether int or char
is used does not have to do anything with the question
whether the numerical value represents a character or not.
 
L

luser- -droog

Hi there,

I'm trying to convert an integer valune to ASCII character. I have
found that I can print an integer value to a character by
   printf("%c", intA);
What I am looking for is something like
   charA = somefunction(intA)

Some hints, please?

Regards,

Xianwen

Assuming that by "integer value" you mean an int in the range [0,9],
then you can convert it to the ASCII code by adding 0x30, as any
ASCII table will tell you. It isn't clear that you mean this, but
I can't think of anything else you might mean.

If the execution character set is ascii already, you could do
something like this:

int i = 5;
char c;
switch(i) {
case 0: c = '0';
case 1: c = '1';
case 2: c = '2';
case 3: c = '3';
case 4: c = '4';
case 5: c = '5';
case 6: c = '6';
case 7: c = '7';
case 8: c = '8';
case 9: c = '9';
default: exit(42);
}
printf("%c\n", c);

But they may laugh at you.
 
B

Ben Bacarisse

Assuming that by "integer value" you mean an int in the range [0,9],
then you can convert it to the ASCII code by adding 0x30, as any
ASCII table will tell you. It isn't clear that you mean this, but
I can't think of anything else you might mean.

I suspect the OP has not yet fully understood that char values are
integer values and that (apart from maybe doing a range check)
somefunction(x) is the identity function in C. But you are right that
it's not entirely clear.
If the execution character set is ascii already, you could do
something like this:

int i = 5;
char c;
switch(i) {
case 0: c = '0';
case 1: c = '1';
case 2: c = '2';
case 3: c = '3';
case 4: c = '4';
case 5: c = '5';
case 6: c = '6';
case 7: c = '7';
case 8: c = '8';
case 9: c = '9';
default: exit(42);
}
printf("%c\n", c);

But they may laugh at you.

I am filled with questions... Who is 'they'? Is the absence of 'break'
a deliberate homework sabotaging ploy? Do you know that the decimal
digits are guaranteed to be consecutive?
 
L

luser- -droog

Assuming that by "integer value" you mean an int in the range [0,9],
then you can convert it to the ASCII code by adding 0x30, as any
ASCII table will tell you. It isn't clear that you mean this, but
I can't think of anything else you might mean.

I suspect the OP has not yet fully understood that char values are
integer values and that (apart from maybe doing a range check)
somefunction(x) is the identity function in C.  But you are right that
it's not entirely clear.


If the execution character set is ascii already, you could do
something like this:
int i = 5;
char c;
switch(i) {
    case 0: c = '0';
    case 1: c = '1';
    case 2: c = '2';
    case 3: c = '3';
    case 4: c = '4';
    case 5: c = '5';
    case 6: c = '6';
    case 7: c = '7';
    case 8: c = '8';
    case 9: c = '9';
    default: exit(42);
}
printf("%c\n", c);
But they may laugh at you.

I am filled with questions...  Who is 'they'?  

Anyone aware that doing a switch for this task is silly.
Is the absence of 'break'
a deliberate homework sabotaging ploy?

No, but I wish I had. It was just an oversight.
 Do you know that the decimal
digits are guaranteed to be consecutive?

I did know that but somehow forgot while writing the message.
But since the OP asked merely for hints, I didn't feel any
great need to offer a tested solution.
 
Joined
Oct 31, 2012
Messages
1
Reaction score
0
Hi there,

I'm trying to convert an integer valune to ASCII character. I have
found that I can print an integer value to a character by
printf("%c", intA);
What I am looking for is something like
charA = somefunction(intA)

Some hints, please?

Regards,

Xianwen



my dear friends, you all are certainly using wrong approach to solve the problem.the guy just simply wants to convert int into ascii. To do so, the program i write below. it is tested and working fine.


#include<stdio.h>
#include<string.h>
#include<conio.h>

char data[1000]= {' '}; /*thing in the bracket is optional*/
char data1[1000]={' '};
int val, a;
char varray [9];

void binary (int digit)
{
if(digit==0)
val=48;
if(digit==1)
val=49;
if(digit==2)
val=50;
if(digit==3)
val=51;
if(digit==4)
val=52;
if(digit==5)
val=53;
if(digit==6)
val=54;
if(digit==7)
val=55;
if(digit==8)
val=56;
if(digit==9)
val=57;
a=0;

while(val!=0)
{
if(val%2==0)
{
varray[a]= '0';
}

else
varray[a]='1';
val=val/2;
a++;
}


while(a!=7)
{
varray[a]='0';
a++;
}


varray [8] = NULL;
strrev (varray);
strcpy (data1,varray);
strcat (data1,data);
strcpy (data,data1);
}


void main()
{
int num;
clrscr();
printf("enter number\n");
scanf("%d",&num);
if(num==0)
binary(0);
else
while(num>0)
{
binary(num%10);
num=num/10;
}
puts(data);
getch();

}

i checked my code and its working good.copy this code in any c++ compiler and you are good to go.thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top