Getting a character from an ascii value (objc/cocoa)

Discussion in 'Mac Programming' started by CMiYC, Oct 22, 2007.

  1. CMiYC macrumors newbie

    Joined:
    Oct 22, 2007
    Location:
    Colorado Springs
    #1
    Anyone have a suggestion on how I would get a character with its ascii code?

    Thanks...
     
  2. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #2
    What language are you using?

    There are several ways and they are all language dependent.
     
  3. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #3
    Whoops, just also it in your title.

    An character in C is just a byte. Therefore it is just a number.

    You can compare the number between 0 and 255 with that actual character without having to cast it.

    So if the user entered 97, this is the character a.

    To actually get the character, store the value in a char. It will become that character instantly.
     
  4. CMiYC thread starter macrumors newbie

    Joined:
    Oct 22, 2007
    Location:
    Colorado Springs
    #4
    Thank you for suggestion. I tried that first, but my cocoa application keeps crashing.

    If I make a little C program to do the "conversion" it works as expected. However, when I try to integrate it into my obj-c program, crashes occur.

    I've tried converting to/from c-style cstrings with things like stringWithCString, but still no joy.
     
  5. CMiYC thread starter macrumors newbie

    Joined:
    Oct 22, 2007
    Location:
    Colorado Springs
    #5
    I should probably show what I am trying to do...

    I have a function called chr:

    char *chr(int i)
    {
    return i;
    }

    which is called like this:
    email = [NSString stringWithCString:chr(i+65)];
    // i is an integer between 0 and 20
     
  6. psingh01 macrumors 65816

    Joined:
    Apr 19, 2004
    #6
    What that function is doing is returning an int as a char pointer. Essentially it is saying that there is a c string located at address i. Which is why it crashes since i is probably some place in memory that you don't have access to and even if you did it would just be a garbage value and not what you really want.

    What the other poster was saying was something like this:

    char c = 97; // this is the same as char c = 'a';


    Your next line would probably work like this:

    char c = 97;
    email = [NSString stringWithCString:&c];

    Because with the &c you are passing in the address of c (which is NOT 97, it is where 97 is stored).
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    You seem to be making a significant error relating to C Strings. A C String is an array of characters terminating with \0. If you do not include the \0 the system will keep moving through your memory looking for it, almost certainly crashing your application in the process. As noted in the documentation for this method "If the argument passed to stringWithCString: is not a zero-terminated C-string, the results are undefined." (also this method is deprecated).

    Either terminate your string correctly or use stringWithCString:length: (also deprecated)
     
  8. iJed macrumors 6502

    Joined:
    Sep 4, 2001
    Location:
    West Sussex, UK
    #8
    Try this

    Code:
    char *chr(int i)
    {
    	char *cstr=(char *)malloc(2);
    	cstr[0]=(char)i;
    	cstr[1]='\0';	
    
    	return cstr;
    }
    
    email = [NSString stringWithCString:chr(i+65)];
    
     
  9. CMiYC thread starter macrumors newbie

    Joined:
    Oct 22, 2007
    Location:
    Colorado Springs
    #9
    Thanks everyone for the help. The last time I used C was years ago for an 8051 microprocessor-based project (where I never used strings anyway). Obj-C is catching me off my game.
     

Share This Page