1, NSData and NSString
NSData-NSString
NSString *astring = [[NSString alloc] Initwithdata:adata encoding:nsutf8stringencoding];
Nsstring->nsdata
NSString *astring = @ "1234ABCD";
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[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
NSData *adata = [[NSData alloc] Initwithbytes:byte length:24];
byte array->16 binary number
byte *bytes = (BYTE *) [aData bytes];
NSString *[email protected] "";
for (int i=0;i<[encrydata length];i++)
{
NSString *newhexstr = [NSString stringwithformat:@ "%x",bytes[i]&0xff]; 16 binary number
if ([Newhexstr length]==1)
HEXSTR = [NSString stringwithformat:@ "%@0%@", hexstr,newhexstr];
Else
HEXSTR = [NSString stringwithformat:@ "%@%@", hexstr,newhexstr];
}
NSLog (@ "bytes 16 binary number is:%@", hexstr);
16 binary number->byte array
Converts 16 binary data into a byte array
NSString *hexstring = @ "3e435fab9c34891f"; 16 binary string
int j=0;
Byte bytes[128];
byte array of 3DS key, 128-bit
for (int i=0;i<[hexstring length];i++)
{
int int_ch; Two-bit 16 binary number after conversion 10 binary number
Unichar hex_char1 = [hexstring characteratindex:i]; First digit in two-bit 16 decimal (high *16)
int int_ch1;
if (hex_char1 >= ' 0 ' && hex_char1 <= ' 9 ')
Int_ch1 = (hex_char1-48) *16; 0 of Ascll-48
else if (hex_char1 >= ' A ' && hex_char1 <= ' F ')
Int_ch1 = (hex_char1-55) *16; A's Ascll-65
Else
Int_ch1 = (hex_char1-87) *16; A's Ascll-97
i++;
Unichar hex_char2 = [hexstring characteratindex:i]; Second bit (low) in two-bit 16 binary digits
int int_ch2;
if (hex_char2 >= ' 0 ' && hex_char2 <= ' 9 ')
INT_CH2 = (hex_char2-48); 0 of Ascll-48
else if (hex_char1 >= ' A ' && hex_char1 <= ' F ')
INT_CH2 = hex_char2-55; A's Ascll-65
Else
INT_CH2 = hex_char2-87; A's Ascll-97
Int_ch = INT_CH1+INT_CH2;
NSLog (@ "int_ch=%d", int_ch);
BYTES[J] = int_ch; Put the converted number in a byte array
j + +;
}
NSData *newdata = [[NSData alloc] initwithbytes:bytes length:128];
NSLog (@ "newdata=%@", NewData);
3. NSData and UIImageNsdata->uiimage
UIImage *aimage = [UIImage imagewithdata:imagedata];
Example: Taking a picture from a local file sandbox and converting 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); Originally from: http://www.cnblogs.com/jacktu/archive/2011/11/08/2241528.html
NSData and NSString, byte array, UIImage mutual conversion