Bit Shifting Question

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
 
M

mlimber

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. [snip]

Depends. How much are you offering in compensation?

Seriously, we'll be more than willing to help if you have a specific C+
+ language question (see <http://www.parashift.com/c++-faq-lite/how-to-
post.html#faq-5.9> for what sort of questions are on-topic here). The
operators you're probably interested in are &, |, ~, <<, and >>. Check
the appendix of your favorite C++ book for more information, and if
you don't understand something, feel free to ask here.

Cheers! --M
 
J

Jim Langston

johnno said:
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

std::string Pack( std::string TheData )
Dim c As Double

double c;
Dim x As Long

long x;
Dim val As Long

long val;
etc...

If you don't know C++ how are you going to code this? There is nothing
extradanary about this program. Everything can be done in C++ by just using
C++ syntax instead of VB.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top