Reputation: 237
I am trying to get bytes from nsdata into char* but the length of char* is more than length of nsdata and contains extra junk characters appended at the last.The code:
NSData * newData=[self dataFromHexString:trimmedHex];
NSLog(@"new data length %d",[newData length]);
char * ciphertext=(char*)[newData bytes];
NSLog(@"length of ciphertext %zd",strlen(ciphertext));
The log:
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
stuck with this from morning.:(
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';
Upvotes: 0
Views: 288
Reputation: 70981
Could it be the 0
-terminator is missing for the sequences of char
s ciphertext
is pointing to?
Update:
Instead of
for (int len=0; len<[self length];len++) {
ciphertext[len]=ciphertextTemp[len];
}
ciphertext[[self length]]='\0';
you could simply do:
strncpy(ciphertext, ciphertextTemp, [newData length]);
ciphertext[[self length] - 1]='\0';
Upvotes: 1