VB6Parse / Library / String / rightb_dollar

VB6 Library Reference

RightB$ Function

The RightB$ function in Visual Basic 6 returns a string containing a specified number of bytes from the right side (end) of a string. Unlike Right$ which works with characters, RightB$ operates at the byte level, making it essential for binary data processing and working with DBCS (Double-Byte Character Set) strings.

Syntax

RightB$(string, length)

Parameters

Return Value

Returns a String containing the rightmost length bytes of string.

Behavior and Characteristics

Byte vs. Character Operation

Length Handling

Character Set Considerations

Common Usage Patterns

1. Extract Binary Data Suffix

Function GetBinarySuffix(data As String, numBytes As Integer) As String
GetBinarySuffix = RightB$(data, numBytes)
End Function

Dim suffix As String
suffix = GetBinarySuffix(binaryData, 4)  ' Get last 4 bytes

2. Read File Trailer

Function ReadFileTrailer(fileData As String) As String
' Get last 8 bytes of file (e.g., signature or checksum)
ReadFileTrailer = RightB$(fileData, 8)
End Function
Function GetProtocolFooter(packet As String) As String
' Get 2-byte footer from network packet
GetProtocolFooter = RightB$(packet, 2)
End Function

4. Process Binary Structures

Type FileHeader
signature As String * 4
version As String * 2
End Type

Function GetVersion(headerData As String) As String
' Extract last 2 bytes as version info
GetVersion = RightB$(headerData, 2)
End Function

5. Network Packet Checksum

Function ExtractChecksum(packet As String) As String
' Network packets often have 2-4 byte checksums at end
ExtractChecksum = RightB$(packet, 4)
End Function

6. GUID/UUID Component Extraction

Function GetGuidSuffix(guidData As String) As String
' Extract last 6 bytes of GUID (node portion)
GetGuidSuffix = RightB$(guidData, 6)
End Function

7. File Format Magic Bytes (Trailer)

Function CheckFileTrailer(fileData As String, expectedTrailer As String) As Boolean
Dim trailer As String
trailer = RightB$(fileData, LenB(expectedTrailer))
CheckFileTrailer = (trailer = expectedTrailer)
End Function

8. Extract Record Suffix

Function GetRecordSuffix(record As String) As String
' Fixed-length binary record with 4-byte suffix
GetRecordSuffix = RightB$(record, 4)
End Function
Function GetImageFooter(imageData As String) As String
' Some image formats have trailers (e.g., JPEG end marker)
GetImageFooter = RightB$(imageData, 2)
End Function

10. Database Record Alignment

Function AlignRecordEnd(record As String, alignment As Integer) As String
' Get aligned portion from end of record
Dim alignedSize As Integer
alignedSize = (LenB(record) \ alignment) * alignment
If alignedSize > 0 Then
AlignRecordEnd = RightB$(record, alignedSize)
Else
AlignRecordEnd = record
End If
End Function

Best Practices

When to Use RightB$ vs Right$

' Use Right$ for text/character operations
Dim fileExt As String
fileExt = Right$(fileName, 3)  ' Get last 3 characters

' Use RightB$ for binary data operations
Dim checksum As String
checksum = RightB$(binaryData, 4)  ' Get last 4 bytes

Validate Byte Count

Function SafeRightB(data As String, numBytes As Integer) As String
If numBytes < 0 Then
SafeRightB = ""
ElseIf numBytes >= LenB(data) Then
SafeRightB = data
Else
SafeRightB = RightB$(data, numBytes)
End If
End Function

Use with LenB for Byte Counting

' Always use LenB when working with RightB$
Dim totalBytes As Long
Dim footer As String
totalBytes = LenB(binaryData)
footer = RightB$(binaryData, 8)

Combine with AscB for Byte Values

Function GetLastByte(data As String) As Byte
Dim lastByte As String
lastByte = RightB$(data, 1)
GetLastByte = AscB(lastByte)
End Function

Binary Structure Parsing

' Extract multiple fields from end of structure
Dim checksum As String
Dim version As String
checksum = RightB$(structData, 4)
version = RightB$(LeftB$(structData, LenB(structData) - 4), 2)

Performance Considerations

' Less efficient: multiple RightB$ calls
For i = 1 To 1000
result = result & RightB$(data, 4)
Next i

' More efficient: single operation or byte array
Dim parts() As String
ReDim parts(1 To 1000)
For i = 1 To 1000
parts(i) = RightB$(data, 4)
Next i
result = Join(parts, "")

Character Encoding and VB6

Unicode String Storage

VB6 stores strings internally as Unicode (UCS-2):

Dim text As String
text = "AB"
Debug.Print Len(text)   ' Prints 2 (characters)
Debug.Print LenB(text)  ' Prints 4 (bytes: 2 bytes per character)

Dim lastChar As String
lastChar = Right$(text, 1)   ' Returns "B" (1 character)
Dim lastTwoBytes As String
lastTwoBytes = RightB$(text, 2)  ' Returns "B" (last 2 bytes = 1 character)

DBCS Considerations

When working with Double-Byte Character Sets:

' Be careful with DBCS text - extracting odd number of bytes
' can split multi-byte characters
Dim japaneseText As String
japaneseText = "ใ“ใ‚“ใซใกใฏ"

' Right$ extracts by characters (safe)
Dim lastChar As String
lastChar = Right$(japaneseText, 1)  ' Gets last character properly

' RightB$ extracts by bytes (may split characters)
Dim lastByte As String
lastByte = RightB$(japaneseText, 1)  ' May get incomplete character!

Common Pitfalls

1. Confusing Bytes with Characters

Dim text As String
text = "Hello"

' Wrong: thinking RightB$ works like Right$
result = RightB$(text, 3)  ' Gets last 3 BYTES (not characters)
' With Unicode, 3 bytes = 1.5 characters (1 complete + half of another)

' Correct: use Right$ for character operations
result = Right$(text, 3)  ' Gets "llo"

2. Odd Byte Counts with Unicode

' Problematic: odd number of bytes splits Unicode characters
Dim text As String
text = "Test"
Dim partial As String
partial = RightB$(text, 3)  ' 3 bytes = 1 character + half a character
' Result may be unexpected or corrupted

' Better: use even byte counts for Unicode
Dim proper As String
proper = RightB$(text, 4)  ' 4 bytes = 2 complete characters

3. Not Using LenB for Length

' Wrong: using Len instead of LenB
If RightB$(data, Len(data) - 4) Then  ' Incorrect!

' Correct: use LenB for byte operations
If RightB$(data, LenB(data) - 4) Then

4. Negative Length Values

' Runtime error: Invalid procedure call or argument
result = RightB$("Hello", -1)  ' ERROR!

' Validate first
If numBytes >= 0 Then
result = RightB$(data, numBytes)
End If

5. Assuming ASCII/ANSI Encoding

' Wrong: assuming 1 byte = 1 character
Dim data As String
data = "ABCD"
Dim lastByte As String
lastByte = RightB$(data, 1)  ' Gets 1 byte, not last character
' In Unicode, this is half of the last character

' Correct: use Right$ or account for 2 bytes per character
Dim lastChar As String
lastChar = Right$(data, 1)   ' Gets "D"
' OR
lastChar = RightB$(data, 2)  ' Gets last 2 bytes = "D"

Limitations

โ† Back to String | View all functions