1
for (int i=0; i < strlen(chars1); i++) {
    //*buf=chars1[i];
    NSLog(@"Received %c\n", chars1[i]);
    ichar = [ NSString stringWithUTF8String:chars1[i] ];
    NSLog(@"Received ", ichar);
    counter=strlen(chars1)-i;
gnutls_record_send (session, [ichar UTF8String] , counter); 
}

I am trying to use this method to send information to my server but i keep getting an warning:

warning: passing argument 1 of 'stringWithUTF8String:' makes pointer from integer without a cast

the warning is for this line: ichar = [ NSString stringWithUTF8String:chars1[i] ];

is there anyway i can store chars1[i] into a temp char or string and the use it in my loop for sending?

UPDATED

I am just looking for a way to parse a NSString from starting character to end and then be able to use that without getting this warning:

makes pointer from integer without a cast

6 Answers 6

1

Maybe this is what you want.

NSString *unicodeString = [NSString stringWithUTF8String:chars1]; 
for (int i=0; i < [unicodeString length]; i++) {
    NSString *ichar = [unicodeString substringWithRange:NSMakeRange(i, 1)];
    NSLog(@"Received %@", ichar);
    counter=strlen(chars1)-i;
    gnutls_record_send (session, [ichar UTF8String] , counter); 
}

That will take a string encoded in UTF-8 in chars1, and send the UTF-8 encoding of each individual character (which itself might be 1 or a few bytes) to gnutls_record_send.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't parse UTF8 one character at a time (the correspondence is not one-to-one). The correct solution depends on the protocol you're parsing, which should specify how to determine when a string begins and ends. Follow the protocol to identify the completed string as a raw array of bytes and then parse it as UTF8 to covert it to a string.

Comments

1

You don't need to take individual element from char array and do all that. There is a class method in NSString,

+ (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc
cString

    A C array of bytes. The array must end with a NULL character; intermediate NULL characters are not allowed.

//Use this 
    NSString* tempString = [NSString stringWithCString:chars1 encoding:NSUTF8StringEncoding];

4 Comments

gnutls_record_send (session, [tempString characterAtIndex:i] , counter); I tried that in my for loop after creating the string that you showed. It does not work. I need a way to get the string character by character.
Instead of saying, "It does not work," can you say what happened (error? warning? exception? wrong data?) and what you expected to happen? It would help clarify what you are trying to achieve.
my bad. it still gives the same warning for the function listed in my comment. the "makes pointer from integer without a cast" for using [tempString characterAtIndex:i] in the gnuTLS function.
@morningstar what i expect to happen is be able to get character by character and send it in the gnuTLS function in my for loop. thats all.
1

This error is most likely caused by this line:

ichar = [ NSString stringWithUTF8String:chars1[i] ];

NSString:stringWithUTF8String: requires a pointer to a string, and you are providing just one character to it.

The warning is telling you that you have passed an integer (a single char is an integer of sorts) in a pointer field; if you tried to run this code it would end poorly, as it will try to read invalid memory.

UTF-8 is a multi-byte encoding format; you can't iterate over it in this way. Consider placing the entire string inside an NSString and modify it using Cocoa's API, finally exporting the string you wish to use with GNUTLS back when done.

Comments

1

If you know that your string is UTF8, create an NSString object using – initWithUTF8String: then use – characterAtIndex: if you need to get individual characters.

Other methods are here:

http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFStrings/Articles/AccessingContents.html

Comments

1

The result of chars[i] will be a single char, not a string. You can't pass it into NSString stringWithUTFString: because it is expecting a nil-terminated C string, not a single character.

You should try something like this instead:

ichar = [NSString stringWithCharacters:&chars[i] length:1];

2 Comments

That won't work. It's like trying to make sense of an English sentence by replacing each words with its first dictionary definition. You can't parse a UTF8 string character-by-character.
I think it's supposed to say [NSString stringWithCharacters:&chars[i] length:1]. Whether it will work will depend on what you want to do, which isn't clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.