- Get link
- X
- Other Apps
hi all,
wonder if can input solve issue.
background.
working on method in obj-c uses straight c functions.
here 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; }
completeness, function dectobin, not correct,( yet, unable point of checking logic).
code:char *dectobin(char * inputchar) { char binaryarr[eight_byte_array]; int = 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 happens. in obj-c code, "temp" contains string, of nature "100110" etc. but, line bytesofstring called, temp reverts \"000
thinking that, , knowledge of memory management in c rudimentary, "temp" being released? not sure. appreciated insight.
and, 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; }
your dectobin() returning pointer local variable (the array binaryarr). local variables not survive after function returns. same c or objective-c. strictly speaking, correct term "automatic", "local" refers scope, not lifetime.
http://en.wikipedia.org/wiki/local_variable
http://en.wikipedia.org/wiki/automatic_variable
refer c reference manual if wikipedia articles aren't sufficient. c keyword (hence, objective-c keyword) 'auto', , implicit storage type variables declared inside function bodies, 'static' implicit storage type variables declared outside function bodies.
also, loop in dectobin() never end if charvalue negative, , overflow buffer until kills something. should iterate 8 times, not indefinitely. if want produce string no leading zeros, need better algorithm have.
code wrong:
the encoding of temp (if temp survived long enough) not utf16. it's ascii or utf8, or 8-bit encoding compatible ascii in first 128 code-points. know because dectobin() operating on char types, not wchar_t types, , default interpretation of character constants '1' , '0' ascii.code:stringwithcstring:temp encoding:nsutf16stringencoding
Forums Macs Mac Programming
- iPhone
- Mac OS & System Software
- iPad
- Apple Watch
- Notebooks
- iTunes
- Apple ID
- iCloud
- Desktop Computers
- Apple Music
- Professional Applications
- iPod
- iWork
- Apple TV
- iLife
- Wireless
- Get link
- X
- Other Apps
Comments
Post a Comment