Convert unsigned char * to int * in Objective-C - iphone

I am trying to convert a unsigned char* to int * on in Objective-C on the iPhone. Is there any API that can help with the conversion?
Here's my best attempt:
-(BOOL)MyFunc:Version:(int *)nVer
{
unsigned char * uszVerSize;
//trying to assign string to int
nVer = uszVerSize[0] ;
}

Dear Lord, I think you have bigger problems than the one stated above.
You need to convert the chars to an int and return that.
return [[NSNumber numberWithUnsignedChar:uszVerSize] intValue];
You should also learn about pointers and how ints and chars differ in memory. You can't just assign a pointer to a char to a pointer to an int.

const char *asciiCString = [#"abc-zABC-Z" cString];
int cStringLen = [#"abc-zABC-Z" length];
for(i=0;i<cStringLen;i++) {
[asciiMArray addObject: [[NSNumber alloc] initWithInteger: asciiCString[i]]];
printf("%d\n",asciiCString[i]);
}
for(i=0;i<cStringLen;i++) {
NSLog(#"%#",[asciiMArray objectAtIndex: i]);
printf("%d\n",asciiCString[i]);
}
This is a code i wrote yesterday, to test some code of my learning face
It may look naive...but if it helps you....
asciiCString[i] returns you ASCII value of the char referenced at the index..
asciiMArray is a NSMutableArray Object

Related

How to store UUID as int

I m having some issues here.. I am using the following code to generate the uuid in mine application.
- (NSString *)GetUUID
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return [(NSString *)string autorelease];
}
This code returns a NSString object back. Now I want to store the generated UUID as unsigned int. Any suggestions please how can i do it here. Thanks.
According to the Wikipedia page, UUIDs are 128 bits long. The largest int value you'll be able to work with is 64 bits. Therefore, I'd says you can't store it in an int because there simply isn't room.
Convert UUID to String using .UUIDString
Take string's .hash property.
Cast to int.
int numericFormUUID = (int)UUIDString.hash;
Note: collision is possible, as always, with hashing. Two users could end up with the same value here, whereas UUID is guaranteed unique.

ObjectiveC Parse Integer from String

I'm trying to extract a string (which contains an integer) from an array and then use it as an int in a function. I'm trying to convert it to a int using intValue.
Here's the code I've been trying.
NSArray *_returnedArguments = [serverOutput componentsSeparatedByString:#":"];
[_appDelegate loggedIn:usernameField.text:passwordField.text:(int)[[_returnedArguments objectAtIndex:2] intValue]];
I get this error:
passing argument 3 of 'loggedIn:::' makes pointer from integer
without a cast
What's wrong?
I really don't know what was so hard about this question, but I managed to do it this way:
[myStringContainingInt intValue];
It should be noted that you can also do:
myStringContainingInt.intValue;
You can just convert the string like that [str intValue] or [str integerValue]
integerValue
Returns the NSInteger value of the receiver’s text.
(NSInteger)integerValue
Return Value
The NSInteger value of the receiver’s text, assuming a decimal representation and skipping whitespace at the beginning of the string. Returns 0 if the receiver doesn’t begin with a valid decimal text representation of a number.
for more information refer here
NSArray *_returnedArguments = [serverOutput componentsSeparatedByString:#":"];
_returnedArguments is an array of NSStrings which the UITextField text property is expecting. No need to convert.
Syntax error:
[_appDelegate loggedIn:usernameField.text:passwordField.text:(int)[[_returnedArguments objectAtIndex:2] intValue]];
If your _appDelegate has a passwordField property, then you can set the text using the following
[[_appDelegate passwordField] setText:[_returnedArguments objectAtIndex:2]];
Basically, the third parameter in loggedIn should not be an integer, it should be an object of some kind, but we can't know for sure because you did not name the parameters in the method call. Provide the method signature so we can see for sure. Perhaps it takes an NSNumber or something.
Keep in mind that international users may be using a decimal separator other than . in which case values can get mixed up or just become nil when using intValue on a string.
For example, in the UK 1.23 is written 1,23, so the number 1.777 would be input by user as 1,777, which, as .intValue, will be 1777 not 1 (truncated).
I've made a macro that will convert input text to an NSNumber based on a locale argument which can be nil (if nil it uses device current locale).
#define stringToNumber(__string, __nullable_locale) (\
(^NSNumber *(void){\
NSLocale *__locale = __nullable_locale;\
if (!__locale) {\
__locale = [NSLocale currentLocale];\
}\
NSString *__string_copy = [__string stringByReplacingOccurrencesOfString:__locale.groupingSeparator withString:#""];\
__string_copy = [__string_copy stringByReplacingOccurrencesOfString:__locale.decimalSeparator withString:#"."];\
return #([__string_copy doubleValue]);\
})()\
)
If I understood you correctly, you need to convert your NSString to int? Try this peace of code:
NSString *stringWithNumberInside = [_returnedArguments objectAtIndex:2];
int number;
sscanf([stringWithNumberInside UTF8String], "%x", &flags);

How to use Array of C language with Objective-C

I'm using Objective-C language. But I don't know how to use c with Objective-C.
Ex) This is Function method.
- ( ?? ) function{
unsigned int first = ..
unsigned int second = ..
unsigned int third = ..
int infoData = {first,second,third};
return infoData;
}
How to fill in parenthesis.
I don't use NSArray.
Please help me.
the answer is the same as it is in C. Objective-C is a strict superset of C.
Assuming you declared int[] infoData you could make the return int*, but you're still going to have problems because the array is allocated on the function's stack. You'll need to dynamically allocate space for it just like you would in C.
(You cannot use int[] as a return type)
The code below will compile, but gcc will warn about returning the address of a function local variable.
#interface test
- (int*) function;
#end
#implementation test
- (int*) function{
unsigned int first = 0;
unsigned int second = 1;
unsigned int third = 2;
int infoData[] = {first,second,third};
return infoData;
}
#end

Why is there no * in this method declaration?

Here is the method declaration midway in Apple's documentation:
Learning Objective-C: A Primer
- (void)insertObject:(id) anObject atIndex:(NSUInteger) index
Why is there no * right after NSUInteger. I thought all objects were pointer types and all strongly typed pointers had to have a * character after it.
NSUInteger is not an object type, it is a typedef to unsigned int.
The only reason that you would actually want to use a * in this context would be if you wanted to get the address of an int and store something in it. (Some libraries do this with error messaging). An example of this:
-(void) methodName: (NSUInteger *) anInt {
*anInt = 5;
}
NSUInteger a;
[obj methodName: &a]; //a is now 5

Proper checking of nil sqlite text column

I have an sqlite string column that is assigned to a string. I need to make sure it isn't nil before assigning. I'm doing this:
char *isNil = sqlite3_column_text(selectstmt, 2);
if(isNil != nil){
myName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];}
which gives the warning:
warning: initialization discards qualifiers from pointer target type
What is the proper way to do it?
You're getting the warning as you're ignoring the const. The API is defined:
const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
You're assigning the return to a char*, so you're dropping the const. That is why you get the warning. You should respect the const.
const unsigned char *isNil = ...
I'm not really a huge objective-c guy, but I think stylistically it's common practice to compare primative types against NULL rather than nil. Also there is no need to call column_text twice.
const char *columnText = (const char *)sqlite3_column_text(selectstmt, 2);
if(columnText != NULL)
{
myName = [NSString stringWithUTF8String: columnText ];
}
You can see above I've cast the const unsigned char pointer to a const signed char pointer. You should make sure you know when you cast away a warning that it is the right thing to do. In this case it is safe to cast to an signed char. In general, never cast away const though, as whoever made that API could be doing something that requires you to treat the data as const.