[英]convert NSString from char array contains chinese character
我正在使用openssl從p12文件中提取代碼符號,並得到它。 但它不是可讀的字符串對象。 例如,我的代碼符號是:“” iPhone Developer:振王“,並且函數返回” iPhone Developer:\\ xE6 \\ x8C \\ xAF \\ xE7 \\ x8E \\ x8B“
這是代碼:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
NSString *p1 = @"/Users/william/Desktop/root.p12";
NSString *s = [self getCodesignFromP12Path:p1 andPassword:@"1"];
// It's output 'iPhone Developer: \xE6\x8C\xAF \xE7\x8E\x8B (7V3KMVKNR4)'
NSLog(@"%@", s);
}
- (NSString *)getCodesignFromP12Path:(NSString *)p12Path andPassword:(NSString *)pwd
{
NSString *string = nil;
NSString *command = [NSString stringWithFormat:@"openssl pkcs12 -in %@ -nodes -passin pass:%@ | openssl x509 -noout -subject | awk -F'[=/]' '{print $6}'", p12Path, pwd];
FILE*pipein_fp;
char readbuf[80] = {0};
if((pipein_fp=popen([command cStringUsingEncoding:NSUTF8StringEncoding],"r"))==NULL)
{
perror("popen");
exit(1);
}
while(fgets(readbuf,80,pipein_fp))
{
string = [NSString stringWithCString:readbuf encoding:NSUTF8StringEncoding];
}
pclose(pipein_fp);
return string;
}
如何獲得正確的漢字值?
您很可能將NSLog的輸出與字符串的內容混淆。
NSLog不會打印中文字符。 設置按鈕標題或文本內容等iOS功能可以正常工作。
現在,我通過此解決方案獲得了代碼簽名,但是我不知道如果使用openssl會導致什么。 這是github演示 。
const NSString * kCertificationPassword = @"1";
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSString *ret = nil;
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"root" ofType:@"p12"];
// Dump identifier with openssl
NSString *identifierWithSSL = [NSString stringWithFormat:@"openssl pkcs12 -in %@ -nodes -passin pass:%@ | openssl x509 -noout -subject | awk -F'[=/]' '{print $6}'", cerPath, kCertificationPassword];
[self shellCMD:identifierWithSSL result:&ret];
// Output: iPhone Developer: \xE6\x8C\xAF \xE7\x8E\x8B (7V3KMVKNR4)
NSLog(@"%@", ret);
// Import certification to keychain.
NSString *importComand = [NSString stringWithFormat:@"security import %@ -k ~/Library/Keychains/login.keychain -P %@ -T /usr/bin/codesign",cerPath, kCertificationPassword];
[self shellCMD:importComand result:nil];
// Dump identifier with security.
NSString *identifierWithSecurity = @"security find-identity -p codesigning | grep 7V3KMVKNR4 | awk -F'[\"\"]' '{print $2}'";
[self shellCMD:identifierWithSecurity result:&ret];
// Output: iPhone Developer: 振 王 (7V3KMVKNR4)
NSLog(@"%@", ret);
}
- (void)shellCMD:(NSString*)cmd result:(NSString**)aResult
{
FILE*pipein_fp;
char readbuf[80] = {0};
if((pipein_fp=popen([cmd cStringUsingEncoding:NSUTF8StringEncoding],"r"))==NULL)
{
perror("popen");
exit(1);
}
while(fgets(readbuf,80,pipein_fp))
{
if ( aResult )
{
*aResult = [NSString stringWithCString:readbuf encoding:NSUTF8StringEncoding];
}
}
pclose(pipein_fp);
}
@end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.