The 1.NSDictionary type is converted to the NSData type:
- Nsdictionary *params = [Nsdictionary Dictionarywithobjectsandkeys:
- @ "balance", @"key",
- @"remaining balance", @"label",
- @ "," @"value",
- @ "USD" @ "CurrencyCode", nil];
- Nsmutabledata *data = [[Nsmutabledata alloc] init];
- Nskeyedarchiver *archiver = [[Nskeyedarchiver alloc] initforwritingwithmutabledata:data];
- [Archiver encodeobject:params forkey:@"Some Key Value"];
- [Archiver finishencoding];
The 2.NSData type is converted to the Nsdictionary type:
- NSData-Nsdictionary
- NSData *data = [[Nsmutabledata alloc] initwithcontentsoffile:[self DataFilePath]];
- Nskeyedunarchiver *unarchiver = [[Nskeyedunarchiver alloc] initforreadingwithdata:data];
- Nsdictionary *mydictionary = [[Unarchiver decodeobjectforkey:@"Some Key Value"] retain];
- [Unarchiver finishdecoding];
- [Unarchiver release];
- [Data release];
The 3.NSDictionary type is converted to the JSON data type:
- Nsdictionary-JSON:
- NSString *jsonstr=[dict Jsonrepresentation];
Note that JSON and nsdictionary types typically use JSON libraries that are exposed by third parties, which readers can find on the web.
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 UIImage
Nsdata->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);
IOS nsdictionary, NSData, JSON and other data types convert each other