UInt8 XOR的数组结果到NSString的转换每次都返回nil [英] UInt8 XOR'd array result to NSString conversion returns nil every time

查看:125
本文介绍了UInt8 XOR的数组结果到NSString的转换每次都返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用iOS Swift 2.0在[UInt8]上执行XOR并将XORd结果转换为String时遇到问题.我必须与一个原始服务器进行交互,该服务器要使用UInt8值的预定义数组进行简单的XOR加密,然后将结果作为String返回.

I'm having issues working with iOS Swift 2.0 to perform an XOR on a [UInt8] and convert the XORd result to a String. I'm having to interface with a crude server that wants to do simple XOR encryption with a predefined array of UInt8 values and return that result as a String.

使用iOS Swift 2.0 Playground,创建以下数组:

Using iOS Swift 2.0 Playground, create the following array:

let xorResult : [UInt8] = [24, 48, 160, 212]   // XORd result
let result = NSString(bytes: xorResult, length: xorResult.count, encoding: NSUTF8StringEncoding)

结果始终为零.如果从数组中删除160和212值,则NSString不为nil.如果我切换到NSUTF16StringEncoding,那么我不会收到nil,但是服务器不支持UTF16.我试过将值转换为十六进制字符串,然后将十六进制字符串转换为NSData,然后尝试将其转换为NSUTF8StringEncoding,但仍然无效,直到删除了160和212.我知道此算法在Java中有效,但是在Java中,重新使用char和StringBuilder的组合,一切都很高兴. iOS Swift中可以解决此问题吗?

The result is always nil. If you remove the 160 and 212 values from the array, NSString is not nil. If I switch to NSUTF16StringEncoding then I do not receive nil, however, the server does not support UTF16. I have tried converting the values to a hex string, then converting the hex string to NSData, then try to convert that to NSUTF8StringEncoding but still nil until I remove the 160 and 212. I know this algorithm works in Java, however in Java we're using a combination of char and StringBuilder and everything is happy. Is there a way around this in iOS Swift?

推荐答案

要将任意二进制数据块存储为字符串,您需要 字符串编码,将每个单个字节(0 ... 255)映射到某个 特点. UTF-8没有此属性,例如160 是多字节UTF-8序列的开始,并且本身无效.

To store an arbitrary chunk of binary data as as a string, you need a string encoding which maps each single byte (0 ... 255) to some character. UTF-8 does not have this property, as for example 160 is the start of a multi-byte UTF-8 sequence and not valid on its own.

具有此属性的最简单编码是 ISO Latin 1 ISO 8859-1 ISO/IEC 8859-1 补充C0和C1控制代码时进行编码. 它映射Unicode代码点U+0000 .. U+00FF0x00 .. 0xFF个字节(比较 8859-1.TXT ).

The simplest encoding with this property is the ISO Latin 1 aka ISO 8859-1, which is the ISO/IEC 8859-1 encoding when supplemented with the C0 and C1 control codes. It maps the Unicode code points U+0000 .. U+00FF to the bytes 0x00 .. 0xFF (compare 8859-1.TXT).

此编码可用于 (NS)String作为NSISOLatin1StringEncoding.

请注意:将任意二进制块转换为 带有NSISOLatin1StringEncoding(NS)String将包含嵌入式 NUL和控制字符.某些功能行为异常 与这样的字符串一起使用时.例如,NSLog()终止 在第一个嵌入的NUL字符处输出.这次转换 旨在解决OP的具体问题(创建QR码, 被第三方应用程序识别).这并不意味着 一种将任意数据转换为字符串的通用机制 以任何方式打印或呈现给用户.

Please note: The result of converting an arbitrary binary chunk to a (NS)String with NSISOLatin1StringEncoding will contain embedded NUL and control characters. Some functions behave unexpectedly when used with such a string. For example, NSLog() terminates the output at the first embedded NUL character. This conversion is meant to solve OP's concrete problem (creating a QR-code which is recognized by a 3rd party application). It is not meant as a universal mechanism to convert arbitrary data to a string which may be printed or presented in any way to the user.

这篇关于UInt8 XOR的数组结果到NSString的转换每次都返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