Special Charakters with printf (C) in xCode

Discussion in 'Mac Programming' started by darxun, Mar 26, 2008.

  1. macrumors member

    Joined:
    Mar 18, 2008
    #1
    Hi there,

    My Problem:

    Code:
    		case 1:
    			{
    				Verschluesseln(szString,iSchl);
    				[COLOR="Red"]printf("Der verschluesselte Text lautet: %s"[/COLOR], szString);
    			};
    The red printf gives me back a String. Each character of this Strings got its ASCII-Code changed by adding a Number (Key). Just consider it as a very bad encryption^^.
    But what I read in the console looks like this: \223\204\222\223 (Result when I type in "test" and Key=31)



    Code:
     #include <stdio.h> 
     #include <stdlib.h> 
     #include <string.h> 
     #define STRLEN 100 
      
     int StringLaenge(unsigned char *szString) 
     { 
          int Len=0; 
          Len=strlen(szString); 
          return Len; 
     } 
     int StringPruefung(unsigned char *szString) 
     { 
          int Len=0, ii=0;
    	  Len=StringLaenge(szString);
          for(ii=0;ii<Len;ii++)         
          { 
               if ((szString[ii]<32)||(szString[ii]>122))   
                    { 
                         return 0; /*Falsche Eingabe*/; 
                    } 
          } 
          return 1; 
     } 
     void Einlesen(unsigned char *szString) 
     { 
    	  printf("Geben Sie einen Text zum Verschluesseln ein: "); 
          gets(szString); 
     } 
     void Ausgabe(unsigned char *szString, char cWahl); 
     int Verschluesseln(unsigned char *szString, int iSchl)
     {
    		int ii=0, Len=0;
    		Len=StringLaenge(szString);
     		for(ii=0;ii<Len;ii++)      
    		szString[ii] = szString[ii]+iSchl;
    
    		return 0;
     }
     void Entschluesseln(unsigned char *szString, int iSchl)
     {
    		int ii=0, Len=0;
    		Len=StringLaenge(szString);
    		for(ii=0;ii<Len;ii++)      
    		szString[ii] = szString[ii]-iSchl;
     }
     void Verdrehe(unsigned char *szString); 
      
     int main (void) 
     { 
          unsigned char szString[STRLEN]; 
          int iSchl=0, iy=0, temp=0, Len=0; 
      
    
          Einlesen(szString);
    	  Len=StringLaenge(szString);
          printf("Geben Sie einen Schluessel ein: ");  
          scanf("%d",&iSchl); 
          temp=StringPruefung(szString); 
          printf("%d \n\n", temp);
    	  printf("Wollen Sie...\n(1)  ...verschluesseln?\n(2)  ...entschluesseln?\n");
    	  scanf("%d", &iy);
    	  switch(iy)
    	  {
    		/*Verschluesselung: Steuerzeichen bis ASCII 31 und ASCII-Werte groesser als 122(z) ausgeschlossen*/
    		case 1:
    			{
    				Verschluesseln(szString,iSchl);
    				printf("Der verschluesselte Text lautet: %s", szString);
    			};
    		break;
    
    		/*Entschluesselung: keine Pr¸fung*/
    		case 2:
    			{
    				Entschluesseln(szString,iSchl);
    				printf("Der entschlüsselte Text lautet: %s", szString);
    
    			};
    		break;
    
    		default:
    			{printf("Diese Auswahl gibt es nicht!");
    			return EXIT_FAILURE;}
    		break;
    	  }
    
          /*Nur bei Verschl¸sseln String pr¸fen*/ 
          return 0;  
     }
    
    
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    This has done just as you've asked, but you are now above 128 in ASCII, so the characters don't exist/are unprintable. Instead, their octal escape sequences are printed:
    t 116
    e 101
    s 115
    t 116

    t 116 + 31 = 147 = o223
    e 101 + 31 = 132 = o204
    s 115 + 31 = 146 = o222

    I'm not sure what result you are hoping for, but if you want to do ROT-n with some wraparound, you will need to do a little more math, such as:
    szString[ii] = szString[ii] + iSchl > 122 ? ((szString[ii]+iSchl) % 122) + 65 : szString[ii] + iSchl;
    to encode, and something like:
    szString[ii] = szString[ii] < 65 + iSchl ? szString[ii] + (122 - 65) - iSchl : szString[ii] - iSchl;

    Viel Gluck!

    -Lee
     
  3. thread starter macrumors member

    Joined:
    Mar 18, 2008
    #3
    I forgot to mention that my key will only go from 1 to 133 (a function which guarantees that comes later), so in unsigned char I will not reach the control characters, which are not printable.

    In unsigned char I have these chracters:
    http://fra.nksteidl.de/Erinnerungen/ascii.php

    BTW: In Viusal Studio I get charcters like ôäÆô (this is for "test", key=31)
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    It seems that wherever you ran that and got \223\204\222\223 didn't seem to be using Der Zeichensatz Latin-1, or if it is it's not liking things above 128 regardless. I'm not able to say how one might adjust that behavior.

    -Lee

    EDIT: On the page you referenced there is a note that characters above 128 are not ascii, so i would expect the behavior of using them to be undefined. Windows does something with those characters, but other platforms may ignore them, print escape characters, etc.
     

Share This Page