[英]Converting InputStream data to different datatypes
I have been working with InputStreams in Objective-C and seem to have taken the wrong step into the processing of the received data. 我一直在使用Objective-C中的InputStreams,似乎在处理接收到的数据时采取了错误的步骤。
I am receiving chunks of bytes, which are read and converted to datatypes, as integers, floats, doubles etc. 我正在接收大块字节,这些字节被读取并转换为数据类型,如整数,浮点数,双精度数等。
So far my process is like this: 到目前为止,我的过程是这样的:
readBuffer = (uint8_t *) malloc(4);
memset(readBuffer, 0, 4);
while (length < byteLength) {
length = [InputStream read:readBuffer 4];
}
[something fourByteUint8ToLong:readBuffer];
Now in order to do some converting from the 4-bytes to long 现在为了将4字节转换为long
- (long) fourByteUint8ToLong:(uint8_t *) buffer
{
long temp = 0;
temp |= buffer[0] & 0xFF;
temp <<= 8;
temp |= buffer[1] & 0xFF;
temp <<= 8;
temp |= buffer[2] & 0xFF;
temp <<= 8;
temp |= buffer[3] & 0xFF;
return temp;
}
Is there not an easier way to handle this by using the objective-C classes? 有没有更简单的方法可以通过使用Objective-C类来解决此问题?
In that case how? 在那种情况下怎么办? 8-bytes -> double, 4-bytes -> float.
8字节->两倍,4字节->浮点数。
Thanks in advance. 提前致谢。
Problem solved by using CoreFoundation.h
class function: 使用
CoreFoundation.h
类函数解决的问题:
uint8_t * buffer;
buffer = (uint8_t *) malloc(8);
double tempDouble = CFConvertFloat64SwappedToHost(*((CFSwappedFloat64*)buffer));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.