将 NSString 转换为 ASCII 二进制等效值(然后再次转换回 NSString)

发布于 2024-10-27 19:56:25 字数 517 浏览 10 评论 0原文

我在这方面遇到了一些问题。

我想获取一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守不住的情 2024-11-03 19:56:25

*请注意,为了更容易理解,此代码省略了将位存储到整数数组中的额外要求。

// test embed
NSString *myString = @"A"; //65
for (int i=0; i<[myString length]; i++) {
    int asciiCode = [myString characterAtIndex:i];
    unsigned char character = asciiCode; // this step could probably be combined with the last
    printf("--->%c<---\n", character);
    printf("--->%d<---\n", character);
    // for each bit in a byte extract the bit
    for (int j=0; j < 8; j++) {
        int bit = (character >> j) & 1;
        printf("%d ", bit);
    }           
}


// test extraction
int extractedPayload[8] = {1,0,0,0,0,0,1,0}; // A (note the byte order is backwards from conventional ordering)
unsigned char retrievedByte = 0;

for (int i=0; i<8; i++) {
    retrievedByte += extractedPayload[i] << i;
}

printf("***%c***\n", retrievedByte);
printf("***%d***\n", retrievedByte);

现在我想在执行这些步骤之前我必须从 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.

// test embed
NSString *myString = @"A"; //65
for (int i=0; i<[myString length]; i++) {
    int asciiCode = [myString characterAtIndex:i];
    unsigned char character = asciiCode; // this step could probably be combined with the last
    printf("--->%c<---\n", character);
    printf("--->%d<---\n", character);
    // for each bit in a byte extract the bit
    for (int j=0; j < 8; j++) {
        int bit = (character >> j) & 1;
        printf("%d ", bit);
    }           
}


// test extraction
int extractedPayload[8] = {1,0,0,0,0,0,1,0}; // A (note the byte order is backwards from conventional ordering)
unsigned char retrievedByte = 0;

for (int i=0; i<8; i++) {
    retrievedByte += extractedPayload[i] << i;
}

printf("***%c***\n", retrievedByte);
printf("***%d***\n", retrievedByte);

Now I guess I've just got to filter out any non ascii characters from my NSString before I do these steps.

说谎友 2024-11-03 19:56:25

将 NSString 转换为 Integer:(从 Ben 发布的链接中获取),

// NSString to ASCII
NSString *string = @"A";
int asciiCode = [string characterAtIndex:0]; // 65

然后将其传递给下面的函数:

NSArray *arrayOfBinaryNumbers(int val) 
{
    NSMutableArray* result = [NSMutableArray array];
    size_t i;
    for (i=0; i < CHAR_BIT * sizeof val; i++) {
        int theBit = (val >> i) & 1;
        [result addObject:[NSNumber numberWithInt:theBit]];
    }
    return result;
}

Convert NSString to Integer:(got it from link Ben posted )

// NSString to ASCII
NSString *string = @"A";
int asciiCode = [string characterAtIndex:0]; // 65

then pass it to function below :

NSArray *arrayOfBinaryNumbers(int val) 
{
    NSMutableArray* result = [NSMutableArray array];
    size_t i;
    for (i=0; i < CHAR_BIT * sizeof val; i++) {
        int theBit = (val >> i) & 1;
        [result addObject:[NSNumber numberWithInt:theBit]];
    }
    return result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文