學(xué)習(xí)Tips
-
ios
中將16進(jìn)制轉(zhuǎn)換為浮點(diǎn)型
NSString *temp = [NSString stringWithFormat:@"%@%@",@"0x",[[[temp0 stringByAppendingString:temp1] stringByAppendingString:temp2] stringByAppendingString:temp3]];
const char *str = NULL;
if ([temp canBeConvertedToEncoding:NSUTF8StringEncoding]) {
str = [temp cStringUsingEncoding:NSUTF8StringEncoding];
}
uint32_t num;
float f;
sscanf(str, "%x", &num); // assuming you checked input
f = *((float*)&num);
printf("the hexadecimal 0x%08x becomes %.3f as a float\n", num, f);
-
CRC16-MODBUS
校驗(yàn)(OC
版本代碼)
+ (NSString *)getCrc16_MODBUSWithString:(NSString *)str{
NSArray *array = [[NSArray alloc]initWithArray:[self seperateStr:str byLength:2]];
unsigned short tmp = 0xffff;
unsigned short ret1 = 0;
Byte buff[10240] = {};
for (int i = 0; i < array.count; i++) {
buff[i] = [[self decimalStringFromHexString:array[i]] intValue];// 這個(gè)是10進(jìn)制轉(zhuǎn)換成16進(jìn)制
}
for(int n = 0; n < array.count; n++){
tmp = buff[n] ^ tmp;
for(int i = 0;i < 8;i++){ /*此處的8 -- 指每一個(gè)char類型又8bit,每bit都要處理*/
if(tmp & 0x01){
tmp = tmp >> 1;
tmp = tmp ^ 0xa001;
}
else{
tmp = tmp >> 1;
}
}
}
/*CRC校驗(yàn)后的值*/
printf("校驗(yàn)后的值%X\n",tmp);
/*將CRC校驗(yàn)的高低位對(duì)換位置*/
ret1 = tmp >> 8;
ret1 = ret1 | (tmp << 8);
printf("高低位對(duì)換后的值ret: %X\n",ret1);
NSString *returnStr = [NSString stringWithFormat:@"%X",ret1];
return returnStr;
}
工具推薦
地址 https://www.gaituya.com/
地址 https://wallhaven.cc/
地址 https://www.fontspace.com/
地址 https://www.iodraw.com/
地址 https://www.cxy521.com