Hi all,
I wonder if I can get some input to solve this issue.
Background.
I am working on a method in Obj-C that uses some straight c functions.
Here is the code:
For completeness, the function decToBin, which is probably not correct,( but as yet, unable to get to that point of checking the logic).
Here is what happens. in the Obj-C code, "temp" initially contains a string, of the nature "100110" etc. But, as soon as the line bytesOfString is called, temp reverts to \"000
My thinking is that, and this is where my knowledge of memory management in C is rudimentary, that "temp" is being released? But not sure. Would appreciated some insight.
And, the other function mentioned.
I wonder if I can get some input to solve this issue.
Background.
I am working on a method in Obj-C that uses some straight c functions.
Here is the code:
Code:
-(NSString*) charToBinary: (NSString *) aCharacter
{
char *charForTest = (char *)[aCharacter UTF8String];
char *temp = (decToBin(charForTest));
NSLog(@"%s", temp);
NSString *bytesOfString = [NSString stringWithCString:temp
encoding:NSUTF16StringEncoding];
return bytesOfString;
}
For completeness, the function decToBin, which is probably not correct,( but as yet, unable to get to that point of checking the logic).
Code:
char *decToBin(char * inputChar)
{
char binaryArr[EIGHT_BYTE_ARRAY];
int i = 0;
int charValue = (int) inputChar[0];
while (charValue) {
binaryArr[i++] = (charValue % 2) ? '1': '0';
charValue /= 2;
}
binaryArr[i] = '\0';
char *t = reverseString(binaryArr);
return t;
}
Here is what happens. in the Obj-C code, "temp" initially contains a string, of the nature "100110" etc. But, as soon as the line bytesOfString is called, temp reverts to \"000
My thinking is that, and this is where my knowledge of memory management in C is rudimentary, that "temp" is being released? But not sure. Would appreciated some insight.
And, the other function mentioned.
Code:
char *reverseString(char *inputString)
{
int i, j;
char t[strlen(inputString)+1];
strcpy(t,inputString);
for (i = 0, j = strlen(inputString) - 1; j >= 0; j--, i++)
{
*(inputString +i) = *(t + j);
}
inputString[i] = '\0';
//printf("\"InputString\":%s\n", inputString);
return inputString;
}