蓝牙与硬件交互的项目,遇到这样需要求,蓝牙返回的数据,带有符号 (-号)
- (NSString *)decimalwithFormat:(NSData *)dataV {
if (dataV == nil) {
return @"0";
}
NSData *ff = [DataConvert hexStringToData:@"FFFFFFFF"];
NSInteger addupInt;
NSString *resultStr;
float addupF = 0.00;
if ([[dataV subdataWithRange:NSMakeRange(0, 1)] isEqualToData:[DataConvert hexStringToData:@"ff"]]) {
addupInt = [DataConvert dataToInteger:ff] - [DataConvert dataToInteger:dataV];
addupF = (addupInt + 1) * 0.01;
resultStr = [NSString stringWithFormat:@"-%.2f",addupF];
} else {
addupInt = [DataConvert dataToInteger:dataV];
addupF = addupInt * 0.01;
resultStr = [NSString stringWithFormat:@"%.2f",addupF];
}
NSLog(@"格式化数据 -- %@",resultStr);
return resultStr;
}