将 NSString 转换为 ASCII 二进制等效值(然后再次转换回 NSString)
我在这方面遇到了一些问题。
我想获取一个 NSString 并将其转换为仅包含 0,1 值的整数数组,该数组表示 ascii 字符串的二进制等价物。
举例来说,假设我有以下内容
NSString *string = @"A"; // Decimal value 65
,我想以数组结束
int binary[8] = {0,1,0,0,0,0,0,1};
,然后假设我有二进制整数数组,我如何返回 NSString?
我知道 NSString 将字符存储为多个字节,但我只想使用 ASCII。 我尝试使用,
NSData *data = [myString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
将我的字符串转换为 ascii,但我仍然遇到很多问题。有人可以帮我吗? :)
I'm having some problems with this.
I want to take an NSString and convert it to an integer array of only 0,1 values that would represent the binary equivalent of an ascii string.
So for example say I have the following
NSString *string = @"A"; // Decimal value 65
I want to end up with the array
int binary[8] = {0,1,0,0,0,0,0,1};
then given that I have the binary integer array, how do I go back to an NSString?
I know that NSString stores characters as multiple bytes but I only want to use ASCII.
I tried using,
NSData *data = [myString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
to convert my string to ascii, but I'm still having a lot of issues. Can someone please help me out? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
*请注意,为了更容易理解,此代码省略了将位存储到整数数组中的额外要求。
现在我想在执行这些步骤之前我必须从 NSString 中过滤掉所有非 ASCII 字符。
*Note this code leaves out the extra requirement of storing the bits into an integer array in order to be easier to understand.
Now I guess I've just got to filter out any non ascii characters from my NSString before I do these steps.
将 NSString 转换为 Integer:(从 Ben 发布的链接中获取),
然后将其传递给下面的函数:
Convert NSString to Integer:(got it from link Ben posted )
then pass it to function below :