IOS Hex and String Conversion

Source: Internet
Author: User

 
 
  1. NSString * dictString = [dict JSONFragment]; // combined.

Use this...

 
 
  1. DictString = {"content": "Sadgfdfg", "phoneno": "", "email": "1049055935@qq.com" }==
  2.  
  3. // Convert the hexadecimal format to a normal string.
  4. + (NSString *) stringFromHexString :( NSString *) hexString {//
  5.  
  6. Char * myBuffer = (char *) malloc (int) [hexString length]/2 + 1 );
  7. Bzero (myBuffer, [hexString length]/2 + 1 );
  8. For (int I = 0; I <[hexString length]-1; I ++ = 2 ){
  9. Unsigned int anInt;
  10. NSString * hexCharStr = [hexString substringWithRange: NSMakeRange (I, 2)];
  11. NSScanner * Records = [[[NSScanner alloc] initWithString: hexCharStr] autorelease];
  12. [Export scanHexInt: & anInt];
  13. MyBuffer [I/2] = (char) anInt;
  14. }
  15. NSString * unicodeString = [NSString stringWithCString: myBuffer encoding: 4];
  16. NSLog (@ "------ string =======%@", unicodeString );
  17. Return unicodeString;
  18.  
  19.  
  20. }
  21.  
  22. // Convert a normal string to a hexadecimal string.
  23.  
  24. + (NSString *) hexStringFromString :( NSString *) string {
  25. NSData * myD = [string dataUsingEncoding: NSUTF8StringEncoding];
  26. Byte * bytes = (Byte *) [myD bytes];
  27. // The following is a hexadecimal Byte conversion.
  28. NSString * hexStr = @"";
  29. For (int I = 0; I <[myD length]; I ++)
  30.  
  31. {
  32. NSString * newHexStr = [NSString stringWithFormat: @ "% x", bytes [I] & 0xff]; // hexadecimal number
  33.  
  34. If ([newHexStr length] = 1)
  35.  
  36. HexStr = [NSString stringWithFormat: @ "% @ 0% @", hexStr, newHexStr];
  37.  
  38. Else
  39.  
  40. HexStr = [NSString stringWithFormat: @ "% @", hexStr, newHexStr];
  41. }
  42. Return hexStr;
  43. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.