ios - 为什么 char 数组在 Objective C 中转换为字符串时会插入尾随字符?

标签 ios objective-c

我正在尝试在 NSString 上编写一个快速类别,以对字符串的内容进行 base64 编码。一切看起来都很好,除了生成的字符串尾部出现额外的字符。谁能解释为什么下面的代码会产生下面的输出?

来源:

const char base64CharSet[64] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    'w', 'x', 'y', 'z', '0', '1', '2', '3',
    '4', '5', '6', '7', '8', '9', '+', '/'
};

const char *input = "Hello, World!";

int length = strlen(input);
int outlen = (length / 3) * 4;
int modlen = length % 3;
int rawlen = length - modlen;

if (modlen != 0)
    outlen += 4;

char output[outlen];

char inbuf[3], outbuf[4];
int inpos = 0, outpos = 0;

for (outpos = 0, inpos = 0; inpos < rawlen; inpos += 3) {
    for (int i = 0; i <  3; i++) {
        int j = inpos + i;
        inbuf[i] = j < length ? input[j] : 0;
    }

    outbuf[0] =  (inbuf[0] & 0xFC) >> 2;
    outbuf[1] = ((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4);
    outbuf[2] = ((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6);
    outbuf[3] =  (inbuf[2] & 0x3F);

    output[outpos++] = base64CharSet[outbuf[0]];
    output[outpos++] = base64CharSet[outbuf[1]];
    output[outpos++] = base64CharSet[outbuf[2]];
    output[outpos++] = base64CharSet[outbuf[3]];
}

if (modlen > 0) {
    char modbuf[3] = {0, 0, 0};

    for (int i = 0; i < modlen; i++) {
        int j = rawlen + i;
        modbuf[i] = input[j];
    }

    outbuf[0] =  (modbuf[0] & 0xFC) >> 2;
    outbuf[1] = ((modbuf[0] & 0x03) << 4) | ((modbuf[1] & 0xF0) >> 4);
    outbuf[2] = ((modbuf[1] & 0x0F) << 2) | ((modbuf[2] & 0xC0) >> 6);
    outbuf[3] =  (modbuf[2] & 0x3F);

    output[outpos++] = base64CharSet[outbuf[0]];
    output[outpos++] = base64CharSet[outbuf[1]];
    output[outpos++] = modlen == 2 ? base64CharSet[outbuf[2]] : '=';
    output[outpos++] = '=';
}

NSLog(@"Input: '%s', Length: %zd", input, strlen(input));
NSLog(@"Output: '%s', Length: %zd, Expected Length: %d", output, strlen(output), outlen);

输出:

2013-03-19 14:46:51.568 Sandbox[19195:c07] Input: 'Hello, World!', Length: 13
2013-03-19 14:46:51.569 Sandbox[19195:c07] Output: 'SGVsbG8sIFdvcmxkIQ==wä]', Length: 23, Expected Length: 20

最佳答案

2013-03-19 14:46:51.569 Sandbox[19195:c07] Output: 'SGVsbG8sIFdvcmxkIQ==wä]', Length: 23, Expected Length: 20

最后的问题是因为你没有用 NULL 终止输出缓冲区。 C 字符串要求字符串中最后一个字符之后的字符为 0(全 0 位,而不是 ASCII“0”:)。


... appending to a full array would raise an exception ...

欢迎来到C!这种语言类似于拿着剪刀奔跑。即使摔倒了,也可能不会受伤。可能不会。

在这种情况下,您实际上并没有写入 NULL 字节,因此,C 字符串的打印只是读取字符串数组后面堆栈上的任何内容。我没有审核代码来确定缓冲区的大小是否正确。

假设您的所有数学计算都是正确的,您可以将缓冲区分配为比编码所需的长度长一个字节,并将终止符放在那里。

char output[outlen + 1];
output[outlen + 1] = 0;

关于ios - 为什么 char 数组在 Objective C 中转换为字符串时会插入尾随字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15508231/

相关文章:

ios - 在 UIViewController 中管理方向的说明

ios - 使两个协议(protocol)方法互斥(实现一个或另一个,而不是两者)

ios - 在 iPhone 上使用 CAShapeLayer 和 CoreAnimation 实现简单的五彩纸屑效果?

ios - 使用多个 UIControl 过滤我的 NSArray of NSDictionaries

objective-c - iOS:通讯录

iOS 方法之弱与强

ios - 如何在 iPhone 应用程序中切换扬声器和耳机

ios - 以编程方式创建的 UITableView 不显示任何内容

ios - 为什么自动布局会出现此布局问题

iphone - ABAddressBookCreate() , ABAddressBookGetGroupCount , ... 返回 @"0x00000000 <nil>"?