2016-06-09 7 views

Antwort

1

Kann nicht in der Lage zu schreiben, in schnellen, aber in Objective-C Code unten ist, kann das, was Sie suchen:

NSString * str = @"Say Hello to My Little Friend"; 

    NSString * hexString = [NSString stringWithFormat:@"%@", 
         [NSData dataWithBytes:[str cStringUsingEncoding:NSUTF8StringEncoding] 
             length:strlen([str cStringUsingEncoding:NSUTF8StringEncoding])]]; 

    for(NSString * toRemove in [NSArray arrayWithObjects:@"<", @">", @" ", nil]) 
     hexString = [hexString stringByReplacingOccurrencesOfString:toRemove withString:@""]; 

    NSLog(@"hexStr:%@", hexString); 

Above Code gibt genaue Zeichenfolge wie Sie gegeben:

5361792048656c6c6f20746f204d79204c6974746c6520467269656e64

Hope it :)

3

Sie diese wie tun kann helfen,

NSString *str = @"Say Hello to My Little Friend"; 
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; 

NSLog(@"data %@",data); 

Optput: data <53617920 48656c6c 6f20746f 204d7920 4c697474 6c652046 7269656e 64>

Swift:

let string = "Say Hello to My Little Friend" 
let data = string.dataUsingEncoding(NSUTF8StringEncoding) 

Swift 3:

let string = "Say Hello to My Little Friend" 
let data = string.data(using: String.Encoding.utf8) 
3

Swift 4, Swift 3

let hex = "ABC".unicodeScalars.filter { $0.isASCII }.map { String(format: "%X", $0.value) }.joined() print(hex) // correctly yields 414243