将c++字符串转换为NSString

Convert c++ string to NSString

本文关键字:NSString 转换 字符串 c++      更新时间:2023-10-16

如何将下面的代码转换为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;

我试过了:

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

但是cyp NSString为空。。

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

打印输出:

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...

那么,为什么我可以像字符串一样打印出来,但不能从"密文"创建NSString

试试这对我有用:

NSString *s1 = @(YOURCSTRING.c_str());