PDA

View Full Version : Printing ASCII characters over 127.




heretiic
May 24, 2009, 05:19 PM
Hi there I am trying to print out all ASCII signs on my macbook.

This is the code I made:

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

int main()
{
unsigned int i;
for(i = 0; i <= 255; i++){
unsigned char c = i;
printf("HEJSAN %c = %d \n",c,c);

}


return;
}

When i gcc it and then runs it, this is the prints i get:
(ill just show the last)
HEJSAN ? = 244
HEJSAN ? = 245
HEJSAN ? = 246
HEJSAN ? = 247
HEJSAN ? = 248
HEJSAN ? = 249
HEJSAN ? = 250
HEJSAN ? = 251
HEJSAN ? = 252
HEJSAN ? = 253
HEJSAN ? = 254
HEJSAN ? = 255
Signs up until 127 works but everything after that is printed as "?", why is that? Asked a couple of friends and none of them has this problem with their macbook.

Best regards
Alex



SilentPanda
May 24, 2009, 05:24 PM
Once above 127 you get into the funky characters... they might just not print well so instead a ? gets displayed.

http://enteos2.area.trieste.it/russo/IntroInfo2001-2002/CorsoRetiGomezel/ASCII-EBIC_files/extend_ascii_table.jpg

heretiic
May 24, 2009, 05:31 PM
SilentPanda: maybe. but if i print a line with something like a or it works for me ( printf(""); ) works... Why is that? And I made another program

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

int main(void)
{
int i;
unsigned char c;

while(1)
{
c = getchar();

if (c != (char)10)
{
printf("%3d = %c\n", (int) c, c);
}
}
}

It tell me what ascii value that a char has. for ex: a = 97 but if i enter it tells me:
195 = ?
182 = ?
This is wierd :S

SilentPanda
May 24, 2009, 05:36 PM
I'm guessing (gonna look into it more in a bit) that %c only prints signed chars? So when you pass a number hgiher than 127 to %c it goofs it up. I'm not really a C programmer... :(

heretiic
May 24, 2009, 05:39 PM
Ok, thank you :)
Is there an alternative to %c for printing unsigned chars?

lloyddean
May 24, 2009, 08:18 PM
Hi there I am trying to print out all ASCII signs on my macbook.

<- snipped ->

Best regards
Alex

This is as it should be.

From the wikipedia article at <http://en.wikipedia.org/wiki/ASCII>.



ASCII includes definitions for 128 characters: 33 are
non-printing, mostly obsolete control characters that affect
how text is processed;[6] 94 are printable characters, and the
space is considered an invisible graphic.


Anything beyond character 127 is NOT part of the standard and
would result in output that varies from platform to platform.
dependent.

With that said:


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

int main(void)
{
int c;

printf("Control-D to stop!\n");
while ( (c = getchar()) != EOF )
{
if ( c != (char)10 )
{
printf("%3d = %c\n", (int) c, c);
}
}
}

heretiic
May 24, 2009, 11:04 PM
Ok. Is it possible to print all "wierd" signs in any other way? I mean besides ascii if that doesnt work.

gnasher729
May 25, 2009, 02:35 AM
Ok. Is it possible to print all "wierd" signs in any other way? I mean besides ascii if that doesnt work.

Most likely your compiler and the console assume that you are using UTF8, not ASCII. Try printing strlen (""). Lookup UTF8 on Google, wikipedia has a decent article.

Guiyon
May 25, 2009, 10:36 AM
Your shell and terminal app would need to support Unicode but here is an example using wprintf which will print some Unicode charsets:

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main( void ) {
wchar_t val = 0x20;

setlocale( LC_ALL, "en_US.UTF-8" );

printf( "Printing the (printable) US-ASCII charset:\n" );
for( ; val < 0x7F; val++ ) {
wprintf( L"Character 0x%3.3X is %lc\n", val, val );
}

printf( "Printing Latin-1 Supplement charset:\n" );
for( val = 0x80; val <= 0xFF; val++ ) {
wprintf( L"Character 0x%3.3X is %lc\n", val, val );
}

printf( "Printing Latin Extended-A charset:\n" );
for( val = 0x100; val <= 0x17F; val++ ) {
wprintf( L"Character 0x%3.3X is %lc\n", val, val );
}

printf( "Printing Latin Extended-B charset:\n" );
for( val = 0x180; val <= 0x24F; val++ ) {
wprintf( L"Character 0x%3.3X is %lc\n", val, val );
}

printf( "Printing Cyrillic charset:\n" );
for( val = 0x400; val <= 0x4FF; val++ ) {
wprintf( L"Character 0x%3.3X is %lc\n", val, val );
}

return 0;
}

ChrisA
May 25, 2009, 11:04 AM
Hi there I am trying to print out all ASCII signs on my macbook.

....
Signs up until 127 works but everything after that is printed as "?", why is that? Asked a couple of friends and none of them has this problem with their macbook.


ASCII is a 7-bit code. There are only 127 character defined.

However you may not be printing ASCII. From the terminal type the command "locale" and see what character set in in use.

heretiic
May 25, 2009, 06:14 PM
Thanks everyone, that solved my problem. :)