-1

I'm newbie in android. I want to convert float number to binary for sending data to embedded system. I'm sending data through rs232 and I want to send binary data.

My binary data must be 8 bit and plus start and stop bit. What I must do ?

Thanks for your help from now.

EDİT : I didn't only ask how to parse. I also asked that how I can add start and stop bit. It's a fully question.

bzkrtmurat
  • 189
  • 1
  • 3
  • 15
  • 1
    http://stackoverflow.com/a/6970090/4028085 – brso05 Jan 16 '15 at 21:56
  • I said that float number and I want to add start and stop bit to this converted float number @brso05 – bzkrtmurat Jan 16 '15 at 22:07
  • Adding the start and stop bits in software would be very unusual. I would expect to specify the start and stop bit configuration when setting up the channel, and then pass it 8 bit bytes. – Patricia Shanahan Jan 16 '15 at 23:16
  • In order to know that this is a duplicate of the suggested question, one has to know both about either `Double.doubleToLongBits` or `Float.floatToIntBits`, and also that UART interfaces typically handle adding the start and stop bits. That is too big a difference to be a true duplicate, so I am voting to reopen. – Patricia Shanahan Jan 16 '15 at 23:27
  • I didn't only ask how to parse. I also asked that how I can add start and stop bit. It's a fully question. – bzkrtmurat Jan 17 '15 at 11:28
  • While the people who falsely marked your question as a duplicate were grossly mistaken, you are also a bit confused yourself. Start and stop bits are an aspect of asynchronous serial formatting which is added at the serial engine engine level. Android doesn't typically support serial ports, so how you would do that depends a lot on how you intended to accomplish serial interface, for example using USB host APIs to talk to an unspecified USB-serial chip, or using normal Linux serial APIs to talk to a UART on some sort of embedded board running a custom Android install. – Chris Stratton Jan 17 '15 at 22:24

2 Answers2

1

Look at java.nio.ByteBuffer, you create a buffer then put floats in it and get bytes out. However please note that float format may be platform specific. As in representation of a given float number on one platform does not necessarily correspond to representation of the same number on another platform. Your best bet may be to transport in string form and parse, not binary.

Lev Kuznetsov
  • 3,520
  • 5
  • 20
  • 33
  • Yes, that's a possible way to get the raw bytes, and you are very right that binary encodings are often non-portable, and typically a pain to work with compared to textual ones when the ~2x gain in efficiency is not utterly required. The start/stop bits are an entirely separate issue of serial line coding. – Chris Stratton Jan 17 '15 at 22:26
0

All numeric representations on a digital computer are binary representations, so converting from float to binary makes no real sense. A float is however a multi-byte representation, so to send it over RS-232 it must be split into bytes. That is a simple case of casting and indexing:

int send_float( float x )
{
    char* c = (char*)&x ;
    for( int i = 0; i < sizeof(float); i++ )
    {
        send_byte( c[i] ) ;
    }
}

However, that is probably not wise - the binary representation of float and byte order on the receiving platform may differ from that of the transmitting station, so it is generally better to convert the float to ASCII and sent a string:

int send_float( float x )
{
    char a[32] ;
    int length = sprintf( a, "%f", x ) ;
    for( int i = 0; i < length; i++ )
    {
        send_byte( c[i] ) ;
    }
}

With respect to applying start and stop bits, these are applies to the individual bytes independently of the data content, so adding them to the float value makes no sense either. The start/stop bits are added by the UART hardware. While it is possible to bit-bang an asynchronous serial transmission in software, you would not do that in Java and in any case to ensure the critically accurate timing on Android, it would require a kernel level interrupt driven driver and would impose a disproportionately high CPU load.

Clifford
  • 88,407
  • 13
  • 85
  • 165