iPhone4 iOS5 如何以编程方式将字符串转换为二进制字符代码?

发布于 2024-12-18 00:29:14 字数 608 浏览 3 评论 0原文

可能的重复:
将 NSString 转换为 ASCII 二进制等效值(然后再次返回 NSString)

我想知道如何获取一个字符并将其转换为二进制字符串。

假设我有一个string :

NSString* test = @"Test";

如何按从左到右的顺序将该字符串打印为每个字符的 ASCII 二进制代码:

T = decimal 84 = binary 1010100
e = decimal 101= binary 1100101

然后我想打印所有字符代码的总和序列

NSString binaryTest = @"10101001100101...";

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

I would like to know how I can get a character and convert it to binary string.

Let's say I have a string :

NSString* test = @"Test";

How can I print this string as an ASCII binary codes for each character in order from left to right:

T = decimal 84 = binary 1010100
e = decimal 101= binary 1100101

I then want to print the sum sequence of all character codes

NSString binaryTest = @"10101001100101...";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

还如梦归 2024-12-25 00:29:14

这应该做你想要的:

    NSMutableString *finalString = [[NSMutableString alloc] init];
    NSMutableString *binary;
    NSMutableString *binaryReversed;

    // The original string
    NSString *test = @"Test";

    char temp;
    int i, j, digit, dec;

    // Loop through all the letters in the original string
    for (i = 0; i < test.length; i++) {
        // Get the character
        temp = [test characterAtIndex:i];
        dec = (int)temp;
        binary = [[NSMutableString alloc] initWithString:@""];
        // Convert the decimal value to binary representation
        // getting the remainders and storing them in an NSString
        while (dec != 0) {
            digit = dec % 2;
            dec = dec / 2;
            [binary appendFormat:@"%d", digit];
        }
        // Reverse the bit sequence in the NSString
        binaryReversed = [[NSMutableString alloc] initWithString:@""];
        for (j = (int)(binary.length) - 1; j >= 0; j--) {
            [binaryReversed appendFormat:@"%C", [binary characterAtIndex:j]];
        }
        // Append the bit sequence to the current letter to the final string
        [finalString appendString:binaryReversed];
        [binaryReversed release];
        [binary release];
    }
    // Just show off the result and release the finalString
    NSLog(@"%@ in binary: %@", test, finalString);
    [finalString release];

This should do what you want:

    NSMutableString *finalString = [[NSMutableString alloc] init];
    NSMutableString *binary;
    NSMutableString *binaryReversed;

    // The original string
    NSString *test = @"Test";

    char temp;
    int i, j, digit, dec;

    // Loop through all the letters in the original string
    for (i = 0; i < test.length; i++) {
        // Get the character
        temp = [test characterAtIndex:i];
        dec = (int)temp;
        binary = [[NSMutableString alloc] initWithString:@""];
        // Convert the decimal value to binary representation
        // getting the remainders and storing them in an NSString
        while (dec != 0) {
            digit = dec % 2;
            dec = dec / 2;
            [binary appendFormat:@"%d", digit];
        }
        // Reverse the bit sequence in the NSString
        binaryReversed = [[NSMutableString alloc] initWithString:@""];
        for (j = (int)(binary.length) - 1; j >= 0; j--) {
            [binaryReversed appendFormat:@"%C", [binary characterAtIndex:j]];
        }
        // Append the bit sequence to the current letter to the final string
        [finalString appendString:binaryReversed];
        [binaryReversed release];
        [binary release];
    }
    // Just show off the result and release the finalString
    NSLog(@"%@ in binary: %@", test, finalString);
    [finalString release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文