Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Hex Dump using absolute arrays and indexed pointers demo

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

  • Hex Dump using absolute arrays and indexed pointers demo

    Code:
    ' HEXDIGITS.BAS
    ' Demo of converting a long string of characters to hex digits using both
    ' an absolute array and offset pointers.
    ' 3/29/03
    ' Author: Michael Mattias Racine WI
    ' Public Domain
    ' Compiler: PB/Win 7.00
    
    #COMPILE EXE
    #DEBUG ERROR ON
    #REGISTER NONE
    #TOOLS OFF
    #DIM ALL
    
    FUNCTION PBMAIN() AS LONG
      CALL TestHexDigits
    END FUNCTION
    
    FUNCTION TestHexDigits() AS LONG
    
      LOCAL I AS LONG, FileData AS STRING, HexData AS STRING
      LOCAL pSrc AS BYTE PTR, pDest AS STRING PTR * 2, OutputBUff AS STRING
    
    
      ' build a "test file"
      FOR I = 255 TO 0 STEP -1
          FileData = FileData & CHR$(I)
      NEXT
    
      ' build a Table of ALL hex characters...(not just x'00' - x'0F')
      FOR I = 0 TO &hFF
          HexData = HexData & HEX$(I,2)
      NEXT
    
      '=================================================================
      ' METHOD ONE: Use Absolute Array
      '=================================================================
      ' define an array of 2 character strings overlaying our hex
      ' characters table:
      REDIM HexChar(&hFF) AS STRING * 2 AT STRPTR(HexData)
     ' Build an output buffer: 2 hex chars required per input character:
      OutputBuff             =  SPACE$(LEN(FileData)*2)
      pSrc                   =  STRPTR (FileData)     ' point to start of input
      pDest                  =  STRPTR (OutputBuff)   ' and output
    
      FOR I = 1 TO LEN(FileData)          ' for each byte of input
         @pDest   =  HexChar(@pSrc)       ' move 2 hex characters to output
         INCR  pSrc                       ' and advance in input & output
         INCR  pDest
      NEXT
      ' show the output, should be 512 characters x'FF'- x'00'
      MSGBOX OutputBuff,, "Using ARRAY"
    
      '=================================================================
      '    METHOD TWO: Use Offset Pointers
      '=================================================================
    
      LOCAL      pHex AS STRING PTR * 2
    
      OutputBuff = SPACE$(LEN(FileData)* 2)    ' reset the output
      pSrc        =  STRPTR (FileData)         ' point to start of input
      pDest       =  STRPTR (OutputBuff)       ' and output
      pHex        =  STRPTR (HexData)          ' set base of lookup table
      FOR I = 1 TO LEN(FileData)               ' for each byte of input
         @pDest   =  @pHex[@pSrc]              ' move 2 hex characters to output
         INCR  pSrc                            ' and advance in input & output
         INCR  pDest
      NEXT
     ' show the output, should be 512 characters x'FF' - x'00'
      MSGBOX OutputBuff, ,"Using Offset pointers"
    
    
    END FUNCTION
    ------------------
    Michael Mattias
    Tal Systems Inc.
    Racine WI USA
    mailto:[email protected][email protected]</A>
    www.talsystems.com
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

  • #2
    And to go the other way.... from hex pairs to a byte stream....

    ie., every two characters of input represents a hex pair; each output character = CHR$(VAL(Hex pair))

    Code:
    #COMPILE EXE
    #DIM ALL
    #COMPILER PBWIN 8.03
    
    FUNCTION PBMAIN () AS LONG
     LOCAL wIn AS STRING, WOut AS STRING
     LOCAL i AS LONG, TestOut AS STRING
     
     wIn =  "01234567890ABCDEF01234567890ABCDEF"  ' <<< "THE JPEG"
     wOut = ""
      
     CALL HexPairsToCharString (wIn, Wout)
      
      ' test return and show results...
      FOR I = 1 TO LEN(Wout)   ' for each character of output
          Testout = Testout & HEX$(ASC(wOut,I),2)
      NEXT
      MSGBOX  "In ="  & wIn & $CRLF & "Out=" & TEstOut
    
    END FUNCTION
    
    
    SUB HexPairsToCharString( sBuf AS STRING, Work AS STRING)
     LOCAL i AS LONG, pSrc AS BYTE PTR, pDest AS BYTE PTR
     LOCAL nibble AS BYTE, iLen AS LONG
        
        '---------------------------------------------------------------
        ' make destination string, will be one-half the length of input
        '---------------------------------------------------------------
        iLen  = LEN (sBuf)
        Work  = STRING$(ilen\2, 0 )   ' string of CHR$(0)
        
        pSrc  = STRPTR (sBuf)         ' point at first chars of input and output
        pDest = STRPTR (Work)
        
        FOR I = 1 TO iLen             ' for each character of input
            SELECT CASE AS LONG @pSrc
                CASE &h30? TO &h39?        ' digits zero to nine
                    nibble           =  @pSrc - &h30?     ' convert from display to decimal values 0-9
                CASE &h41? TO &h46?            ' digits "A" thur "F"
                    nibble           =  @psrc - &h37?     ' convert from display to decimal values 10-15
           END SELECT
           IF I MOD 2 THEN             ' working on high-order nibble
                @pDest   = nibble      ' store
                SHIFT LEFT @pdest, 4   ' move to high order position
           ELSE
               @pdest  = @pDest OR nibble  ' add nibble value to low-order position, preserving high order
               INCR      pDest             ' done with this output char, advance to next
           END IF
           INCR pSrc  ' advance to next input char
        NEXT I
        
    END SUB



    [This message has been edited by Michael Mattias (edited August 19, 2007).]
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment

    Working...
    X