Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Print Extended ASCII in ANSI C

Reply
Thread Tools

Print Extended ASCII in ANSI C

 
 
ramif
Guest
Posts: n/a
 
      12-01-2007
Is there a way to print extended ASCII in C??

I tried to code something, but it only displays strange symbols.
here is my code:

main()
{
char chr = 177; //stores the extended ASCII of a symbol
printf("Character with an ascii code of 177: %c \n", chr);
//tries to print an ASCII symbol...

return 0;
}

thankx
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      12-01-2007
ramif wrote:

> Is there a way to print extended ASCII in C??


There is no guarantee to even print the ASCII characters as C is
character encoding agnostic, except for the fact that a certain basic
set of characters must be available and that the value of characters in
the range 0... 9 must be continuous.

> I tried to code something, but it only displays strange symbols.
> here is my code:
>
> main()
> {
> char chr = 177; //stores the extended ASCII of a symbol
> printf("Character with an ascii code of 177: %c \n", chr);
> //tries to print an ASCII symbol...
>
> return 0;
> }


If you are sure that your machine as support for extended characters
try:

#include <stdio.h>

int main(void) {
unsigned char c;

for (c = 32; c <= 255; c++) putc(c, stdout);
return 0;
}

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-01-2007
ramif said:

> Is there a way to print extended ASCII in C??


Yes, but it may involve more work than you anticipated. Not all systems use
ASCII, and those that do use it may not use extended ASCII, or at least
not the extended ASCII that you want. (For example, a typical IBM PC's
extended character set depended on which code page you selected.)

> I tried to code something, but it only displays strange symbols.
> here is my code:
>
> main()
> {
> char chr = 177; //stores the extended ASCII of a symbol
> printf("Character with an ascii code of 177: %c \n", chr);
> //tries to print an ASCII symbol...
>
> return 0;
> }


Your code is not legal C. Either you're using C99 (unlikely) or you're not.
If you are, then main() - which takes advantage of the implicit int return
type for functions - is illegal; you'll need int main() at the very least,
and preferably int main(void). But if you're not using C99, then //stores
is a syntax error.

Also, you're missing <stdio.h>, so the printf invokes undefined behaviour.

Other than that, the code does what you tell it - i.e. it attempts to
display a glyph corresponding to the character code 177. What that does
depends very much on the system you are using, and C offers no guarantees
that the displayed glyph is the one you were actually after.

Without looking it up, I can't even begin to guess what glyph you were
hoping to see, but I can tell you that on some systems it's likely to
display the glyph you want, and on others this is very unlikely indeed.

To display the character you want, irrespective of the platform you are
using, you have a couple of obvious choices. One is to set up your own
character set, using arrays of strings. (Very painful, and it comes out in
huge letters!) Another way is to find or write a graphics library, and use
that to prepare a bitmap or JPEG or something, where you literally draw
the characters yourself. Again, this is probably more work than you want
to do.

So, if neither of those solutions appeals to you, your other option is to
consult the documentation for your implementation, to find out whether it
provides an extension to achieve what you want. If you need help with this
option, ask in a newsgroup devoted to your implementation or platform.
Possibilities include comp.os.ms-windows.programmer.win32 and
comp.unix.programmer.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
ramif
Guest
Posts: n/a
 
      12-01-2007
santosh wrote:
> ramif wrote:
>
>> Is there a way to print extended ASCII in C??

>
> There is no guarantee to even print the ASCII characters as C is
> character encoding agnostic, except for the fact that a certain basic
> set of characters must be available and that the value of characters in
> the range 0... 9 must be continuous.
>
>> I tried to code something, but it only displays strange symbols.
>> here is my code:
>>
>> main()
>> {
>> char chr = 177; //stores the extended ASCII of a symbol
>> printf("Character with an ascii code of 177: %c \n", chr);
>> //tries to print an ASCII symbol...
>>
>> return 0;
>> }

>
> If you are sure that your machine as support for extended characters
> try:
>
> #include <stdio.h>
>
> int main(void) {
> unsigned char c;
>
> for (c = 32; c <= 255; c++) putc(c, stdout);
> return 0;
> }
>


I tried your code, and gcc gave me the following warning:
"warning: comparison is always true due to limited range of data type"

BTW, i'm using Linux Fedora 7 running on x86_64 architecture. GCC
version is 4.1.2 20070925

Do you think that my PC supports extended ASCII??

A few month ago, i've written a Pascal program (on Windows XP) that
displays the extended ASCII and it worked without any problems.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      12-01-2007
ramif wrote:

> santosh wrote:
>> ramif wrote:
>>
>>> Is there a way to print extended ASCII in C??

>>
>> There is no guarantee to even print the ASCII characters as C is
>> character encoding agnostic, except for the fact that a certain basic
>> set of characters must be available and that the value of characters
>> in the range 0... 9 must be continuous.
>>
>>> I tried to code something, but it only displays strange symbols.
>>> here is my code:
>>>
>>> main()
>>> {
>>> char chr = 177; //stores the extended ASCII of a symbol
>>> printf("Character with an ascii code of 177: %c \n", chr);
>>> //tries to print an ASCII symbol...
>>>
>>> return 0;
>>> }

>>
>> If you are sure that your machine as support for extended characters
>> try:
>>
>> #include <stdio.h>
>>
>> int main(void) {
>> unsigned char c;
>>
>> for (c = 32; c <= 255; c++) putc(c, stdout);
>> return 0;
>> }
>>

>
> I tried your code, and gcc gave me the following warning:
> "warning: comparison is always true due to limited range of data
> type"


Oops, bitten by the unsigned bug again!

Well change the type of 'c' to int.

> BTW, i'm using Linux Fedora 7 running on x86_64 architecture. GCC
> version is 4.1.2 20070925
>
> Do you think that my PC supports extended ASCII??
>
> A few month ago, i've written a Pascal program (on Windows XP) that
> displays the extended ASCII and it worked without any problems.


As Richard said, most systems do support extended characters, but
exactly which set is currently applicable is system dependant. The C
language only guarantees that the characters in it's basic source and
execution character set are present. Beyond that everything is
implementation dependant.

 
Reply With Quote
 
ramif
Guest
Posts: n/a
 
      12-01-2007
Richard Heathfield wrote:
> ramif said:
>
>> Is there a way to print extended ASCII in C??

>
> Yes, but it may involve more work than you anticipated. Not all systems use
> ASCII, and those that do use it may not use extended ASCII, or at least
> not the extended ASCII that you want. (For example, a typical IBM PC's
> extended character set depended on which code page you selected.)
>
>> I tried to code something, but it only displays strange symbols.
>> here is my code:
>>
>> main()
>> {
>> char chr = 177; //stores the extended ASCII of a symbol
>> printf("Character with an ascii code of 177: %c \n", chr);
>> //tries to print an ASCII symbol...
>>
>> return 0;
>> }

>
> Your code is not legal C. Either you're using C99 (unlikely) or you're not.
> If you are, then main() - which takes advantage of the implicit int return
> type for functions - is illegal; you'll need int main() at the very least,
> and preferably int main(void). But if you're not using C99, then //stores
> is a syntax error.
>
> Also, you're missing <stdio.h>, so the printf invokes undefined behaviour.
>
> Other than that, the code does what you tell it - i.e. it attempts to
> display a glyph corresponding to the character code 177. What that does
> depends very much on the system you are using, and C offers no guarantees
> that the displayed glyph is the one you were actually after.
>
> Without looking it up, I can't even begin to guess what glyph you were
> hoping to see, but I can tell you that on some systems it's likely to
> display the glyph you want, and on others this is very unlikely indeed.
>
> To display the character you want, irrespective of the platform you are
> using, you have a couple of obvious choices. One is to set up your own
> to do.
>
> So, if neither of those solutions appeals to you, your other option is to
> consult the documentation for your implementation, to find out whether it
> provides an extension to achieve what you want. If you need help with this
> option, ask in a newsgroup devoted to your implementation or platform.
> Possibilities include comp.os.ms-windows.programmer.win32 and
> comp.unix.programmer.
>


I'm trying to use the extended ASCII as shown in this website
http://www.asciitable.com/. I'm using an x86_64 Linux (Fedora 7)

Thankx for your response.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-01-2007
ramif said:

<snip>

> I'm trying to use the extended ASCII as shown in this website
> http://www.asciitable.com/. I'm using an x86_64 Linux (Fedora 7)


So you have some options. One is:

#include <stdio.h>

int main(void)
{
int i;
for(i = 0; i < 4; i++)
{
puts(" * * * * * *");
putchar('\n');
puts("* * * * * *");
putchar('\n');
}
return 0;
}

Another is:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int rc = EXIT_FAILURE;
unsigned char bmp177[] =
{
0x42, 0x4D, 0x76, 0x02, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x10, 0x00,
0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x12, 0x0B,
0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 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FF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF,
0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00,
0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00
};
FILE *fp = fopen("177.bmp", "w");
if(fp != NULL)
{
fwrite(bmp177, 1, sizeof bmp177, fp);
if(!ferror(fp))
{
rc = EXIT_SUCCESS;
}
if(fclose(fp) != 0)
{
rc = EXIT_FAILURE;
}
}
return rc;
}

