漂浮到chars和chars浮动 [英] Floats to chars and chars to floats

查看:76
本文介绍了漂浮到chars和chars浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-Hello,


我正在寻找一种方法将(32位)浮点值解构为4

(8位)char值。然后我需要一种方法来获取这四个(8位)char

值并构造一个(32位)浮点值。


我问的原因是我正在通过rs485串行

网络发送内容,并且协议设置为逐字节发送。它一次发送一个

(8位)字符。我想以四个字符的形式在这个

网络上发送一个浮动...


任何想法,建议? (顺便说一句:这是柏树的C编译器

微控制器,而不是C ++编译器)


谢谢,

Aaron

解决方案

Kosio写道:

- 你好,

我是寻找一种方法将(32位)浮点值解构为四个
(8位)char值。然后我需要一种方法来获取这四个(8位)char值/并构造一个(32位)浮点值。

我问的原因是我在rs485上发送东西串行
网络,协议设置为逐字节发送。它一次发送一个
(8位)字符。我想以四个字符的形式在这个
网络上发送一个浮动...

任何想法,建议? (顺便说一句:这是一个C编译器,用于cypress
微控制器,而不是C ++编译器)




首先,我希望你理解一个字节和一个

八位字节。在C中,一个字节必须至少有8位,但可能有更多。在包含标题< limits.h>后,使用

宏CHAR_BIT可以在编译时确定一个字节中的
位数。


从你的描述来看,听起来这个函数应该发送8位

块,即使CHAR_BIT超过8个。


#include< limits.h>


void serialise_float(浮点值)

{

unsigned char * p = (unsigned char *)& value;

size_t i,j;

for(i = 0; i< sizeof value; i ++){

for(j = 0; j<(CHAR_BIT + 7)/ 8; j ++)

send(p [i]>> j * 8& 0xFF);

}

}


替换发送使用您需要用来通过此串行连接发送

字节的函数的名称。


它将以本机表示形式发送浮点数,char通过char和

以8位八位字节发送每个字符,首先是最不重要的八位字节。


编写一个函数来在另一台机器上反序列化这些数据需要

你要知道浮点数据的确切二进制格式,以及原始机器上CHAR_BIT的

值是多少。


如果您可以假设CHAR_BIT为8,并且''float''在每台机器上具有相同的

二进制格式,那么这样的函数就足够了:


float deserialise_float(void)

{

浮点值;

unsigned char * p =(unsigned char *)& value ;

size_t i;


for(i = 0; i< sizeof value; i ++)

{

p [i] =接收();

}


返回值;

}

更换接收使用从串行连接接收字节

的函数的名称。


我的代码未经测试,仅用作示例。


-

Simon。




Kosio写道:< blockquote class =post_quotes> -Hello,

我正在寻找一种方法将(32位)浮点值解构为四个
(8位)char值。然后我需要一种方法来获取这四个(8位)char值/并构造一个(32位)浮点值。

我问的原因是我在rs485上发送东西串行
网络,协议设置为逐字节发送。它一次发送一个
(8位)字符。我想以四个字符的形式在这个
网络上发送一个浮动...

任何想法,建议? (顺便说一下:这是一个C编译器,用于cypress
微控制器,而不是C ++编译器)

谢谢,
Aaron




以下假定通信线路的两端都是相同的字节性别,并使用相同的浮点数表示:


/ * sender * /

浮动theValue;

unsigned char * bytes =(unsigned char *)& theValue;

int i;

for(i = 0; i< sizeof theValue; i ++)

{

send(bytes [i]);

}


/ *接收器* /

浮动theValue;

unsigned char bytes [sizeof theValue];

int i;

for(i = 0; i< sizeof theValue; i ++)

{

receive(& bytes) [i]);

}

theValue = *((float *)bytes);


快速,脏,和不安全,但它应该给你一个开始的地方。

以上代码基本上将theValue视为unsigned char数组。你可能想要为两端开发一个更强大的协议,

需要不同的endianess和代表帐户。


