OC 型別轉換

2021-09-26 02:31:49 字數 2992 閱讀 4602

nsdata-> nsstring

nsstring *astring = [[nsstring alloc] initwithdata:adataencoding:nsutf8stringencoding];

nsstring->nsdata

nsstring *astring = @"1234abcd";

nsdata *adata = [astring datausingencoding: nsutf8stringencoding];

2.nsdata 與 byte

nsdata-> byte陣列

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陣列-> nsdata

byte byte = ;

nsdata *adata = [[nsdata alloc] initwithbytes:byte length:24];

byte陣列->16進製制數

byte *bytes = (byte *)[adata bytes];

nsstring *hexstr=@"";

for(int i=0;i<[encrydata length];i++)

nsstring *newhexstr = [nsstring stringwithformat:@"%x",bytes[i]&0xff];///16進製制數

if([newhexstr length]==1)

hexstr = [nsstring stringwithformat:@"%@0%@",hexstr,newhexstr];

else 

hexstr = [nsstring stringwithformat:@"%@%@",hexstr,newhexstr];

nslog(@"bytes 的16進製制數為:%@",hexstr);

16進製制數->byte陣列

/ 將16進製制資料轉化成byte 陣列

nsstring *hexstring = @"3e435fab9c34891f"; //16進製制字串

int j=0;

byte bytes[128];  ///3ds key的byte 陣列, 128位

for(int i=0;i<[hexstring length];i++)

int int_ch;  /// 兩位16進製制數轉化後的10進製數

unichar hex_char1 = [hexstring characteratindex:i]; 兩位16進製制數中的第一位(高位*16)

int int_ch1;

if(hex_char1 >= '0' && hex_char1 <='9')

int_ch1 = (hex_char1-48)*16;    0 的ascll - 48

else if(hex_char1 >= 'a' && hex_char1 <='f')

int_ch1 = (hex_char1-55)*16; a 的ascll - 65

else 

int_ch1 = (hex_char1-87)*16; a 的ascll - 97

i++;

unichar hex_char2 = [hexstring characteratindex:i]; ///兩位16進製制數中的第二位(低位)

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; a 的ascll - 65

else 

int_ch2 = hex_char2-87; a 的ascll - 97

int_ch = int_ch1+int_ch2;

nslog(@"int_ch=%d",int_ch);

bytes[j] = int_ch;  ///將轉化後的數放入byte陣列裡

j++;

nsdata *newdata = [[nsdata alloc] initwithbytes:bytes length:128];

nslog(@"newdata=%@",newdata);

3. nsdata 與 uiimage

nsdata->uiimage

uiimage *aimage = [uiimage imagewithdata: imagedata];

nsstring *path = [[nsbundle mainbundle] bundlepath];

nsstring *name = [nsstring stringwithformat:@"ceshi.png"];

nsdata *imagedata = [nsdata datawithcontentsoffile: finalpath];

uiimage *aimage = [uiimage imagewithdata: imagedata];

uiimage-> nsdata

nsdata *imagedata = uiimagepngrepresentation(aimae); 

nsstring *str = @"aa21f0c1762a3abc299c013abe7dbcc50001dd";

nsdata* bytes = [str datausingencoding:nsutf8stringencoding];

byte * mybyte = (byte *)[bytes bytes];

IOS之 OC 和 C型別轉換

在oc和 c之間互相呼叫的時候,有時候必須用一些橋接關鍵字.bridge只做型別轉換,但是不修改物件 記憶體 管理權 bridge retained 也可以使用cfbridgingretain 將objective c的物件轉換為core foundation的物件,同時將物件 記憶體 的管理權交給...

OC多型 指標變數的強制型別轉換

main.m 指標變數強制型別轉換和判斷指標變數的實際型別 created by goddog on 15 1 8.1.除了id之外,指標變數只能呼叫它編譯時型別的方法,不能呼叫它執行時型別的方法,故強轉。2.為類保證程式能正常執行,一般建議執行強轉之前先判斷該物件是否為該類或其子類。3.bool ...

OC 資料型別之間的轉換方法

nsnumber轉nsstring 假設現有一nsnumber的變數a,要轉換成nsstring型別的b 方法如下 nsnumberformatter numberformatter nsnumberformatter alloc init b numberformatter stringfromn...