NSdata, NSString, Byte array, UIImage conversion, nsdatansstring
1. NSData and NSString
NSData-> NSString
NSString * aString = [[NSString alloc] initWithData: adataencoding: NSUTF8StringEncoding];
NSString-> NSData
NSString * aString = @ "1234 abcd ";
NSData * aData = [aString dataUsingEncoding: NSUTF8StringEncoding];
2. NSData and Byte
NSData-> Byte array
NSString * testString = @ "1234567890 ";
NSData * testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
Byte * testByte = (Byte *) [testData bytes];
For (int I = 0; I <[testData length]; I ++)
Printf ("testByte = % d \ n", testByte [I]);
Byte array-> NSData
Byte byte [] = {, 21 };
NSData * adata = [[NSData alloc] initWithBytes: byte length: 24];
Byte array-> hexadecimal number
Byte * bytes = (Byte *) [aData bytes];
NSString * hexStr = @"";
For (int I = 0; I <[encryData length]; I ++)
{
NSString * newHexStr = [NSString stringWithFormat: @ "% x", bytes [I] & 0xff]; // hexadecimal number
If ([newHexStr length] = 1)
HexStr = [NSString stringWithFormat: @ "% @ 0% @", hexStr, newHexStr];
Else
HexStr = [NSString stringWithFormat: @ "% @", hexStr, newHexStr];
}
NSLog (@ "bytes hexadecimal number: % @", hexStr );
Hexadecimal number-> Byte array
Convert hexadecimal data into Byte Arrays
NSString * hexString = @ "3e435fab9c34891f"; // hexadecimal string
Int j = 0;
Byte bytes [128]; // Byte array of 3ds key, 128 bits
For (int I = 0; I <[hexString length]; I ++)
{
Int int_ch; // The hexadecimal number after the two hexadecimal numbers are converted.
Unichar hex_char1 = [hexString characterAtIndex: I]; // the first of the two hexadecimal numbers (high * 16)
Int int_ch1;
If (hex_char1> = '0' & hex_char1 <= '9 ')
Int_rectangle = (hex_char1-48) * 16; // 0 Ascll-48
Else if (hex_char1> = 'A' & hex_char1 <= 'F ')
Int_rectangle = (hex_char1-55) * 16; // Ascll-65 of
Else
Int_rectangle = (hex_char1-87) * 16; // Ascll-97 of
I ++;
Unichar hex_char2 = [hexString characterAtIndex: I]; // the second digit in the two hexadecimal numbers (low position)
Int int_ch2;
If (hex_char2> = '0' & hex_char2 <= '9 ')
Int_ch2 = (hex_char2-48); // 0 Ascll-48
Else if (hex_char1> = 'A' & hex_char1 <= 'F ')
Int_ch2 = hex_char2-55; // Ascll-65 for
Else
Int_ch2 = hex_char2-87; // a's Ascll-97
Int_ch = int_ch + int_ch2;
NSLog (@ "int_ch = % d", int_ch );
Bytes [j] = int_ch; // put the converted number into the Byte array
J ++;
}
NSData * newData = [[NSData alloc] initWithBytes: bytes length: 128];
NSLog (@ "newData = % @", newData );
3. NSData and UIImage
NSData-> UIImage
UIImage * aimage = [UIImage imageWithData: imageData];
// Example: extract the image from the local file sandbox and convert it to NSData
NSString * path = [[NSBundle mainBundle] bundlePath];
NSString * name = [NSString stringWithFormat: @ "ceshi.png"];
NSString * finalPath = [path stringByAppendingPathComponent: name];
NSData * imageData = [NSData dataWithContentsOfFile: finalPath];
UIImage * aimage = [UIImage imageWithData: imageData];
UIImage-> NSData
NSData * imageData = UIImagePNGRepresentation (aimae );