将这种十六进制字符串转换为 NSData/NSString
我有这个十六进制字符串:
\x5c30\x3032\x5f5c\x3337\x345c\x3334\x366f\x5c32\x3633\x5c30\x3136\x5c32\x3132\x5c32\x3234\x4e5c\x3236\x335c\x3231\x335c\x3337\x355c\x3335\x315c\x3232\x365c\x3337
如何将其转换为 NSString 或 NSData?我虽然使用 C 方法,但我对 C 没有经验:(
I have this hex string:
\x5c30\x3032\x5f5c\x3337\x345c\x3334\x366f\x5c32\x3633\x5c30\x3136\x5c32\x3132\x5c32\x3234\x4e5c\x3236\x335c\x3231\x335c\x3337\x355c\x3335\x315c\x3232\x365c\x3337
How could I convert it to a NSString or NSData? I though of using C methods, but I'm not experienced in C :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说看起来像 Unicode 字符(特别是 CJK 表意文字)。
使用 NSScanner 扫描字符串。扫描到反斜杠,然后 将扫描到的任何内容添加到可变字符串。然后,扫描反斜杠并将其扔掉,然后扫描 x 并将其扔掉。
然后,扫描四个单个字符,这将是数字(NSScanner 没有扫描单个字符的方法,因此您需要使用
characterAtIndex:
自己获取它们,然后调整扫描仪的扫描位置相应)。执行十六进制数字字符到数字的适当转换,并进行数学运算以将它们组合成单个数字,您将获得由转义序列表示的代码点(字符值)。将该单个字符添加到您的字符串中。重复此操作,直到用完输入字符串,并且您已将输入字符串及其所有转义序列转换为包含未转义字符的字符串。
Looks like Unicode characters (specifically, CJK ideographs) to me.
Use an NSScanner to scan the string. Scan up to a backslash, and add whatever you scanned to a mutable string. Then, scan the backslash and throw it away, and then scan the x and throw that away.
Then, scan four single characters, which will be the digits (NSScanner doesn't have a method to scan a single character, so you will need to get them yourself using
characterAtIndex:
and then adjust the scanner's scan location accordingly). Perform the appropriate conversion of the hexadecimal digit characters to numbers and the math to assemble a single number from them, and you will have the code point (character value) represented by the escape sequence. Add that single character to your string.Repeat that until you run out of input string, and you will have converted the input string with all its escape sequences into a string with the unescaped characters.