类型从float转换为unsigned char * [英] type cast from float to unsigned char*

查看:1004
本文介绍了类型从float转换为unsigned char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此代码的问题是当我尝试调用WriteHeadAndBuffer(kwdId,sizeof(float),(unsigned char*)&f);
在下面的代码中,类型强制转换为(unsigned char*)&f后,我没有得到正确的值(不知道它是unicode还是垃圾).

可以像这样将强制类型float转换为unsigned char* ...
我正在使用vs2008.



The problem that i m getting in this code is when i try to call WriteHeadAndBuffer(kwdId,sizeof(float),(unsigned char*)&f);
in the below code i am not getting correct value(Don''t know what it is unicode or garbage) after type cast (unsigned char*)&f.

Is is possible to type cast float to unsigned char* like this...
I am using vs2008.



void CQ3DStreamFile::Write(const unsigned long kwdId,const char* keyword,float f)
{
    if (!CheckStream(kOpenedWrite))
        return;

    if (m_Binary)
    {
        WriteHeadAndBuffer(kwdId,sizeof(float),(unsigned char*)&f);
    }
    else
    {
        doWriteIndent();
        fprintf(m_File,"%s\t%s\n",keyword,STRFLOAT(f));
    }
}



[修改:添加的代码格式]



[Modified: added code formatting]

推荐答案

如果我正确地理解了您的意图,那么您会误解类型强制转换及其用途.您的变量f包含浮点数的二进制表示形式.然后,您将该变量的地址从float *强制转换为char *.这对f的内容没有任何作用.现在,您有了一个指针,要求编译器考虑一个指向char的指针,该指针不指向任何种类.

显然,您想要的是f中值的字符表示.您的代码中目前还没有这样的东西. C或C ++强制转换运算符都与创建这样的东西无关.

如果我误解了您要做什么,请澄清您的问题.

周围有很多事情可以用来做到这一点.您可能会看到的一些可能性: CString :: Format() [ ^ ], sprintf() [ boost :: lexical_cast [此处 [
If I am understanding your intent correctly, you are misunderstanding type casts and what they do. Your variable f contains a binary representation of a floating point number. You are then casting the address of that variable from float * to char *. This does nothing whatsoever to the contents of f. You now have a pointer that you have demanded that the compiler consider a pointer to char that points to nothing of the sort.

Evidently, what you want is a character representation of the value in f. You have no such thing in your code at present. Neither a C or a C++ cast operator is relevant to creating such a thing.

If I am misunderstanding what you are trying to do, please clarify your question.

There are a number of things around that can be used to do this. Some posibilites that you might look at: CString::Format()[^], sprintf()[^], boost::lexical_cast[^], and std::ostringstream which can be used similarly to std::cout. An article that discusses these is here[^].

Most of these work with chars and not unsigned chars, so you may need a cast from char * to unsigned char *. Also what CString will give you is a const char *.

Note: I have been assuming here that you are not using a unicode build and do not have to worry about conversions to/from a unicode encoding.


我想,您想执行二进制文件浮标转储,

但是写作可能确实完成了
在float内容的第一个0字节... :)

您可以发布WriteHeadAndBuffer(..)的实现吗?
I guess, you want to perform a binary dump of a float,

but the writing does probably finish
at the first 0-byte of the floats content... :)

Could you post the implementation of WriteHeadAndBuffer(..) ?


这篇关于类型从float转换为unsigned char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