gpt4 book ai didi

ios - 从 nsdata 获取字节附加额外的垃圾字符

转载 作者:行者123 更新时间:2023-11-29 03:43:39 28 4
gpt4 key购买 nike

我试图从 nsdata 中获取字节到 char* 中,但是 char* 的长度超过了 nsdata 的长度,并且在最后附加了额外的垃圾字符。代码:

 NSData * newData=[self dataFromHexString:trimmedHex];
NSLog(@"new data length %d",[newData length]);
char * ciphertext=(char*)[newData bytes];
NSLog(@"length of ciphertext %zd",strlen(ciphertext));

日志:

2013-08-02 17:02:28.175 AES[4187:11303] new data length 128
2013-08-02 17:02:28.176 AES[4187:11303] length of ciphertext 144

从早上就坚持这个。:(

const char * ciphertextTemp=(const char*)[newData bytes];
char ciphertext[[newData length]+1];
for (int len=0; len<[self length];len++) {
ciphertext[len]=ciphertextTemp[len];
}
ciphertext[[self length]]='\0';

最佳答案

是否是 charciphertext 指向的序列缺少 0 终止符?


更新:

代替

for (int len=0; len<[self length];len++) {
ciphertext[len]=ciphertextTemp[len];
}
ciphertext[[self length]]='\0';

你可以简单地这样做:

strncpy(ciphertext, ciphertextTemp, [newData length]);
ciphertext[[self length] - 1]='\0';

关于ios - 从 nsdata 获取字节附加额外的垃圾字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18015670/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com