随笔-142  评论-23  文章-3  trackbacks-0

BYTE* To Float*

char c = *(char*)pByte;
short shrt = *(short*)(pByte+1);
long lng = *(long*)(pByte+3);
float flt = *(float*)(pByte+7);
string str = (char*)(pByte+11);

 

JohnEx:

You are not converting char to float; rather you are interpreting a sequence of 4 bytes as a float. Not the same thing at all. There is no need to first interpret the sequence of bytes as a string, or to use memcpy.

You have a BYTE stream, say pByte (a BYTE*), and the float value is at positions 7 through 10

float f = *(float*)(pByte+7);

Similarly for the other values

char c = *(char*)pByte;
short shrt = *(short*)(pByte+1);
long lng = *(long*)(pByte+3);
float flt = *(float*)(pByte+7);
string str = (char*)(pByte+11);

(assuming the string part is correctly null-terminated). I would write a single function to write these values into a struct, so the rest of the code can be clean.

posted on 2011-11-19 15:03 3D入魔 阅读(13) 评论(0) 编辑 收藏
昵称:3D入魔
园龄:6年6个月
粉丝:6
关注:0
<2011年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

搜索

 
 

常用链接

随笔分类

随笔档案

文章分类

Direct3D技术

OpenGL技术

VC技术

百家杂学

开源软件

理财知识

其它技术

企业文化及管理

最新评论

阅读排行榜

评论排行榜

推荐排行榜