Announcement

Collapse
No announcement yet.

Byte Order of Elements of a C Structure

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

  • Byte Order of Elements of a C Structure

    Hello community,



    at the moment I code a COM library to access an SAP system via SAP NetWeaver RFC library. Now I work with C structures, because the RFC library is originally coded in C. If I call a function with an array of bytes and look at the result, I see that the byte order of the elements are different from PowerBASIC. E.g. I use the function RfcGetFieldDescByIndex to get a field description of an SAP table. The result is in a C structure, e.g. the type of the field as a word. If I use the PB function Peek(Word, ...) I get 0x0600 but the correct value is 0x0006 - RFCTYPE_NUM. Now my questions:
    1. Stores C all values in another byte order as PowerBASIC - also pointer?
    2. Now I use this function to get the correct value:
      Code:
      Method PokeWStruc(ByVal memoryAddr As Long, ByVal value As Word)
        Poke Byte, memoryAddr, Hi(Byte, value)
        Poke Byte, memoryAddr + 1, Lo(Byte, value)
      End Method
      Exists any PowerBASIC build in functions?
    Thanks for tips.

    Cheers
    Stefan
    Visit my private homepage and my commercial homepage
    Meet me at XING or Google+

  • #2
    out of the docu:

    > DIM I AS WORD
    > A C/C++ UINT16 and a Delphi word are equivalent to a PowerBASIC Word.

    I think all Intel / Windows compiler use the same internal byteorder for WORD, but maybe your data is comming from a different world.

    Like Windows use $CRLF at end of line and Linux / unix / Mac do use $CR or $LF alone.
    Last edited by Hubert Brandel; 31 Jul 2013, 06:43 PM.
    Regards,
    Hubert

    ------------------------------------
    http://familie-brandel.de/index_e.html

    Comment


    • #3
      HI and LO don't refer to the address, they refer to the magnitude, which in Intel format is reversed from the address.

      What I really don't understand is,

      Code:
      Method PokeWStruc(ByVal memoryAddr As Long, ByVal value As Word)
        Poke Byte, memoryAddr, Hi(Byte, value)
        Poke Byte, memoryAddr + 1, Lo(Byte, value)
      End Method
      Why not just POKE the 16-bit WORD into address in one shot instead of doing it one eight-bit BYTE at a time? Unless you want to do some kind of byte reversal - for some reason I cannot divine - that should work.
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Code:
        #COMPILE EXE
        #DIM ALL
        
        FUNCTION PBMAIN () AS LONG
           LOCAL wrd, wrd2 AS WORD
        
           wrd = &h0601                     'here's (approx.) your original word you want to reverse
        
           wrd2 = PEEK(WORD, VARPTR(wrd))   'you load it like you originally wanted to do
           ? HEX$(wrd2, 4)
        
           !ror wrd2, 8                     ;and now reverse its bytes by rotating 8 bits which takes only a few tix
           'rotate right wrd2, 8            'slower way
        
           ? HEX$(wrd2, 4)
        
        END FUNCTION

        Comment


        • #5
          Hello Hubert,
          thanks for your reply and suggestion. I think you are right.
          Cheers
          Stefan

          Hello Michael,
          thanks for your reply. The data in the structure are different and it is not possible to read the data correct with Peek(Word, ...). I will analyze it further.
          Cheers
          Stefan

          Hello John,
          thanks for your reply. ROR command is cool.
          Cheers
          Stefan
          Visit my private homepage and my commercial homepage
          Meet me at XING or Google+

          Comment


          • #6
            > The data in the structure are different and it is not possible to read the data
            > correct with Peek(Word, ...). I will analyze it further.

            If you need a structure conversation, a UNION could make this elegant.
            There are examples in the online help under

            => UNION, Unions
            => UNION, UNION/END UNION statements

            PEEK, POKE and ASM commands are shurely cool, but if you use the UNIONS way you can understand it in 20 Years further ;-)
            Regards,
            Hubert

            ------------------------------------
            http://familie-brandel.de/index_e.html

            Comment

            Working...
            X