View previous topic :: View next topic |
Author |
Message |
jasques
Joined: 30 Dec 2007 Posts: 4
|
Posted: Sat Jan 16, 2010 3:29 pm Post subject: How to create Single from 4 byte array? |
|
|
Hi,
I'm currently working with Vector digital compass and heading information is returned as 4 bytes representing Float (in BASCOM it is Single).
But I can't manage to convert it, I have tried memcopy, out etc. Please help! |
|
Back to top |
|
|
MAK399
Joined: 23 Jun 2009 Posts: 110 Location: Germany
|
Posted: Sat Jan 16, 2010 3:57 pm Post subject: |
|
|
Hi jasques,
here is an example with Overlay:
Code: | dim SingleVar as single
Dim Bytevar1 As Byte At Singlevar Overlay
Dim Bytevar2 As Byte At Singlevar + 1 Overlay
Dim Bytevar3 As Byte At Singlevar + 2 Overlay
Dim Bytevar4 As Byte At Singlevar + 3 Overlay |
I'ts from here:
http://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&p=36696#36696
MAK399 _________________ Using BASCOM 1.12.0.0 |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sat Jan 16, 2010 4:02 pm Post subject: |
|
|
You may use:
Code: | Dim Float_bytes(4) As Byte
Dim Single_val As Single At Float_bytes(1) Overlay |
But your float has to have the same layout, 23 bit value, 8 bit mantissa, 1 bit sign, and of course the alignment of the bytes in the array has to be: LWord(LByte, HByte), HWord(LByte, HBite), the same as it would be in memory.
Edit: was a bit slower, but my approach seems the easier one |
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Sat Jan 16, 2010 4:18 pm Post subject: |
|
|
This gives a way to test the results on the simulator.
Code: |
$regfile = "m644pdef.dat"
$crystal = 8000000
$baud = 9600 'baud rate for all serial communications
$hwstack = 48 ' default use 32 for the hardware stack
$swstack = 16 ' default use 10 for the SW stack
$framesize = 48 ' default use 40 for the frame space
$sim
Dim Ii As Byte
Dim S As Single , Sb(4) As Byte At S Overlay
Do
Input "enter byte 1: " , Sb(1)
Input "enter byte 2: " , Sb(2)
Input "enter byte 3: " , Sb(3)
Input "enter byte 4: " , Sb(4)
Print S
Input "enter single: " , S
For Ii = 1 To 4 : Print Sb(ii) ; " " ; : Next : Print : Print
Loop
End 'end program
|
Ennio |
|
Back to top |
|
|
jasques
Joined: 30 Dec 2007 Posts: 4
|
Posted: Sat Jan 16, 2010 5:30 pm Post subject: |
|
|
Hey!
many thanks, I believe this solves my issue I totally forgot about overlaying. |
|
Back to top |
|
|
|