Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Convert an integer value to ASCII character

Reply
Thread Tools

Convert an integer value to ASCII character

 
 
Xianwen Chen
Guest
Posts: n/a
 
      02-13-2011
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
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      02-13-2011
Xianwen Chen <(E-Mail Removed)> writes:
>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?

 
Reply With Quote
 
 
 
 
Dr Nick
Guest
Posts: n/a
 
      02-13-2011
Xianwen Chen <(E-Mail Removed)> writes:

> 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.
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      02-13-2011
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) writes:
>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.

 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      02-13-2011
On Sun, 13 Feb 2011 08:13:33 -0800 (PST), Xianwen Chen
<(E-Mail Removed)> wrote:

>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().
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      02-13-2011
(E-Mail Removed)-berlin.de (Stefan Ram) writes:
>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.

 
Reply With Quote
 
luser- -droog
Guest
Posts: n/a
 
      02-14-2011
On Feb 13, 10:13*am, Xianwen Chen <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-14-2011
luser- -droog <(E-Mail Removed)> writes:

> On Feb 13, 10:13Â*am, Xianwen Chen <(E-Mail Removed)> wrote:
>> 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?

<snip>
> 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?

--
Ben.
 
Reply With Quote
 
luser- -droog
Guest
Posts: n/a
 
      02-14-2011
On Feb 14, 5:35*am, Ben Bacarisse <(E-Mail Removed)> wrote:
> luser- -droog <(E-Mail Removed)> writes:
> > On Feb 13, 10:13*am, Xianwen Chen <(E-Mail Removed)> wrote:
> >> 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?

> <snip>
> > 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.
 
Reply With Quote
 
alphareaper alphareaper is offline
Junior Member
Join Date: Oct 2012
Posts: 1
 
      10-31-2012
Quote:
Originally Posted by Xianwen Chen View Post
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert an Character to the ASCII Character Code John Gregory Ruby 0 07-05-2009 10:32 PM
problem to convert integer to ascii chars for LCD in vhdl Kenneth VHDL 2 08-20-2007 10:29 PM
How to convert a 4 byte character string to its equivalent 4 byte integer value? Polaris431 C Programming 8 12-04-2006 07:00 AM
How to convert an integer to ASCII character ? akarui.tomodachi@gmail.com C Programming 16 03-03-2006 02:01 AM
[FR/EN] how to convert the characters ASCII(0-255) to ASCII(0-127) Alextophi Perl Misc 8 12-30-2005 10:43 AM



Advertisments