Created
February 21, 2019 01:38
-
-
Save zwo/5d660e738f317d95f83ddf491b98ccd6 to your computer and use it in GitHub Desktop.
NSString <> unicode, file read
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/stat.h> | |
@implementation NSString (test) | |
+ (NSString *)unicodeStringWithString:(NSString *)string { | |
NSString *result = [NSString string]; | |
for (int i = 0; i < [string length]; i++) { | |
result = [result stringByAppendingFormat:@"\\u%04x", [string characterAtIndex:i]]; | |
/* | |
因为 Unicode 用 16 个二进制位(即 4 个十六进制位)表示字符,对于小于 0x1000 字符要用 0 填充空位, | |
所以使用 %04x 这个转换符, 使得输出的十六进制占 4 位并用 0 来填充开头的空位. | |
*/ | |
} | |
return result; | |
} | |
+ (NSString *)stringWithUnicodeString:(NSString *)string { | |
NSArray *strArray = [[string substringFromIndex:2] componentsSeparatedByString:@"\\u"]; | |
NSString *result = [NSString string]; | |
for (NSString *str in strArray) { | |
NSString *tmpStr = [@"0x" stringByAppendingString:str]; | |
unichar c = strtoul([tmpStr UTF8String], 0, 0); | |
/* | |
上面两行也可以写成下面一行: | |
unichar c = strtoul([str UTF8String], 0, 16); | |
*/ | |
result = [result stringByAppendingString:[NSString stringWithCharacters:&c length:1]]; | |
} | |
return result; | |
} | |
@end | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
uint16 buffer[1024]; | |
long position=0x20f000; | |
int numOfUnichar=15; | |
NSString *binaryPath=@"/Applications/xxx.app/Contents/MacOS/xxx"; | |
const char *binary_path=[binaryPath cStringUsingEncoding:NSUTF8StringEncoding]; | |
struct stat s; | |
if(stat(binary_path, &s) != 0) { | |
perror(binary_path); | |
NSLog(@"binary path wrong"); | |
exit(1); | |
} | |
FILE *binaryFile = fopen(binary_path, "r+"); | |
if(!binaryFile) { | |
printf("Couldn't open file %s\n", binary_path); | |
exit(1); | |
} | |
fseek(binaryFile, position, SEEK_SET);//0x00010020ecde | |
fread(&buffer, sizeof(buffer), 1, binaryFile); | |
fclose(binaryFile); | |
unichar a; | |
NSMutableString *muString=[NSMutableString string]; | |
for (int i=0; i<numOfUnichar; i++) { | |
a=buffer[i]; | |
[muString appendString:[NSString stringWithCharacters:&a length:1]]; | |
} | |
NSLog(@"%@",muString); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment