How to convert a text string to hex string?

后端 未结 3 1909
一向
一向 2021-01-18 14:06

Lets say I have a text string : \"Say Hello to My Little Friend\"

A function should return hex value as:

5361792048656c6c6f20746f204d79204c6974746c6520467269

3条回答
  •  清歌不尽
    2021-01-18 14:43

    You can do this like,

    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)
    

提交回复
热议问题