Printing ASCII characters over 127.

Discussion in 'Mac Programming' started by heretiic, May 24, 2009.

  1. macrumors newbie

    #1
    Hi there I am trying to print out all ASCII signs on my macbook.

    This is the code I made:

    Code:
    #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
     
  2. Moderator emeritus

    SilentPanda

    #2
  3. macrumors newbie

    #3
    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

    Code:
    #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
     
  4. Moderator emeritus

    SilentPanda

    #4
    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... :(
     
  5. macrumors newbie

    #5
    Ok, thank you :)
    Is there an alternative to %c for printing unsigned chars?
     
  6. macrumors 6502a

    #6
    This is as it should be.

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


    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:

    Code:
    #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);
            }
        }
    }
    
     
  7. macrumors newbie

    #7
    Ok. Is it possible to print all "wierd" signs in any other way? I mean besides ascii if that doesnt work.
     
  8. macrumors G5

    gnasher729

    #8
    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.
     
  9. macrumors 6502a

    #9
    Your shell and terminal app would need to support Unicode but here is an example using wprintf which will print some Unicode charsets:

    Code:
    #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;
    }
     
  10. macrumors G4

    #10
    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.
     
  11. macrumors newbie

    #11
    Thank you!

    Thanks everyone, that solved my problem. :)
     

Share This Page