Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 24, 2009, 05:19 PM   #1
heretiic
macrumors newbie
 
Join Date: May 2009
Printing ASCII characters over 127.

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

Last edited by SilentPanda; May 24, 2009 at 05:33 PM. Reason: code tags
heretiic is offline   0 Reply With Quote
Old May 24, 2009, 05:24 PM   #2
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: The Bamboo Forest
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...scii_table.jpg
__________________
My 24 hour web cam!
SilentPanda is offline   0 Reply With Quote
Old May 24, 2009, 05:31 PM   #3
heretiic
Thread Starter
macrumors newbie
 
Join Date: May 2009
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

Last edited by SilentPanda; May 24, 2009 at 05:34 PM. Reason: code tags
heretiic is offline   0 Reply With Quote
Old May 24, 2009, 05:36 PM   #4
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: The Bamboo Forest
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...
__________________
My 24 hour web cam!
SilentPanda is offline   0 Reply With Quote
Old May 24, 2009, 05:39 PM   #5
heretiic
Thread Starter
macrumors newbie
 
Join Date: May 2009
Ok, thank you
Is there an alternative to %c for printing unsigned chars?
heretiic is offline   0 Reply With Quote
Old May 24, 2009, 08:18 PM   #6
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
Quote:
Originally Posted by heretiic View Post
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>.


Quote:
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:

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);
        }
    }
}
lloyddean is offline   0 Reply With Quote
Old May 24, 2009, 11:04 PM   #7
heretiic
Thread Starter
macrumors newbie
 
Join Date: May 2009
Ok. Is it possible to print all "wierd" signs in any other way? I mean besides ascii if that doesnt work.
heretiic is offline   0 Reply With Quote
Old May 25, 2009, 02:35 AM   #8
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by heretiic View Post
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.
gnasher729 is offline   0 Reply With Quote
Old May 25, 2009, 10:36 AM   #9
Guiyon
macrumors 6502a
 
Join Date: Mar 2008
Location: North Shore, MA
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;
}
__________________
Make life easier if you have a programming question!
http://www.sscce.org/

Last edited by Guiyon; May 25, 2009 at 06:48 PM.
Guiyon is offline   0 Reply With Quote
Old May 25, 2009, 11:04 AM   #10
ChrisA
macrumors G4
 
Join Date: Jan 2006
Location: Redondo Beach, California
Quote:
Originally Posted by heretiic View Post
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.
ChrisA is offline   0 Reply With Quote
Old May 25, 2009, 06:14 PM   #11
heretiic
Thread Starter
macrumors newbie
 
Join Date: May 2009
Thank you!

Thanks everyone, that solved my problem.
heretiic is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How can I add 127.0.0.1 localhost to my etc/hosts file/ oxband Mac OS X 10.7 Lion 3 Jun 20, 2013 01:52 PM
Walmart Offering iPhone 5 for $127, Third-Generation iPad for $399 MacRumors MacRumors.com News Discussion 403 Jan 28, 2013 03:07 AM
Eclipse Juno: Error 127 timebourne Mac Programming 6 Dec 10, 2012 09:41 PM

Forum Jump

All times are GMT -5. The time now is 04:04 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC