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"
Type LongOfBytes
a As Byte
b As Byte
c As Byte
d As Byte
End Type
Union LongByteUnion
Long As Long
Bytes As LongOfBytes
End Union
Global hDlg As Dword, Buffer$, TextData$, BadChar$, HexTable$, HexDigit$
Global qFreq, qStart, qStop As Quad
Global Rep() As Byte, hexChr() As Word
Global HexTableArray() As String * 2
Global dispChar As String
Global HexChars() As Word
Global ValidChars() As Byte
'$Template = Space$(7) + ":" + Space$(25) + ":" + Space$(9) + $CrLf
'$Template = "000000 " + ":" + Space$(25) + ":" + Space$(9) + $CrLf
$Template = "000000 : xx xx xx xx xx xx xx xx : ........" + $CrLf
$Template16 = "000000 : xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx : ................" & $CrLf
Function PBMain() As Long
Dialog New Pixels, 0, "Hex Dump",300,300,400,300, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 101,"Sample", 10,20,60,20
Control Add Button, hDlg, 100,"Speed Test _ Beene01 (first)", 80,20,280,20
Control Add Button, hDlg, 151,"Sample", 10,50,60,20
Control Add Button, hDlg, 150,"Speed Test _ Larry14 (shift)", 80,50,280,20
Control Add Button, hDlg, 161,"Sample", 10,80,60,20
Control Add Button, hDlg, 160,"Speed Test _ CookLarry25 (16char)", 80,80,280,20
Control Add Button, hDlg, 201,"Sample", 10,110,60,20
Control Add Button, hDlg, 200,"Speed Test _ Beene30 (minimal)", 80,110,280,20
Control Add Button, hDlg, 301,"Sample", 10,140,60,20
Control Add Button, hDlg, 300,"Speed Test _ LarryBeene34 (minimal+shift)", 80,140,280,20
Control Add Button, hDlg, 401,"Sample", 10,170,60,20
Control Add Button, hDlg, 400,"Speed Test _ LarryBeene39 (unrolled)", 80,170,280,20
Control Add Button, hDlg, 501,"Sample", 10,200,60,20
Control Add Button, hDlg, 500,"Speed Test _ LarryJohn42 (Dim At)", 80,200,280,20
Control Add Button, hDlg, 601,"Sample", 10,230,60,20
Control Add Button, hDlg, 600,"Speed Test _ Paul50", 80,230,280,20
Control Add Button, hDlg, 701,"Sample", 10,260,60,20
Control Add Button, hDlg, 700,"Speed Test _ Paul50ASM", 80,260,280,20
QueryPerformanceFrequency qFreq
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local i,r As Long
Select Case Cb.Msg
Case %WM_InitDialog
ReDim hexChr(255)
For i=0 To 255
Local v As String
v = Hex$(i,2)
hexChr(i) = CvWrd(v)
Next
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
For i = 0 To 255
Select Case i
Case 0 To 47, 58 To 64, 91 To 96, 123 To 255
dispChar += "."
Case Else
dispChar += Chr$(i)
End Select
Next
ReDim HexTableArray(255)
For i = 0 To 255 : HexTableArray(i) = Hex$(i,2) : Next i
Dim HexChars(255)
Dim ValidChars(255)
For r = 0 To 255
HexChars(r) = Asc(Hex$(r,1))* 256 + Asc(Hex$(r,2))
Select Case r
Case 48 To 57, 65 To 90, 97 To 122 : ValidChars(r)= r
Case Else : ValidChars(r)=Asc(".")
End Select
Next
Case %WM_Command
Select Case Cb.Ctl
Case 101
Dialog Set Text hDlg, "Hex Dump - Gary01"
TextData$ = Repeat$(20,"01234567") '10M text characters
BuildHexString8_Gary01 'results in Global Buffer$
? "Gary01" + $CrLf + $CrLf + Buffer$
Case 100
Dialog Set Text hDlg, "Hex Dump - Gary01"
TextData$ = Repeat$(1200000,"01234567") '10M text characters
QueryPerformanceCounter qStart
BuildHexString8_Gary01 'results in Global Buffer$
QueryPerformanceCounter qStop
? "Gary01" + $CrLf + $CrLf + "Chars: " + Format$(Len(TextData$),"###,###,##0") + $CrLf _
+ Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
Case 151
Dialog Set Text hDlg, "Hex Dump - Larry14"
TextData$ = Repeat$(20,"01234567") '10M text characters
BuildHexString8_Larry14 'results in Global Buffer$
? "Larry14" + $CrLf + $CrLf + Buffer$
Case 150
Dialog Set Text hDlg, "Hex Dump - Larry14"
TextData$ = Repeat$(1200000,"01234567") '10M text characters
QueryPerformanceCounter qStart
BuildHexString8_Larry14 'results in Global Buffer$
QueryPerformanceCounter qStop
? "Larry14" + $CrLf + $CrLf + "Chars: " + Format$(Len(TextData$),"###,###,##0") + $CrLf _
+ Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
Case 161
Dialog Set Text hDlg, "Hex Dump - CookLarry21"
TextData$ = Repeat$(20,"01234567") '10M text characters
BuildHexString16_CookLarry25 'results in Global Buffer$
? "CookLarry21" + $CrLf + $CrLf + Buffer$
Case 160
Dialog Set Text hDlg, "Hex Dump - CookLarry21"
TextData$ = Repeat$(1200000,"01234567") '10M text characters
QueryPerformanceCounter qStart
BuildHexString16_CookLarry25 'results in Global Buffer$
QueryPerformanceCounter qStop
? "CookLarry21" + $CrLf + $CrLf + "Chars: " + Format$(Len(TextData$),"###,###,##0") + $CrLf _
+ Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
Case 201
Dialog Set Text hDlg, "Hex Dump - Beene30"
TextData$ = Repeat$(20,"01234567") '10M text characters
BuildHexString8_Beene30 'results in Global Buffer$
? "Beene30" + $CrLf + $CrLf + Buffer$
Case 200
Dialog Set Text hDlg, "Hex Dump - Beene30"
TextData$ = Repeat$(1200000,"01234567") '10M text characters
QueryPerformanceCounter qStart
BuildHexString8_Beene30 'results in Global Buffer$
QueryPerformanceCounter qStop
? "Beene30" + $CrLf + $CrLf + "Chars: " + Format$(Len(TextData$),"###,###,##0") + $CrLf _
+ Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
Case 301
Dialog Set Text hDlg, "Hex Dump - LarryBeene34"
TextData$ = Repeat$(20,"01234567") '10M text characters
BuildHexString8_LarryBeene34 'results in Global Buffer$
? "LarryBeene34" + $CrLf + $CrLf + Buffer$
Case 300
Dialog Set Text hDlg, "Hex Dump - LarryBeene34"
TextData$ = Repeat$(1200000,"01234567") '10M text characters
QueryPerformanceCounter qStart
BuildHexString8_LarryBeene34 'results in Global Buffer$
QueryPerformanceCounter qStop
? "LarryBeene34" + $CrLf + $CrLf + "Chars: " + Format$(Len(TextData$),"###,###,##0") + $CrLf _
+ Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
Case 401
Dialog Set Text hDlg, "Hex Dump - LarryBeene39"
TextData$ = Repeat$(20,"01234567") '10M text characters
BuildHexString8_LarryBeene39 'results in Global Buffer$
? "LarryBeene39" + $CrLf + $CrLf + Buffer$
Case 400
Dialog Set Text hDlg, "Hex Dump - LarryBeene39"
TextData$ = Repeat$(1200000,"01234567") '10M text characters
QueryPerformanceCounter qStart
BuildHexString8_LarryBeene39 'results in Global Buffer$
QueryPerformanceCounter qStop
? "LarryBeene39" + $CrLf + $CrLf + "Chars: " + Format$(Len(TextData$),"###,###,##0") + $CrLf _
+ Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
Case 501
Dialog Set Text hDlg, "Hex Dump - LarryJohn42"
TextData$ = Repeat$(20,"01234567") '10M text characters
BuildHexString8_LarryJohn42 'results in Global Buffer$
? "LarryJohn42" + $CrLf + $CrLf + Buffer$
Case 500
Dialog Set Text hDlg, "Hex Dump - LarryJohn42"
TextData$ = Repeat$(1200000,"01234567") '10M text characters
QueryPerformanceCounter qStart
BuildHexString8_LarryJohn42 'results in Global Buffer$
QueryPerformanceCounter qStop
? "LarryJohn42" + $CrLf + $CrLf + "Chars: " + Format$(Len(TextData$),"###,###,##0") + $CrLf _
+ Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
Case 601
Dialog Set Text hDlg, "Hex Dump - BeenePaul35"
TextData$ = Repeat$(20,"01234567") '10M text characters
BuildHexString8_Paul50 'results in Global Buffer$
? "BeenePaul35" + $CrLf + $CrLf + Buffer$
Case 600
Dialog Set Text hDlg, "Hex Dump - Paul50"
TextData$ = Repeat$(1200000,"01234567") '10M text characters
QueryPerformanceCounter qStart
BuildHexString8_Paul50 'results in Global Buffer$
QueryPerformanceCounter qStop
? "Paul50" + $CrLf + $CrLf + "Chars: " + Format$(Len(TextData$),"###,###,##0") + $CrLf _
+ Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
Case 701
Dialog Set Text hDlg, "Hex Dump - Paul50ASM"
TextData$ = Repeat$(20,"01234567") '10M text characters
BuildHexString8_Paul50ASM 'results in Global Buffer$
? "Paul50ASM" + $CrLf + $CrLf + Buffer$
Case 700
Dialog Set Text hDlg, "Hex Dump - Paul50ASM"
TextData$ = Repeat$(1200000,"01234567") '10M text characters
QueryPerformanceCounter qStart
BuildHexString8_Paul50ASM 'results in Global Buffer$
QueryPerformanceCounter qStop
? "Paul50ASM" + $CrLf + $CrLf + "Chars: " + Format$(Len(TextData$),"###,###,##0") + $CrLf _
+ Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
End Select
End Select
End Function
Sub BuildHexString8_Gary01
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
Local pTextChar As Byte Ptr
'pad last line with Chr$(0)
If Len(TextData$) Mod 8 Then
Buffer$ = String$(8 - Len(TextData$) Mod 8, $Nul)
TextData$ = Build$(TextData$, Buffer$)
End If
'Create the output buffer using the template
Rows = Len(TextData$)/8
Buffer$ = Repeat$(Rows, $Template)
'Add the Hex data to the output buffer
pTextChar = StrPtr(TextData$)
pBuffer = StrPtr(Buffer$) + 9
For i = 1 To Rows
For j = 1 To 8
Poke$ pBuffer, HexTableArray(@pTextChar)
pTextChar = pTextChar + 1
pBuffer = pBuffer + 3
Next j
pBuffer = pBuffer + 21
Next i
'remove non-displayable characters
Replace Any BadChar$ With Repeat$(Len(BadChar$),".") In TextData$
'Add the line numbers and text characters to the output buffer
pText = StrPtr(TextData$)
pBuffer = StrPtr(Buffer$)
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
End Sub
Function BuildHexString8_Larry14() As Long
Register i As Long
Register j As Long
Local rows, cnt, templateLen, e As Long
Local pNumDest As Byte Ptr: ' Line offset pointer
Local pHexDest As Byte Ptr
Local pBaseDest As Byte Ptr: ' Output base pointer
Local pCharDest As Byte Ptr: ' Character output pointer
Local pDispChar As Byte Ptr: ' Translation buffer
Local pCharSrc As Byte Ptr: ' Current character
Local c, l, v As Byte
' 16 is often more common for a hex display xx xx xx xx xx xx xx xx | xx xx xx xx xx xx xx xx ........|........
cnt = Len( TextData$ )
rows = (cnt \ 8) + 1
buffer = Repeat$( rows, $template )
templateLen = Len( $template )
'Add the Hex data to the output buffer
pCharSrc = StrPtr(TextData$)
pDispChar = StrPtr( dispChar )
pBaseDest = StrPtr( buffer )
For i = 0 To cnt-1 Step 8
pNumDest = pBaseDest + 0: ' Start of offset
pHexDest = pBaseDest + 9: ' Start of hex #'s
pCharDest = pBaseDest + 35: ' Start of characters
' Hex offset (1/2 remaining time)
c = i
v = c And 15: @pNumDest[5] = v + 48 + IIf&( v>9, 8 , 0 )
Shift Right c, 4
v = c And 15: @pNumDest[4] = v + 48 + IIf&( v>9, 8 , 0 )
Shift Right c, 4
v = c And 15: @pNumDest[3] = v + 48 + IIf&( v>9, 8 , 0 )
Shift Right c, 4
v = c And 15: @pNumDest[2] = v + 48 + IIf&( v>9, 8 , 0 )
Shift Right c, 4
v = c And 15: @pNumDest[1] = v + 48 + IIf&( v>9, 8 , 0 )
Shift Right c, 4
v = c And 15: @pNumDest[0] = v + 48 + IIf&( v>9, 8 , 0 )
e = IIf&( i + 7 >= cnt, cnt-i-1, 7 )
For j = 0 To e
c = @pCharSrc[j]
@pCharDest[j] = @pDispChar[ c ]
l = c And 15
@pHexDest[1] = l + 48 + IIf&( l>9, 8, 0 )
Shift Right c, 4
@pHexDest[0] = c + 48 + IIf&( c>9, 8, 0 )
pHexDest += 3
Next
pCharSrc += 8: ' Next 8 characters
pBaseDest += templateLen
Next
Function = cnt
End Function
Function BuildHexString16_CookLarry25() As Long
Register i As Long
Register j As Long
Local rows, cnt, templateLen, e As Long
Local pNumDest As Byte Ptr: ' Line offset pointer
Local pHexDest As Byte Ptr
Local pBaseDest As Byte Ptr: ' Output base pointer
Local pCharDest As Byte Ptr: ' Character output pointer
Local pDispChar As Byte Ptr: ' Translation buffer
Local pCharSrc As Byte Ptr: ' Current character
Local l, v As Byte
Local c As Long
Local ds As String
cnt = Len( TextData$ )
rows = (cnt \ 16) + 1 + IIf&((cnt Mod 16)=0, -1, 0) 'avoids the extra line if evenly divisible by 16
buffer = Repeat$( rows, $template16 )
templateLen = Len( $template16 )
'Add the Hex data to the output buffer
pCharSrc = StrPtr(TextData$)
pDispChar = StrPtr( dispChar )
pBaseDest = StrPtr( buffer )
For i = 0 To cnt-1 Step 16
pNumDest = pBaseDest + 0: ' Start of offset
pHexDest = pBaseDest + 9: ' Start of hex #'s
pCharDest = pBaseDest + 59: ' Start of characters
' Hex offset (1/2 remaining time)
c = i
v = c And 15: @pNumDest[5] = v + 48 + IIf&( v>9, 7 , 0 )
Shift Right c, 4
v = c And 15: @pNumDest[4] = v + 48 + IIf&( v>9, 7 , 0 )
Shift Right c, 4
v = c And 15: @pNumDest[3] = v + 48 + IIf&( v>9, 7 , 0 )
Shift Right c, 4
v = c And 15: @pNumDest[2] = v + 48 + IIf&( v>9, 7 , 0 )
Shift Right c, 4
v = c And 15: @pNumDest[1] = v + 48 + IIf&( v>9, 7 , 0 )
Shift Right c, 4
v = c And 15: @pNumDest[0] = v + 48 + IIf&( v>9, 7 , 0 )
e = IIf&( i + 15 >= cnt, cnt-i-1, 15 )
For j = 0 To e
c = @pCharSrc[j]
@pCharDest[j] = @pDispChar[ c ]
l = c And 15
@pHexDest[1] = l + 48 + IIf&( l>9, 7, 0 )
Shift Right c, 4
@pHexDest[0] = c + 48 + IIf&( c>9, 7, 0 )
pHexDest += 3
Next
pCharSrc += 16: ' Next 8 characters
pBaseDest += templateLen
Next
Function = cnt
End Function
Sub BuildHexString8_Beene30
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
Sub BuildHexString8_LarryBeene34 'Post #34 Beene code modified to use Shift, no HexDigit
Local i,j,v,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$)
For i = 0 To Rows-1
iPos = i * 8
For j = 0 To 7 Step 2
If j<6 Then
v = (iPos And &H000000FF) * 2
Shift Right iPos, 8
@pBuffer[4-j] = @pHexTable[v]: ' High digit
@pBuffer[5-j] = @pHexTable[v+1]: ' Low digit
End If
v = @pTextData * 2
@pBuffer[9 + j*3] = @pHexTable[v] 'hex data 1st byte
@pBuffer[10 + j*3] = @pHexTable[v+1] 'hex data 2nd byte
@pBuffer[35 + j] = Rep(@pTextData) 'text data
Incr pTextData
v = @pTextData * 2
@pBuffer[12 + j*3] = @pHexTable[v] 'hex data 1st byte
@pBuffer[13 + j*3] = @pHexTable[v+1] 'hex data 2nd byte
@pBuffer[36 + j] = Rep(@pTextData) 'text data
Incr pTextData
Next j
pBuffer += 45
Next i
End Sub
Sub BuildHexString8_LarryBeene39 'Post 39 unrolled
Local i,j,v,p,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 = Space$( rows * 45 )
'add the Hex data to the output buffer
pHexTable = StrPtr(HexTable$) : pTextData = StrPtr(TextData$)
pBuffer = StrPtr(Buffer$)
#Align 4
For i = 0 To Rows-1
iPos = p
j = (iPos And &H000000FF) * 2
Shift Right iPos, 8
@pBuffer[4] = @pHexTable[j]: ' High digit
@pBuffer[5] = @pHexTable[j+1]: ' Low digit
j = (iPos And &H000000FF) * 2
Shift Right iPos, 8
@pBuffer[2] = @pHexTable[j]: ' High digit
@pBuffer[3] = @pHexTable[j+1]: ' Low digit
j = (iPos And &H000000FF) * 2
Shift Right iPos, 8
@pBuffer[0] = @pHexTable[j]: ' High digit
@pBuffer[1] = @pHexTable[j+1]: ' Low digit
j = @pTextData[p+0]
@pBuffer[35] = Rep(j) 'text data
j *= 2
@pBuffer[9 ] = @pHexTable[j] 'hex data 1st byte
@pBuffer[10] = @pHexTable[j+1] 'hex data 2nd byte
j = @pTextData[p+1]
@pBuffer[36] = Rep(j) 'text data
j *= 2
@pBuffer[12] = @pHexTable[j] 'hex data 1st byte
@pBuffer[13] = @pHexTable[j+1] 'hex data 2nd byte
j = @pTextData[p+2]
@pBuffer[37] = Rep(j) 'text data
j *= 2
@pBuffer[15] = @pHexTable[j] 'hex data 1st byte
@pBuffer[16] = @pHexTable[j+1] 'hex data 2nd byte
j = @pTextData[p+3]
@pBuffer[38] = Rep(j) 'text data
j *= 2
@pBuffer[18] = @pHexTable[j] 'hex data 1st byte
@pBuffer[19] = @pHexTable[j+1] 'hex data 2nd byte
j = @pTextData[p+4]
@pBuffer[39] = Rep(j) 'text data
j *= 2
@pBuffer[21] = @pHexTable[j] 'hex data 1st byte
@pBuffer[22] = @pHexTable[j+1] 'hex data 2nd byte
j = @pTextData[p+5]
@pBuffer[40] = Rep(j) 'text data
j *= 2
@pBuffer[24] = @pHexTable[j] 'hex data 1st byte
@pBuffer[25] = @pHexTable[j+1] 'hex data 2nd byte
j = @pTextData[p+6]
@pBuffer[41] = Rep(j) 'text data
j *= 2
@pBuffer[27] = @pHexTable[j] 'hex data 1st byte
@pBuffer[28] = @pHexTable[j+1] 'hex data 2nd byte
j = @pTextData[p+7]
@pBuffer[42] = Rep(j) 'text data
j *= 2
@pBuffer[30] = @pHexTable[j] 'hex data 1st byte
@pBuffer[31] = @pHexTable[j+1] 'hex data 2nd byte
@pBuffer[7] = 58
@pBuffer[33] = 58
@pBuffer[43] = 13
@pBuffer[44] = 10
pBuffer += 45
p += 8
Next i
End Sub
Sub BuildHexString8_LarryJohn42
Local i,j,o,v,p,iPos,Rows,iResult As Long
Local pHexDigit, pHexTable, pTextData, pBuffer As Byte Ptr
Local w1(), w2() As Word
'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 = Space$( rows * 45 )
'add the Hex data to the output buffer
pHexTable = StrPtr(HexTable$) : pTextData = StrPtr(TextData$)
pBuffer = StrPtr(Buffer$)
Dim w1(Rows*23) As Word At pBuffer
Dim w2(Rows*23) As Word At pBuffer+1
#Align 4
For i = 0 To Rows-1 Step 2
iPos = p
j = iPos And &H000000FF
Shift Right iPos, 8
w1(o+2) = hexChr( j )
j = iPos And &H000000FF
Shift Right iPos, 8
w1(o+1) = hexChr( j )
j = iPos And &H000000FF
Shift Right iPos, 8
w1(o+0) = hexChr( j )
j = @pTextData[p+1]
@pBuffer[36] = Rep(j) 'text data
w1(o+6) = hexChr(j)
j = @pTextData[p+3]
@pBuffer[38] = Rep(j) 'text data
w1(o+9) = hexChr(j)
j = @pTextData[p+5]
@pBuffer[40] = Rep(j) 'text data
w1(o+12) = hexChr(j)
j = @pTextData[p+7]
@pBuffer[42] = Rep(j) 'text data
w1(o+15) = hexChr(j)
'---------------------------------------------------
j = @pTextData[p+0]
@pBuffer[35] = Rep(j) 'text data
w2(o+4) = hexChr( j )
j = @pTextData[p+2]
@pBuffer[37] = Rep(j) 'text data
w2(o+7) = hexChr(j)
j = @pTextData[p+4]
@pBuffer[39] = Rep(j) 'text data
w2(o+10) = hexChr(j)
j = @pTextData[p+6]
@pBuffer[41] = Rep(j) 'text data
w2(o+13) = hexChr(j)
@pBuffer[7] = 58
@pBuffer[33] = 58
@pBuffer[43] = 13
@pBuffer[44] = 10
pBuffer += 45
p += 8
' ==========================================================
If i= rows-1 Then Exit For
iPos = p
j = iPos And &H000000FF
Shift Right iPos, 8
w2(o+24) = hexChr( j )
j = iPos And &H000000FF
Shift Right iPos, 8
w2(o+23) = hexChr( j )
j = iPos And &H000000FF
Shift Right iPos, 8
w2(o+22) = hexChr( j )
j = @pTextData[p+1]
@pBuffer[36] = Rep(j) 'text data
w2(o+28) = hexChr(j)
j = @pTextData[p+3]
@pBuffer[38] = Rep(j) 'text data
w2(o+31) = hexChr(j)
j = @pTextData[p+5]
@pBuffer[40] = Rep(j) 'text data
w2(o+34) = hexChr(j)
j = @pTextData[p+7]
@pBuffer[42] = Rep(j) 'text data
w2(o+37) = hexChr(j)
'---------------------------------------------------
j = @pTextData[p+0]
@pBuffer[35] = Rep(j) 'text data
w1(o+27) = hexChr( j )
j = @pTextData[p+2]
@pBuffer[37] = Rep(j) 'text data
w1(o+30) = hexChr(j)
j = @pTextData[p+4]
@pBuffer[39] = Rep(j) 'text data
w1(o+33) = hexChr(j)
j = @pTextData[p+6]
@pBuffer[41] = Rep(j) 'text data
w1(o+36) = hexChr(j)
@pBuffer[7] = 58
@pBuffer[33] = 58
@pBuffer[43] = 13
@pBuffer[44] = 10
pBuffer += 45
p += 8
o += 45
Next i
End Sub
Sub BuildHexString8_Paul50
Local b, pDataOut As Long 'must be first so they can be the REGISTER variables
Local r, rows, pDataIn As Long, Index As LongByteUnion
'create the output buffer and set pointers
Rows = (Len(TextData) -1) \ 8 + 1
Buffer = Space$(rows * 45)
pDataOut = StrPtr(Buffer)
pDataIn = StrPtr(TextData)
'do each row
For r = 0 To Rows *8 -8 Step 8
Index.Long = r
'the 6 hex digit offset into the file
Poke Word,pDataOut, HexChars(Index.Bytes.c)
Poke Word,pDataOut+2,HexChars(Index.Bytes.b)
Poke Word,pDataOut+4,HexChars(Index.Bytes.a)
'separating colon
Poke Byte, pDataOut + 7,&h3A 'asc(":")
'Now the 8 bytes of HEX data
For b = 0 To 7 : Poke Word, pDataOut+9+3*b, HexChars(Peek(Byte, pDataIn+b)) : Next
'separating colon
Poke Byte, pDataOut + 33,&h3A 'asc(":")
'then the ASCII data
For b = 0 To 7 : Poke Byte,pDataOut+35+b,ValidChars(Peek(Byte, pDataIn+b)) : Next
'$CRLF at the end
Poke Word,pDataOut+43,&h0A0D '$asc(CRLF)
'update the pointers
pDataIn += 8
pDataOut += 45
Next
End Sub
Sub BuildHexString8_Paul50ASM
#Register None
Local rows, pDataOut, pDataIn, MainLoopLimit As Long
Local pHexChars, pValidChars As Long
pHexChars = VarPtr(HexChars(0))
pValidChars=VarPtr(ValidChars(0))
'create the output buffer
Rows = (Len(TextData) -1) \ 8 + 1
Buffer = Space$(rows * 45)
pDataOut = StrPtr(Buffer)
pDataIn = StrPtr(TextData)
'do each row
MainLoopLimit = Rows *8 -8
'FOR r = 0 TO Rows *8 -8 STEP 8
!pusha 'save all the registers
!mov ecx,0 'Loop counter starts at 0
!mov esi,pDataOut 'get pointer to output buffer
!mov edi,pDataIn 'get pointer to input buffer
MainLoop:
'the 6 hex digit offset into the file
!push ecx 'save the loop counter while I extract the bytes from it and reuse it later
'POKE WORD,pDataOut+4,HexChars(@pLBU.Bytes.a)
!mov edx,pHexChars 'pointer to the HexChars table
!movzx ebx,cl 'get the low byte of the loop counter
!mov eax,[edx+ebx*2] 'look up the HEX equivalent characters
!mov [esi+4],ax 'store it in the output buffer
'POKE WORD,pDataOut, HexChars(@pLBU.Bytes.c)
'POKE WORD,pDataOut+2,HexChars(@pLBU.Bytes.b)
!ror ecx,8 'get the 2 upper bytes into the lower half of the register so I can get at them
!movzx ebx,cl 'get the middle byte of the loop counter
!mov eax, [edx+ebx*2] 'look up the HEX equivalent characters
!shl eax,16 'move them into place in the top half of register
!movzx ebx,ch 'get the high byte of the loop counter
!mov ax,[edx+ebx*2] 'look up the hex equvalent charcters and put them in the lower half of eax
!mov [esi],eax 'store the 4 HEX characted in the output buffer
'separating colon
'POKE BYTE, pDataOut + 7,&h3A 'asc(":")
!mov byte ptr [esi+7], &h3A 'asc(":") 'store first colon in the correct place in the output buffer
'Now the 8 bytes of HEX data
' FOR b = 0 TO 7
' POKE WORD, pDataOut+9+3*b, HexChars(PEEK(BYTE, pDataIn+b))
!mov ebx,7 '8 character to process
lp2:
!movzx ecx,byte ptr [edi + ebx] 'get next character
!mov edx,pHexChars 'get pointer to HexTable
!lea eax,[ebx*2+ebx] 'calcilate 3*loop counter as each byte takes up 3 bytes when the space is included with the 2 HEX digits
!movzx edx,word ptr [edx + ecx*2] 'get the 2 HEX characters
!mov [esi+eax+9],dx 'store them in the output buffer
' POKE BYTE,pDataOut+35+b,ValidChars(PEEK(BYTE, pDataIn+b))
!mov edx,pValidChars 'get the pointer to the Valid Characters table
!mov eax,[edx+ecx] 'look up the replacement character to use
!mov [esi+ebx+35],al 'store it in the output bffer
' NEXT
!dec ebx 'Next character
!jns short lp2
'separating colon
'POKE BYTE, pDataOut + 33,&h3A 'asc(":")
!mov byte ptr [esi+33], &h3A 'asc(":") 'store second colon in the correct place in the output buffer
'$CRLF at the end
'POKE WORD,pDataOut+43,&h0A0D '$asc(CRLF)
!mov word ptr [esi+43], &h0A0D 'store $CRLF in the correct place in the output buffer
'update the pointers
'pDataIn += 8
!add edi,8
'pDataOut += 45
!add esi,45
'NEXT
!pop ecx 'get loop counter off stack
!add ecx,8 'update count of bytes done
!cmp ecx,MainLoopLimit 'is it finished?
!jle MainLoop 'No, go back and do next line
!popa 'restore registers
End Sub
'gbs_01273
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm