iOS--NSData and nsstring,byte arrays, UIImage conversion

Source: Internet
Author: User

---restore content starts---

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); Transferred from: http://www.devdiv.com/home.php?mod=space&uid=11511&do=blog&id=8919

---restore content ends---

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); Transferred from: http://www.devdiv.com/home.php?mod=space&uid=11511&do=blog&id=8919

iOS--NSData and nsstring,byte arrays, UIImage conversion

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.