3

How do I convert the code bellow to a NSString?

std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;

for( int i = 0; i < ciphertext.size(); i++ ) {

    std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}

std::cout << std::endl << std::endl;

I tried :

 NSString *cyp = [NSString stringWithUTF8String:ciphertext.c_str()];

But the cyp NSString is null..

NSString *cyp = @(ciphertext.c_str());
NSLog(@"cypher %@",cyp);
NSLog(@"cypher %s",ciphertext.c_str());

prints out:

Pas[8044:70b] playntext Now is the time for all good men to come to the aide...
2013-11-27 14:38:01.421 Pas[8044:70b] cypher (null)
2013-11-27 14:38:01.422 Pas[8044:70b] cypher ¯≥Íä≥z=(fúóß≥¢P%Ä’“2ŒË
W3ÔpˇHÈËò©¬^ß∞@C°¸#±°Î≤ˆóbp°Å nxÄê
2013-11-27 14:38:01.423 Pas[8044:70b] decrypted Now is the time for all good men to come to the aide...

So why can I print it out like a string but can't create a NSString from "ciphertext"

user1028028
  • 6,323
  • 9
  • 34
  • 59

1 Answers1

3

Try that this works for me:

NSString *s1 = @(YOURCSTRING.c_str());
Greg
  • 25,317
  • 6
  • 53
  • 62
  • For this answer, you have to pray that your C string uses UTF-8. If it does, this is the best answer. – JeremyP Nov 27 '13 at 10:31
  • s1 will be `nil` if YOURCSTRING is like `\xffffff80\x02-addListener`, which includes non-UTF-8 character – RY_ Zheng Mar 18 '21 at 04:40