请注意,我对串行通信知之甚少,所以这可能不是你想要的。


感谢建议,我会尝试实施它并让你知道它是如何工作的。


-Hello,

I''m looking for a way to deconstruct a (32 bit) float value to four
(8 bit) char values. I then need a way to take those four (8 bit) char
values and construct a (32 bit) float value.

The reason I ask is that I am sending things across an rs485 serial
network, and the protocol is set up to send byte by byte. It sends one
(8 bit) char at a time. I would like to send a float across this
network in the form of four chars...

Any ideas, suggestions? (Btw: this is a C compiler for a cypress
microcontroller, not a C++ compiler)

Thanks,
Aaron

解决方案

Kosio wrote:

-Hello,

I''m looking for a way to deconstruct a (32 bit) float value to four
(8 bit) char values. I then need a way to take those four (8 bit) char
values and construct a (32 bit) float value.

The reason I ask is that I am sending things across an rs485 serial
network, and the protocol is set up to send byte by byte. It sends one
(8 bit) char at a time. I would like to send a float across this
network in the form of four chars...

Any ideas, suggestions? (Btw: this is a C compiler for a cypress
microcontroller, not a C++ compiler)



Firstly, I hope you understand the difference between a byte and an
octet. In C, a byte must have at least 8 bits, but may have more. The
number of bits in a byte can be determined at compile-time by using the
macro CHAR_BIT after including the header <limits.h>.

From your description, it sounds like this function should send 8-bit
chunks, even if CHAR_BIT is more than 8.

#include <limits.h>

void serialise_float(float value)
{
unsigned char *p = (unsigned char *)&value;
size_t i, j;
for(i = 0; i < sizeof value; i++) {
for(j = 0; j < (CHAR_BIT+7)/8; j++)
send(p[i] >> j * 8 & 0xFF);
}
}

Replace "send" with the name of the function you need to use to send a
byte over this serial connection.

It will send the float in the native representation, char by char, and
send each char in 8-bit octets, least significant octet first.

Writing a function to deserialise this data on another machine requires
you to know the exact binary format of the float data, and what the
value of CHAR_BIT was on the original machine.

If you can assume that CHAR_BIT was 8, and that ''float'' has the same
binary format on each machine, then a function like this should suffice:

float deserialise_float(void)
{
float value;
unsigned char *p = (unsigned char *)&value;
size_t i;

for(i = 0; i < sizeof value; i++)
{
p[i] = receive();
}

return value;
}

Replacing "receive" with the name of the function that receives a byte
from the serial connection.

My code is untested and is only intended to serve as an example.

--
Simon.



Kosio wrote:

-Hello,

I''m looking for a way to deconstruct a (32 bit) float value to four
(8 bit) char values. I then need a way to take those four (8 bit) char
values and construct a (32 bit) float value.

The reason I ask is that I am sending things across an rs485 serial
network, and the protocol is set up to send byte by byte. It sends one
(8 bit) char at a time. I would like to send a float across this
network in the form of four chars...

Any ideas, suggestions? (Btw: this is a C compiler for a cypress
microcontroller, not a C++ compiler)

Thanks,
Aaron



The following assumes that both ends of the communication line are the
same byte sex and use the same representation for float:

/* sender */
float theValue;
unsigned char *bytes = (unsigned char *)&theValue;
int i;
for (i = 0; i < sizeof theValue; i++)
{
send(bytes[i]);
}

/* receiver */
float theValue;
unsigned char bytes[sizeof theValue];
int i;
for (i = 0; i < sizeof theValue; i++)
{
receive(&bytes[i]);
}
theValue = *((float *) bytes);

Quick, dirty, and unsafe, but it should give you a place to start. The
above code basically treats theValue as an array of unsigned char. You
will probably want to develop a more robust protocol for both ends that
takes different endianess and representation in account.

Note that I know less than nothing about serial comms, so this may not
be what you want.


Thanks for the advice, I''ll try to implement it and let you know how it
works.


这篇关于漂浮到chars和chars浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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