Shop OBEX P1 Docs P2 Docs Learn Events
how do i convert a float value into a 4byte array! Solved! — Parallax Forums

how do i convert a float value into a 4byte array! Solved!

AnubisbotAnubisbot Posts: 112
edited 2009-02-14 01:54 in Propeller 1
Hi i got stuck , and the search in the forum, turned out 0.

Ok i have my gps float numbers and need to send them over serial out via full duplex driver.
I know that a float can be packed in 4 bytes but how??
Is there a code example some where ??

best regards Anubisbot

Post Edited (Anubisbot) : 2/14/2009 1:19:11 AM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-13 21:25
    It's simple, so there are no examples. If you have a long called XXX, you can refer to the individual bytes as XXX.byte[noparse][[/noparse] i ] where "i" is a value from 0 to 3 with 0 being the low order byte and 3 the high order byte. If your FullDuplexSerial object is called "ser", you'd use:
    for i from 0 to 3
       ser.tx(XXX.byte[noparse][[/noparse] i ])
    


    alternatively:
    for i from 3 to 0
       ser.tx(XXX.byte[noparse][[/noparse] i ])
    


    You can also use the FloatString object to convert the floating point number to a string like -23.56 and send that with FullDuplexSerial.

    Post Edited (Mike Green) : 2/13/2009 9:31:29 PM GMT
  • AnubisbotAnubisbot Posts: 112
    edited 2009-02-13 22:05
    I know how to send it , but my problem is :
    fv := GPS.Float_Latitude_Deg
    



    i need to get the value fv what is a flaot into my

    Byte sendLA
    


    so how do i convert the float number into the 4 bytes ??

    Best regards Anubisbot
  • AnubisbotAnubisbot Posts: 112
    edited 2009-02-13 23:30
    that would be great..
    and then i need to get the 4 bytes back to a float in c++ ???

    Best regards
  • JasonDorieJasonDorie Posts: 1,930
    edited 2009-02-13 23:43
    Look at Mike's code again, replacing the word 'long' with the word 'float'. The prop doesn't know the difference, so you can access each byte in the float/long variable using the code Mike posted above, no shifting required. There's no need to 'convert' the number - you can simply use the serial.tx command passing in fv.byte[noparse][[/noparse]n] where n is a number from 0 to 3. That will send the n-th byte of the float/long.

    Jason

    Post Edited (JasonDorie) : 2/13/2009 11:49:45 PM GMT
  • AnubisbotAnubisbot Posts: 112
    edited 2009-02-14 00:16
    yes of course, why didnt i see that bevore [noparse]:)[/noparse] now i understand cool thats simple, so what it does it grabs the bytes out of the memory [noparse]:)[/noparse]
    So in my c++ i need to join them them with memcpy() ?? hopefully.. Great thanx mike...
  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-14 01:25
    The only problem with this solution is that you may not know what floating point format your C++ uses. The Propeller's floating point object uses standard IEEE single precision format numbers. Most modern computers do use the IEEE format, so that should be fine. If it doesn't work, check your C++ manual.
  • AnubisbotAnubisbot Posts: 112
    edited 2009-02-14 01:54
    it works like a charm [noparse]:)[/noparse] i got now my RC remote link setup and recive now my GPS data on the pc via xbee, and also i send RC data from the pc to the prop, and that all 45 times a sec. juhu...

    Thanx again[noparse]:)[/noparse]
Sign In or Register to comment.