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, HexData$, Buffer$, TextData$, BadChar$
Global qFreq, qStart, qMid, qStop As Quad, ShowBuffer As Long
$Template = Space$(7) + ":" + Space$(25) + ":" + Space$(9) + $CrLf '45
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Beene", 30,10,140,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
For i = 0 To &hFF : HexData$ = HexData$ & Hex$(i,2) & $Spc : Next i
BadChar$ = Chr$(1 To 47, 58 To 64, 91 To 96, 123 To 254) '48-57 '65-90 '97-122
Case %WM_Command
Select Case Cb.Ctl
Case 100
'file data in FileData
TextData$ = Repeat$(1200000,"01234567") : ShowBuffer = 0 '~10M text characters
' TextData$ = Repeat$(10,"01234567") : ShowBuffer = 1 '80 text characters
QueryPerformanceCounter qStart
BuildHexString 'results in Global Buffer$
QueryPerformanceCounter qStop
? "Chars: " + Format$(Len(TextData$),"###,###,##0") + $CrLf _
+ Format$((qMid-qStart)/qFreq,"###.000") & " seconds (get Hex string)" + $CrLf _
+ Format$((qStop-qMid)/qFreq,"###.000") & " seconds (format for display)" + $CrLf _
+ Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
If ShowBuffer Then ? Buffer$
End Select
End Select
End Function
Sub BuildHexString
Local i,iHex,iCount,Rows As Long, a$, b$, c$, d$, temp$
'hex version of TextData$ is in HexData$
Local pSrc As Byte Ptr, pDest As String Ptr * 3
ReDim HexChar(&hFF) As String * 3 At StrPtr(HexData$)
HexData$ = Space$(Len(TextData$)*3)
pSrc = StrPtr (TextData$) ' point to start of input
pDest = StrPtr (HexData$) ' and output
For i = 1 To Len(TextData$) ' for each byte of input
@pDest = HexChar(@pSrc) ' move 2 hex characters to output
Incr pSrc ' and advance in input & output
Incr pDest
Next
QueryPerformanceCounter qMid
'replace unwanted characters with "."
Replace Any BadChar$ With Repeat$(Len(BadChar$),".") In TextData$
Rows = Len(TextData$)/8
Buffer$ = Repeat$(Rows, $Template)
'combine TextData$ and HexData$ as output in Buffer$
Local pBuffer As String Ptr * 6
Local pText As String Ptr * 8
Local pHex As String Ptr * 24
pText = StrPtr(TextData$)
pHex = StrPtr(Hexdata$)
pBuffer = StrPtr(Buffer$)
For i = 1 To Rows
Poke$ pBuffer, Hex$((i-1)*8,6)
pBuffer = pBuffer + 9
Poke$ pBuffer, @pHex
pBuffer = pBuffer + 26
Poke$ pBuffer, @pText
Incr pHex
Incr pText
pBuffer = pBuffer + 10
Next i
End Sub
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode=1
#Include "win32api.inc"
Global hDlg As Dword, Buffer$, TextData$, BadChar$
Global qFreq, qStart, qMid, qStop As Quad, ShowBuffer As Long
Global HexTable() As String * 2
$Template = Space$(7) + ":" + Space$(25) + ":" + Space$(9) + $CrLf '45
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Beene", 30,10,140,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 HexTable(255)
For i = 0 To 255 : HexTable(i) = Hex$(i,2) : Next i
BadChar$ = Chr$(1 To 47, 58 To 64, 91 To 96, 123 To 254) '48-57 '65-90 '97-122
Case %WM_Command
Select Case Cb.Ctl
Case 100
'file data in FileData
TextData$ = Repeat$(1200000,"01234567") : ShowBuffer = 0 '~10M text characters
TextData$ = Repeat$(10,"01234567") : ShowBuffer = 1 '80 text characters
QueryPerformanceCounter qStart
BuildHexString 'results in Global Buffer$
QueryPerformanceCounter qStop
? "Chars: " + Format$(Len(TextData$),"###,###,##0") + $CrLf _
+ Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
If ShowBuffer Then ? Buffer$
End Select
End Select
End Function
Sub BuildHexString
Local i,j,Rows As Long
'combine TextData$ and HexData$ as output in Buffer$
Local pBuffer As String Ptr * 6
Local pText As String Ptr * 8
Rows = Len(TextData$)/8
Buffer$ = Repeat$(Rows, $Template)
pBuffer = StrPtr(Buffer$)
pText = StrPtr(TextData$)
For i = 1 To Rows
Poke$ pBuffer, Hex$((i-1)*8,6)
pBuffer = pBuffer + 35
Poke$ pBuffer, @pText
Incr pText
pBuffer = pBuffer + 10
Next i
Replace Any BadChar$ With Repeat$(Len(BadChar$),".") In TextData$
Local pTextChar As Byte Ptr
pTextChar = StrPtr(TextData$)
pBuffer = StrPtr(Buffer$) + 9
For i = 1 To Rows
For j = 1 To 8
Poke$ pBuffer, HexTable(@pTextChar)
pTextChar = pTextChar + 1
pBuffer = pBuffer + 3
Next j
pBuffer = pBuffer + 21
Next i
End Sub
'gbs_01337
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm