NSData 和 NSMutableData,代表著object-c 的數據緩衝區。
因在對讀/寫BLE,其均為NSData為其對象。
所以,必須介紹這個資料類別的相關使用
NSData的作用:
1.對數據讀取NSData;
2.輸出NSData
A.建立
dataxxx: 類別
initxxxx: 實例
data:建出一個不含任何資料的,空的NSData
dataWithByte:length /initWithBytes:length: 複制C array來初始始化NSData
dataWithNoCopyByte:length /initWithNoCopyBytes:length:
直接利用C array中資料來初始代。只有nsdata 執行relloc才會free C array
dataWithByteNoCopy:length:freeWhenDon: /initWithBytesNoCopy:length:freeWhenDon:
同上,但只有當 最後的參數為YES。且NSData以relloc,程式才會free C array
dataWithContentsOfFile:/initWithContentsOfFile
讀取文件內容並初始化NSData
dataWithContentOfURL:/initWithContentOfURL:
讀取URL文件內容並初始化NSData
dataWithData:/initWithData:
使用另一個NSData來初始化
read/write NSData
bytes: 返回其NSdata數值
getBytes:length: 取得指定長度的資料
getBytes:range: 取得指定範圍的資料
subdataWithRange: 取得指定範圍的資料並組成另一NSData
writeToFile:atomically: 將NSData 寫入 文件
writeToURL:atomically: 將NSData 寫入URL 文件
B.各資料轉換
1. NSData 與 NSString
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[] = {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數組->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];
//例:從本地文件沙盒中取圖片並轉換为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);
http://fanli7.net/a/JAVAbiancheng/j2ee/2012/0519/165001.html
沒有留言:
張貼留言