Convert NSString to ASCII Binary Equivilent (and then back to an NSString again)

后端 未结 2 2026
囚心锁ツ
囚心锁ツ 2021-01-26 19:06

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 as

2条回答
  •  情话喂你
    2021-01-26 19:36

    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;
    }
    

提交回复
热议问题