NSData *theData --> Byte dataByte
方式一:
theData --> NSMutableData *allData
unsigned char *dataByte = CFDataGetMutableBytePtr((__bridge CFMutableDataRef)allData);
// unsigned char *就是Byte 的实际数据类型
方式二:
问度娘
Byte dataByte --> NSData *theData
方式一:
__block NSMutableData * dataM = [NSMutableData data];
[theData enumerateByteRangesUsingBlock:^(const void * _Nonnull bytes, NSRange byteRange, BOOL * _Nonnull stop) {
[dataM appendBytes:bytes length:byteRange.length];
}];
方式二:
问度娘
NSData *theData --> void *dataPtr
方式一:
theData --> NSMutableData *allData
void *dataPtr = allData.mutableBytes;
备注:这里不直接使用void *dataPtr = (void *)theData;
是因为theData.bytes为(const Void *)类型,为了避免精度丢失尽可能使用同一数据类型
方式二:
待产...
void * --> NSData
查看NSData的相关文档即可
NSData *theData --> NSString *dataStr
OC中目前这种方式最靠谱:
theData --> NSMutableData *dataM
char *pChar = (char *)dataM.mutableBytes;
char arrayChar[33];// char数组的长度 = theData.length + 1,
for (int i = 0; i < theData.length; i ++) {
arrayChar[i] = pChar[i];
}
arrayChar[32] = '\0';
dataStr = [NSString stringWithUTF8String:arrayChar];
NSData *theData --> int numInt
方式一:
theData --> NSMutableData *dataM
int *intArr = (int *)dataM.mutableBytes;
numInt = intArr[0];
方式二:(这种方式仅适用小端)
theData --> NSMutableData *dataM
int *intArr = (int *)dataM.mutableBytes;
int numInt = CFSwapInt32(* intArr);