Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode=1
#Include "win32api.inc"
Global hDlg As Dword, Buffer$, TextData$, BadChar$, HexTable$, HexDigit$
Global qFreq, qStart, qStop As Quad
Global Rep() As Byte
'$Template = Space$(7) + ":" + Space$(25) + ":" + Space$(9) + $CrLf
$Template = "000000 " + ":" + Space$(25) + ":" + Space$(9) + $CrLf
Function PBMain() As Long
Dialog New Pixels, 0, "Hex Dump",300,300,200,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Display Sample Results", 10,10,180,20
Control Add Button, hDlg, 200,"Speed Test (10MB Characters)", 10,40,180,20
QueryPerformanceFrequency qFreq
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local i As Long
Select Case Cb.Msg
Case %WM_InitDialog
ReDim Rep(255)
BadChar$ = Chr$(0 To 47, 58 To 64, 91 To 96, 123 To 255) 'GoocChar: 48-57 65-90 97-122
For i = 0 To 255
If InStr(BadChar$,Chr$(i)) Then Rep(i) = Asc(".") Else Rep(i) = i
Next i
For i = 0 To 255 : HexTable$ += Hex$(i,2) : Next i
For i = 0 To 15 : HexDigit$ += Hex$(i,1) : Next i
Case %WM_Command
Select Case Cb.Ctl
Case 100
TextData$ = Repeat$(40,"01234567") '16 text characters
BuildHexString8 'results in Global Buffer$
? Buffer$
Case 200
TextData$ = Repeat$(1200000,"01234567") '10M text characters
QueryPerformanceCounter qStart
BuildHexString8 'results in Global Buffer$
QueryPerformanceCounter qStop
? "Chars: " + Format$(Len(TextData$),"###,###,##0") + $CrLf _
+ Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
End Select
End Select
End Function
Sub BuildHexString8
Local i,j,iPos,Rows,iResult As Long
Local pHexDigit, pHexTable, pTextData, pBuffer As Byte Ptr
'setup
If Len(TextData$) Mod 8 Then TextData$ += String$(8 - Len(TextData$) Mod 8, $Nul) 'pad with Chr$(0)
Rows = Len(TextData$)/8 '# rows needed in Buffer$
Buffer$ = Repeat$(Rows, $Template) 'empty Buffer$ (contains rows of templates)
'add the Hex data to the output buffer
pHexTable = StrPtr(HexTable$) : pTextData = StrPtr(TextData$)
pBuffer = StrPtr(Buffer$) : pHexDigit = StrPtr(HexDigit$)
For i = 0 To Rows-1
iPos = i * 8
For j = 0 To 7
If j < 6 Then
@pBuffer[5-j] = @pHexDigit[iPos Mod 16] 'character position
iPos = iPos \ 16
End If
@pBuffer[9 + j*3] = @pHexTable[@pTextData * 2] 'hex data 1st byte
@pBuffer[10 + j*3] = @pHexTable[@pTextData * 2+1] 'hex data 2nd byte
@pBuffer[35 + j] = Rep(@pTextData) 'text data
Incr pTextData
Next j
pBuffer = pBuffer + 45
Next i
End Sub
'gbs_01272
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm