J
johnno
Is anyone able to help me here? I have the following VB code and wish to
have it rewritten in C++ but unsure how. Any help would be greatly
appreciated. Effectively the code is packing a 10 letter word into 60 bits.
I only need to deal with 64 valid values. ie the values 90 - 64gv so the
first 26 values are not needed. Thanks in advance
Public Function Pack(ByVal TheData As String) As String
Dim c As Double
Dim x As Long
Dim val As Long
Dim bOut(0 To 7) As Byte
Dim lByte As Long
Dim lBit As Long
Dim AscChr As String
TheData = UCase$(TheData) & Space$(10) ' pad to ensure
at least 10 valid chars
For x = 1 To Len(TheData)
val = Asc(Mid$(TheData, x, 1)) - 26 ' Remove lower
26 ascii characters to keep in our range. Highest ascii used is 90(Z) - 26 =
64
If val Then PackBytes bOut, val - 1, lByte, lBit ' needs some
handling for asc values outside valid range A-Z 0-9 & [space]
Next
For x = LBound(bOut) To UBound(bOut)
AscChr = AscChr & Chr(bOut(x))
Next x
CopyMemory c, bOut(0), 8
Pack = AscChr
End Function
Private Sub PackBytes(ByRef TheBits() As Byte, ByVal TheChar As Long, ByRef
CurrentByte As Long, ByRef CurrentBit As Long)
If CurrentByte > 7 Or (CurrentByte = 7 And CurrentBit > 2) Then Exit Sub
' code too long
If CurrentBit = 0 Then
TheBits(CurrentByte) = TheChar * 4 '
set high 6 bits
CurrentBit = 6
Exit Sub
ElseIf CurrentBit = 2 Then
TheBits(CurrentByte) = TheBits(CurrentByte) Or TheChar '
set low 6 bits
CurrentByte = CurrentByte + 1
ElseIf CurrentBit = 4 Then
TheBits(CurrentByte) = TheBits(CurrentByte) Or (TheChar \ 4) '
set low 2 bits
CurrentByte = CurrentByte + 1
TheBits(CurrentByte) = (TheChar And 3) * 64 '
set high 4 bits
ElseIf CurrentBit = 6 Then
TheBits(CurrentByte) = TheBits(CurrentByte) Or (TheChar \ 16) '
set low 4 bits
CurrentByte = CurrentByte + 1
TheBits(CurrentByte) = (TheChar And 15) * 16 '
set high 2 bits
End If
CurrentBit = CurrentBit - 2
End Sub
have it rewritten in C++ but unsure how. Any help would be greatly
appreciated. Effectively the code is packing a 10 letter word into 60 bits.
I only need to deal with 64 valid values. ie the values 90 - 64gv so the
first 26 values are not needed. Thanks in advance
Public Function Pack(ByVal TheData As String) As String
Dim c As Double
Dim x As Long
Dim val As Long
Dim bOut(0 To 7) As Byte
Dim lByte As Long
Dim lBit As Long
Dim AscChr As String
TheData = UCase$(TheData) & Space$(10) ' pad to ensure
at least 10 valid chars
For x = 1 To Len(TheData)
val = Asc(Mid$(TheData, x, 1)) - 26 ' Remove lower
26 ascii characters to keep in our range. Highest ascii used is 90(Z) - 26 =
64
If val Then PackBytes bOut, val - 1, lByte, lBit ' needs some
handling for asc values outside valid range A-Z 0-9 & [space]
Next
For x = LBound(bOut) To UBound(bOut)
AscChr = AscChr & Chr(bOut(x))
Next x
CopyMemory c, bOut(0), 8
Pack = AscChr
End Function
Private Sub PackBytes(ByRef TheBits() As Byte, ByVal TheChar As Long, ByRef
CurrentByte As Long, ByRef CurrentBit As Long)
If CurrentByte > 7 Or (CurrentByte = 7 And CurrentBit > 2) Then Exit Sub
' code too long
If CurrentBit = 0 Then
TheBits(CurrentByte) = TheChar * 4 '
set high 6 bits
CurrentBit = 6
Exit Sub
ElseIf CurrentBit = 2 Then
TheBits(CurrentByte) = TheBits(CurrentByte) Or TheChar '
set low 6 bits
CurrentByte = CurrentByte + 1
ElseIf CurrentBit = 4 Then
TheBits(CurrentByte) = TheBits(CurrentByte) Or (TheChar \ 4) '
set low 2 bits
CurrentByte = CurrentByte + 1
TheBits(CurrentByte) = (TheChar And 3) * 64 '
set high 4 bits
ElseIf CurrentBit = 6 Then
TheBits(CurrentByte) = TheBits(CurrentByte) Or (TheChar \ 16) '
set low 4 bits
CurrentByte = CurrentByte + 1
TheBits(CurrentByte) = (TheChar And 15) * 16 '
set high 2 bits
End If
CurrentBit = CurrentBit - 2
End Sub