and then display the bitmap in whatever way seems best to you.

Alternatively, you could consult the documentation for your implementation,
to find out whether it provides an extension to achieve what you want. If
you need help with this option, ask in a newsgroup devoted to your
implementation or platform. Possibilities include
comp.os.ms-windows.programmer.win32 and comp.unix.programmer.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Erik Trulsson
Guest
Posts: n/a
 
      12-01-2007
ramif <(E-Mail Removed)> wrote:
> Richard Heathfield wrote:
>> ramif said:
>>
>>> Is there a way to print extended ASCII in C??

>>
>> Yes, but it may involve more work than you anticipated. Not all systems use
>> ASCII, and those that do use it may not use extended ASCII, or at least
>> not the extended ASCII that you want. (For example, a typical IBM PC's
>> extended character set depended on which code page you selected.)
>>
>>> I tried to code something, but it only displays strange symbols.
>>> here is my code:
>>>
>>> main()
>>> {
>>> char chr = 177; //stores the extended ASCII of a symbol
>>> printf("Character with an ascii code of 177: %c \n", chr);
>>> //tries to print an ASCII symbol...
>>>
>>> return 0;
>>> }

>>
>> Your code is not legal C. Either you're using C99 (unlikely) or you're not.
>> If you are, then main() - which takes advantage of the implicit int return
>> type for functions - is illegal; you'll need int main() at the very least,
>> and preferably int main(void). But if you're not using C99, then //stores
>> is a syntax error.
>>
>> Also, you're missing <stdio.h>, so the printf invokes undefined behaviour.
>>
>> Other than that, the code does what you tell it - i.e. it attempts to
>> display a glyph corresponding to the character code 177. What that does
>> depends very much on the system you are using, and C offers no guarantees
>> that the displayed glyph is the one you were actually after.
>>
>> Without looking it up, I can't even begin to guess what glyph you were
>> hoping to see, but I can tell you that on some systems it's likely to
>> display the glyph you want, and on others this is very unlikely indeed.
>>
>> To display the character you want, irrespective of the platform you are
>> using, you have a couple of obvious choices. One is to set up your own
>> to do.
>>
>> So, if neither of those solutions appeals to you, your other option is to
>> consult the documentation for your implementation, to find out whether it
>> provides an extension to achieve what you want. If you need help with this
>> option, ask in a newsgroup devoted to your implementation or platform.
>> Possibilities include comp.os.ms-windows.programmer.win32 and
>> comp.unix.programmer.
>>

>
> I'm trying to use the extended ASCII as shown in this website
> http://www.asciitable.com/. I'm using an x86_64 Linux (Fedora 7)


That particular extension to ASCII looks similar to the one which was
usually used with MS-DOS. It is unlikely that whichever terminal emulator
you are using uses that particular character set. Most likely it is set up
to use either ISO-8859-1 or UTF-8 which are different extensions to ASCII.

If you absolutely want to use that particular character set, you will have
to ask at some forum appropriate to you system how to set up your terminal emulator
to use that character set (if it is even possible.)


--
<Insert your favourite quote here.>
Erik Trulsson
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-01-2007
ramif wrote:
> Is there a way to print extended ASCII in C??
>
> I tried to code something, but it only displays strange symbols.
> here is my code:
>
> main()
> {
> char chr = 177; //stores the extended ASCII of a symbol
> printf("Character with an ascii code of 177: %c \n", chr);
> //tries to print an ASCII symbol...
>
> return 0;
> }


This has got nothing to do with C.

Your computer's terminal display system is printing whatever is
character 177 in its display font. You'll need to figure out how to
change that font if you want to display some other character. Doing this
is NOT a C question.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-01-2007
santosh wrote:
>

.... snip ...
>
> If you are sure that your machine as support for extended
> characters try:
>
> #include <stdio.h>
>
> int main(void) {
> unsigned char c;
>
> for (c = 32; c <= 255; c++) putc(c, stdout);
> return 0;
> }


If your machine has 8 bit bytes, has that stopped running yet?

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
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
pre-ansi to ansi c++ conversion? Frank Iannarilli C++ 2 07-21-2009 11:05 PM
Are there statistics packages in ANSI C and/or ANSI C++? lbrtchx@gmail.com C Programming 11 04-28-2008 03:00 AM
Are there statistics packages in ANSI C and/or ANSI C++? lbrtchx@gmail.com C++ 1 04-24-2008 06:44 PM
how to print extended ASCII codes in vc? chjiangwh@gmail.com C Programming 3 04-11-2006 09:49 AM
routine/module to translate microsoft extended ascii to plain ascii James O'Brien Perl Misc 3 03-05-2004 04:33 PM



Advertisments