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
Comment