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

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
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.
 
Comment

CMiYC

macrumors newbie
Original poster
Oct 22, 2007
8
3
Menlo Park
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.
 
Comment

CMiYC

macrumors newbie
Original poster
Oct 22, 2007
8
3
Menlo Park
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
 
Comment

psingh01

macrumors 65816
Apr 19, 2004
1,408
403
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).
 
Comment

robbieduncan

Moderator emeritus
Jul 24, 2002
24,772
136
Harrogate
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
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)
 
Comment

iJed

macrumors 6502
Sep 4, 2001
264
0
West Sussex, UK
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
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)];
 
Comment

CMiYC

macrumors newbie
Original poster
Oct 22, 2007
8
3
Menlo Park
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.
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.