Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Convert Ascii to char

Reply
Thread Tools

Convert Ascii to char

 
 
meendar
Guest
Posts: n/a
 
      08-22-2007
Hi,

I am having a character pointer which contains ascii values. i just
want to convert all these ascii values to respective characters and
again store it in another character pointer.

Anybody please help in c language.

Thanks in Advance.

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      08-22-2007
meendar wrote:

> Hi,
>
> I am having a character pointer which contains ascii values.


It cannot. A char pointer can only point to char objects. A char object can
contain character codes, among which ASCII is one encoding.

> i just want to convert all these ascii values to respective characters and
> again store it in another character pointer.


The ASCII codes _are_ the characters. There is not need to convert any
further, unless you want to encode with a different encoding, in which case
your system should provide the necessary routines.

char c = 'A';
char *cptr = &c;

Now 'c' is a char object which is initialised with the system's value for
the character 'A'. In ASCII it's 65, but in another encoding it may be a
different value. 'cptr' is a pointer to char and is initialised to point
to 'c' here.

To display the character in 'c' to screen do:
putchar(c);

Other methods are using putc or printf. Look up your library documentation
or C textbook.

If this is not your problem, try giving more details and if possible code.

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      08-22-2007
meendar wrote:

> I am having a character pointer which contains ascii values.


That's unlikely. `char*` variables contain pointer values,
not ascii character values. The characters the pointer points
to may, or may not, be encoded in ascii.

> i just
> want to convert all these ascii values to respective characters and
> again store it in another character pointer.


I /think/ you mean you want to copy a string from one place
to another.

If the expression `source` has value pointer to string of length N,
and the expression `dest` has value pointer to an array of chars
of length at least N+1, then executing the expression:

strcpy( dest, source )

will copy the string from `source` to `dest`. To use this expression,
you should ensure you've #included <string.h>.

This code doesn't care about ascii. In the unlikely event that matters,
you'll have to explain your problem more centrally.

Note that it's your responsibility to ensure that `dest` points to
enough characters. If it doesn't, your program will be broken, even
if nothing bad /seems/ to happen.

--
Third Floor And Still OK! Hedgehog
"Who do you serve, and who do you trust?" /Crusade/

 
Reply With Quote
 
meendar
Guest
Posts: n/a
 
      08-23-2007
Hi,

I assume the question is misunderstood, hence ask the query in
different way. I am having a file with ascii values, how can i convert
these ascii values to corresponding characters and print it?

Thanks










On Aug 22, 7:50 pm, Chris Dollin <(E-Mail Removed)> wrote:
> meendar wrote:
> > I am having a character pointer which contains ascii values.

>
> That's unlikely. `char*` variables contain pointer values,
> not ascii character values. The characters the pointer points
> to may, or may not, be encoded in ascii.
>
> > i just
> > want to convert all these ascii values to respective characters and
> > again store it in another character pointer.

>
> I /think/ you mean you want to copy a string from one place
> to another.
>
> If the expression `source` has value pointer to string of length N,
> and the expression `dest` has value pointer to an array of chars
> of length at least N+1, then executing the expression:
>
> strcpy( dest, source )
>
> will copy the string from `source` to `dest`. To use this expression,
> you should ensure you've #included <string.h>.
>
> This code doesn't care about ascii. In the unlikely event that matters,
> you'll have to explain your problem more centrally.
>
> Note that it's your responsibility to ensure that `dest` points to
> enough characters. If it doesn't, your program will be broken, even
> if nothing bad /seems/ to happen.
>
> --
> Third Floor And Still OK! Hedgehog
> "Who do you serve, and who do you trust?" /Crusade/



 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      08-23-2007
meendar wrote:

> I assume the question is misunderstood, hence ask the query in
> different way. I am having a file with ascii values,


What do you mean by "a file with ascii values"?

Do you mean a 'binary' file all of whose bytes are ascii-encoded
characters, or a 'text' file containing numerals which are the decimal
representations of ascii encodings [1], or what?

Show us. An ounce of example is worth a pound of speculation [0].

> how can i convert these ascii values to corresponding characters
> and print it?


If it's a text file like [1], then:

* you can read in lines with `fgets`
* you can convert the decimal numbers to integers using `strtol`
* you can convert those numbers to characters using a table [2]
* you can store the characters in an array if you wish
* you can output the array with `fwrite` or the characters with `fputc`

[0] "kilo[gramme]" has too many syllables, and "imperial" is a good
game. Of /course/ I have scales that can weigh speculation -- this
is a research lab!

[1] Like:

32 83 112 111 111 33 10

[2] Your implementation need not use ascii encodings, so you'd need to
translate the ascii value to your local character set; the easy
way is to use a char[] with the i'th char being your local character
set's character corresponding to the character with ascii value i.
If your implementation /does/ use ascii and you don't want your
code to be portable to non-ascii implementations, you can just
stuff the ascii value into a char -- in C, chars are like small
integers anyway.

--
Chris "seeking Cavorite" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      08-23-2007
meendar wrote:
> Hi,
>
> I assume the question is misunderstood, hence ask the query in
> different way. I am having a file with ascii values, how can i convert
> these ascii values to corresponding characters and print it?


Please don't top-post. (If you don't know what top-posting is, I suggest
you read <http://www.caliburn.nl/topposting.html>)

What do you mean by "ascii values" in a file?

Do you mean that where there would be a 'A' you have one of the
character strings "101" (octal), "65" (decimal) or "41" (hexadecimal) ?

 
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 Unicode Codepoint(dec) value to ASCII char in C++(Linux)? Aneesh E Warrier C++ 6 12-05-2006 12:13 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 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
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments