Announcement

Collapse
No announcement yet.

C2PB

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

  • C2PB

    I've found a fair amount of information about CRC for DTC/DTE communications.
    Most of it is written in C or C+. The below psuedo code creates a look up
    table. The help file makes it easy to find corresponding PB data types, but I'm
    not familiar enough with C syntax to get anything more from this code than it looks
    like two "FOR/NEXT" loops (and I'm not real sure about that). If someone could
    translate this to PowerBASIC syntax I would very much appreciate it.

    unsigned short a = 0
    unsigned short b[256]={0}
    unsigned short c[8] = {0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008}

    {
    int d, e, f
    for (d=0; d<256; d++)
    {
    for (b[d]=0, f = 0x0000, e=0; e<8; e++)
    {
    if(d & f)b[d]^=c[e];
    f<<=1;
    }
    }
    a=0;
    Return 0;
    }



    ------------------
    Mark Pruitt
    [email protected]



    [This message has been edited by mark pruitt (edited June 11, 2003).]
    sigpicMark Pruitt
    [email protected]

    http://ezreregister.com
    Manage 3rd party BlackBerry software registration codes.

    It's not the only way to do it...just the EZ way!

  • #2
    I think this is a pretty close translation, the variables a, b and c are probably global, d, e and f are local. I put the code in a PbMain() function for testing. Note also, I started the arrays at zero as this is what C does. You'll have to test whether the results are correct, though!

    Code:
    Global a        As Integer
    Global b()      As Integer
    Global c()      As Integer
    
    
    Function PBMain
    
     Dim d As Long, e As Long, f As Long
    
     ReDim b(0:255)
     ReDim c(0:7)
     
     c(0) = 1
     c(1) = 2
     c(2) = 3
     c(3) = 4
     c(4) = 5
     c(5) = 6
     c(6) = 7
     c(7) = 8
    
     For d = 0 To 255
    
         For e = 0 To 7
            If (d And f) Then b(d) = b(d) Or c(e)
            Shift Left f, 1
         Next e
    
     Next d
     a = 0
     Function = 0
    
    End Function
    ------------------
    Kev Peel
    KGP Software (Custom Programming Solutions), UK.
    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

    Comment


    • #3

      Thanks Kev! As soon as I saw you had posted, I thought of your header
      conversion tool. Do you know if it would give the same results?

      ------------------
      Mark Pruitt
      [email protected]
      sigpicMark Pruitt
      [email protected]

      http://ezreregister.com
      Manage 3rd party BlackBerry software registration codes.

      It's not the only way to do it...just the EZ way!

      Comment


      • #4
        Hi Mark, no unfortunately the hdr2inc program only converts equates, function declarations and UDT's to PowerBASIC. I've always wanted to write a CPP2PB translator but that would mean writing a "psuedo-compiler" to be accurate enough

        ------------------
        Kev Peel
        KGP Software (Custom Programming Solutions), UK.
        kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

        Comment


        • #5
          Without seeing the whole code, variables a, b and c might not be
          global. They may only have file scope in which case you've got
          a possible prob somewhere down the line.

          If the variables were global, the "extern" or "extern C"
          definition would be present.

          C/C++ arrays are ALWAYS indexed at zero(0).

          Hope this helps,
          Cecil

          ------------------

          Comment


          • #6
            I think 'unsigned short' should strictly be a WORD in PowerBASIC, though I'm not sure if that
            makes any difference to these calcs. Mind you, if they really are globals, it could be significant.

            Also, I think the first line inside the loop needs to be an XOR, not an OR, ie:
            Code:
            If (d And f) Then b(d) = b(d) xor c(e)
            --Paul



            ------------------
            Zippety Software, Home of the Lynx Project Explorer
            http://www.zippety.net
            mailto[email protected][email protected]</A>
            Zippety Software, Home of the Lynx Project Explorer
            http://www.zippety.net
            My e-mail

            Comment


            • #7
              I finally had a chance to try this out. Special thanks to Kev Peel for the initial
              translation, and Paul, for pointing out "XOR" instead of "OR". I changed variable
              names, commented, etc. to make the code more meaningful.

              Code:
                  'CRC-16 look up table
                  '
                  'initial translation from C by Kev Peel
                  '
                  'tweaked by Mark Pruitt
                  '
                  'feel free to use, modify, etc
                  '
                  '**********************************************************************************************************************
              
              
                  #COMPILE EXE
              
                  FUNCTION PBMAIN ()
              
                  GLOBAL CrcReg%           'global variable to hold current value of CRC Register
                  GLOBAL table% ()         'global look up table
                  LOCAL c% ()              'increments a counter from 0 to 7
                  LOCAL d$                 'added for testing, can be commented out
              
                  LOCAL i??, j??, mask??
              
                  REDIM table%(0 TO 255)
              
                  REDIM c%(0 TO 7)
                          c%(0) = &h1001   'put hex values here to create custom CRC table
                          c%(1) = &h2002
                          c%(2) = &h4004
                          c%(3) = &h8008
                          c%(4) = &h1111
                          c%(5) = &h2222
                          c%(6) = &h4444
                          c%(7) = &h9008
              
                  FOR i?? = 0 TO 255             'first "FOR" loop; increments table%()
              
                          table%(i??) = 0        'initial settings for each element of table%()
              
                          mask?? = &h0001
              
                              FOR j?? = 0 TO 7    'second "FOR" loop; this one creates the data for table%()
              
                              IF(i?? AND mask??) THEN table%(i??) = table%(i??) XOR c%(j??)
                          SHIFT LEFT mask??, 1
                                  NEXT j??
                                      d$ = STR$(table%(i??)) 'this line and next line added for testing only
                                         PRINT d$
                              NEXT i??
              
                          CrcReg% = 0           'set global variable to initial value
                          FUNCTION = 0          'function is done
                          WAITKEY$              'added for testing purposes; can be commented out
                          END FUNCTION
              
              
              ------------------
              Mark Pruitt
              [email protected]


              [This message has been edited by mark pruitt (edited June 12, 2003).]
              sigpicMark Pruitt
              [email protected]

              http://ezreregister.com
              Manage 3rd party BlackBerry software registration codes.

              It's not the only way to do it...just the EZ way!

              Comment

              Working...
              X