Announcement

Collapse
No announcement yet.

How to conver 4 single bytes to 32-bit?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to conver 4 single bytes to 32-bit?

    Hi,

    Could anyone help me out to show how to convert 4 single bytes to 32-bit long integer?

    The Hex values I use is 30 48 02 62 (48 72 02 98 in Dec)

    This is a date and I know the answer is 08/04/30 or 0x080430 in long integer (or long value).

    I use PB DLL 8.04
    Last edited by Natalie Angel; 15 Dec 2008, 02:54 AM.
    If you don't know what the problem is, then you can't find the answer.

  • #2
    Not sure how you exactly want to organise the bytes but maybe like this:

    Code:
    LOCAL sBytes as STRING
    LOCAL lNum as LONG
    
    sBytes = "01FFAABB"
    lNum = VAL("&h"+sBytes)
    Kind regards
    Eddy

    Comment


    • #3
      Outcome is wrong

      Hi Eddy,

      Thank you for this but this and others was the first thing I tried and if I use your sample like this;

      Dim sBytes As String
      Dim lNum As Long

      sBytes = "30480262"
      lNum = Val("&h" + sBytes)

      or like this

      lNum = Val("&H 30480262&")

      I get 810025570 which is wrong as the answer is the date 08/04/30 or 0x080430 (2008 April 30)

      In the same way I've got 20 01 ce 64 or 32 01 206 106 in Dec and this is the time 20:02:26 so the decimal value is 0x200226 but I don't know how to add the 4 bytes to become a 32-bit value which for sure is not just to add them together as the value is then wrong?

      Don't know if this helps but the PDF that describes this protocol says the 4 hex is in little endian format in uint32 which makes me think of C conversation?

      OK I tried lNum = CLng(Val("&H30480262&")) with the same result
      Last edited by Natalie Angel; 15 Dec 2008, 03:43 AM.
      If you don't know what the problem is, then you can't find the answer.

      Comment


      • #4
        This seems more like a format problem.
        Do you know the format of the hex bytes? How to convert them from hex to a date ? Or maybe this is exactly your question ..?

        Kind regards
        Eddy

        Comment


        • #5
          Hi Eddy,

          Yes that is my question. This is used with communication to a GPS receiver and to ask for Firmware version. as a reply from the unit I get a long string where the date and time is in 32-bit while the other data is in 8-bit as it's only ascii code.

          Somebody told be once to convert 4 single bytes to 32-bit you need first to convert to binary and then use rotate bit? But I'm sure there must be a more simple way than that...
          If you don't know what the problem is, then you can't find the answer.

          Comment


          • #6
            Maybe this way ?

            Attila,

            If it is about a date format it is not inconceivable that fractions of bytes are used.
            As your data in hex is 30 48 02 62, and you know the result should be 08 04 30, the solution could be to reverse the order of the bytes
            to 62 02 48 30, and to assume that 30 indeed occupies a byte, but that 4 and 8 each occupy a half byte.
            This could be possible, as month do not go over 12, and for years a 10 year period might be used.

            Arie Verheul

            Comment


            • #7
              Arie, don't forget that this is in HEX not DEC so if you look at the time sample it's 20 01 ce 64 and this is the time 20:02:26 or 0x200226 in decimal so it can't be in reversed order as this is HEX 30E22

              For the date sample the HEX value of the known answer 0x080430 is 13A2E so I know I must convert 30 48 02 62 in HEX to become 13A2Eh but how?

              ADDED!!!! ==========>

              OK I could speak with the manufacturer of the module and they say in fact the date and time is HEX values so the date should be 00 08 04 30 in HEX and not decimal, easy as that so the problem seams to be about missing "0"'s or something when I read the incoming data from the receiver or something so the HEX values get shifted which makes Arie quite right so let's see how it goes...
              Last edited by Natalie Angel; 15 Dec 2008, 06:19 AM.
              If you don't know what the problem is, then you can't find the answer.

              Comment


              • #8
                >The Hex values I use is 30 48 02 62 (48 72 02 98 in Dec)
                >This is a date and I know the answer is 08/04/30 or 0x080430 in long integer (or long >value

                What is format of "08/04/30" as you describe it??

                MM/DD/YY
                DD/MM/YY
                YY/MM/DD
                YY/DD/YY

                You don't say where you are from but if you are more familiar with the European format dates (DD MM YY) then you may have a lot of folks on this side of the pond chasing their tails.

                MCM
                Michael Mattias
                Tal Systems Inc. (retired)
                Racine WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  Depending on what you want to accomplish, this might be a good time to use a UNION. With this structure you can easily read/write the individual bytes of a LONG or DWORD while still having easy access to the full 32-bit value.
                  Erich Schulman (KT4VOL/KTN4CA)
                  Go Big Orange

                  Comment


                  • #10
                    Originally posted by Attila Fiko View Post
                    Hi,

                    Could anyone help me out to show how to convert 4 single bytes to 32-bit long integer?

                    The Hex values I use is 30 48 02 62 (48 72 02 98 in Dec)

                    This is a date and I know the answer is 08/04/30 or 0x080430 in long integer (or long value).

                    I use PB DLL 8.04
                    I don't see any way to get that date out of those bytes.
                    Code:
                    Local d, d1, d2, d3 As Long, s As String
                    d = Val("&h30480262") 
                    d1 = Val("&h62024830") 'reverse order
                    s$ = "08/04/30 " & $CrLf & $CrLf & Str$(d) & $CrLf & Using$("#,", d) & $CrLf & "Reverse order" & $CrLf & Str$(d1) & $CrLf & Using$("#,", d1)
                    ClipBoard Set Text s$ To d
                    ? s$
                    'shows
                    '08/04/30 
                    '
                    ' 810025570
                    '810,025,570
                    'Reverse order
                    ' 1644316720
                    '1,644,316,720
                    =================================================
                    "The nice thing about being a celebrity is
                    if you bore people they think it's their fault."
                    Henry Kissinger (1923-)
                    =================================================
                    It's a pretty day. I hope you enjoy it.

                    Gösta

                    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                    Comment


                    • #11
                      OK so I solved the problem but thank you all for your help anyway.

                      But I will give the solution here as it might help others working with HEX data.

                      When I did read the incoming data HEX values like "06" it was converted as "6" so when I added each value together I was loosing some zeros, not always because I had zeros in the final string too, but the right way for me to convert the binary ASCII data to HEX was to use this;

                      (sConvert2HEX is a string and sTmp is the incoming ASCII character [can be anything from 0 to 255] - one by one)

                      sConvert2HEX = sConvert2HEX & Right$("00" & Hex$(Asc(sTmp)), 2)

                      When it was converted wrong I was using;

                      sConvert2HEX = sConvert2HEX & Hex$(Asc(sTmp)) so a BIG WARNING on this one :wavey:

                      Hope it can help
                      Last edited by Natalie Angel; 15 Dec 2008, 09:26 AM.
                      If you don't know what the problem is, then you can't find the answer.

                      Comment


                      • #12
                        Just one more thing so you can see the problem.

                        The wrong string was using
                        sConvert2HEX = sConvert2HEX & Hex$(Asc(sTmp))
                        2525F280504C4E3032323035384E3034304802622001CE6A01CE65

                        The final and correct string for the date was using
                        sConvert2HEX = sConvert2HEX & Right$("00" & Hex$(Asc(sTmp)), 2)
                        2525F280504C4E3032323035384E303430040800260220001CE6A5

                        What a difference it can make and can you spot the date and time?

                        It's 30040800 (30/04/2008) (4 bytes in HEX) for the date and 26022000 (20:02:26) for the time
                        If you don't know what the problem is, then you can't find the answer.

                        Comment


                        • #13
                          Originally posted by Attila Fiko View Post

                          What a difference it can make and can you spot the date and time?

                          It's 30040800 (30/04/2008) (4 bytes in HEX) for the date
                          Please show (working) code how you arrive at the date. I dont see how myself. Thanks.

                          ========================================
                          "Women might be able to fake orgasms.
                          But men can fake a whole relationship."
                          Sharon Stone
                          ========================================
                          It's a pretty day. I hope you enjoy it.

                          Gösta

                          JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                          LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                          Comment


                          • #14
                            The data comes from a GPS receiver based on a request command to request data like in this case firmware version.

                            The data arrives in binary as ASCII code and the only thing I do is to loop trugh the receiving string converting one-by one character to HEX using the string above.
                            If you don't know what the problem is, then you can't find the answer.

                            Comment


                            • #15
                              Originally posted by Attila Fiko View Post
                              The data comes from a GPS receiver based on a request command to request data like in this case firmware version.

                              The data arrives in binary as ASCII code and the only thing I do is to loop trugh the receiving string converting one-by one character to HEX using the string above.
                              But don't you then have to convert the ASCII characters to Hex bytes which you then converty to a Date? I am at a loss how "30040800" gets converted to "30/04/2008" whether it's in Hex, Decimal, Ascii or Swahili.

                              "30" & "04" I can see (decimal). It's the "0800" that has me hung up. If Hex it's equivalent to year 2048 in decimal.

                              Can you just post the code snippet that does that? Thanks.
                              ====================================
                              "Beware of geeks bearing formulas.”
                              Warren Buffett (1930)
                              ====================================
                              It's a pretty day. I hope you enjoy it.

                              Gösta

                              JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                              LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                              Comment


                              • #16
                                OK I see what you mean. the 4th byte is not used so "08" is 2008 and I don't know why they use 4 bytes but it's given in UINT32 (in C) so I guess that's the reason as the microcontroller used is 32-bit.

                                Part of my code that reads the data from the GPS receiver goes like this;

                                sConvert2HEX = ""
                                For iLoop = 1 To Len(sTempGetComBuffer)
                                sTmp = Mid$(sTempGetComBuffer, iLoop, 1)
                                sConvert2HEX = sConvert2HEX & Right$("00" & Hex$(Asc(sTmp)), 2)
                                Next iLoop

                                where sTempGetComBuffer is what I got from the COM-port terminated with CRLF. :wavey:
                                Last edited by Natalie Angel; 15 Dec 2008, 09:22 PM.
                                If you don't know what the problem is, then you can't find the answer.

                                Comment


                                • #17
                                  Originally posted by Gösta H. Lovgren-2 View Post
                                  But don't you then have to convert the ASCII characters to Hex bytes which you then converty to a Date? I am at a loss how "30040800" gets converted to "30/04/2008" whether it's in Hex, Decimal, Ascii or Swahili.

                                  "30" & "04" I can see (decimal). It's the "0800" that has me hung up. If Hex it's equivalent to year 2048 in decimal.

                                  Can you just post the code snippet that does that? Thanks.
                                  The answer, of course, has to be is the date is represented in three bytes not four. "30 04 08" and 2000 is added to the third. The fourth byte "00" is maybe just noise and/or unrelated to the date data.

                                  There is a very serious drawback to this method. In less than 91 (92?)years it will be obsolete. Perhaps that's intentional. Sort of 1950's Detroit style built in automobile obsolescence. And we all know how that worked out.

                                  ==================================================
                                  "I'll moider da bum."
                                  Heavyweight boxer Tony Galento,
                                  when asked what he thought of William Shakespeare
                                  ==================================================
                                  It's a pretty day. I hope you enjoy it.

                                  Gösta

                                  JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                                  LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                                  Comment


                                  • #18
                                    The final and correct string for the date was using
                                    sConvert2HEX = sConvert2HEX & Right$("00" & Hex$(Asc(sTmp)), 2)
                                    2525F280504C4E3032323035384E303430040800260220001CE6A5
                                    I guess the key words here are "32-bit integer little endian" as expressed above. 30040800 hex/little endian should be read as 00080430 which looks like ISO format except that year 2000 is considered year 0. (Maybe, Gösta, that this protocol is meant to last a few generations? )

                                    Likewise, 26022000, as it should be read 00200226 where you either are to ignore the first two numbers (again) or they do allow for up to 9999 hours and then some minutes in their timing routines.

                                    This is a format called 'packed BCD' (BCD = binary coded decimal) which is quite common in the micro controller world and (Intel) assembler. Also worth noting, it is normal to vary the interpretation of each number in such binary sequences, as can be seen here (example picked as the string has a GPS origin):

                                    http://www.elgps.com/public_ftp/Docu...F_Protocol.pdf



                                    ViH

                                    Comment


                                    • #19
                                      Vidar, maybe the timing uses the API:
                                      Code:
                                      GetTickCount 'WinApi that keeps track of elapsed time 'reset every 49 days
                                      Which allows timing intervals covering days/weeks (as long as the 49 day boundry isn't crossed in the meantime).

                                      ... 30040800 hex/little endian should be read as 00080430 ...
                                      Can't be hex as &h30 = 48 decimal. (no month I know of has 48 days).

                                      ==================================
                                      "Even when all the experts agree,
                                      they may well be mistaken."

                                      Bertrand Russell
                                      ==================================
                                      Last edited by Gösta H. Lovgren-2; 16 Dec 2008, 08:59 PM.
                                      It's a pretty day. I hope you enjoy it.

                                      Gösta

                                      JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                                      LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                                      Comment


                                      • #20
                                        Can't be hex as &h30 = 48 dec...
                                        My bad. It is packed BCD, so it should have said binary.

                                        WinAPI? In a GPS? That would be embedded Windows, then, I presume. I haven't looked at what calls are available for embedded Windows ...

                                        ViH

                                        Comment

                                        Working...
                                        X