VB6Parse / Library / String / leftb_dollar

VB6 Library Reference

LeftB$ Function

Returns a String containing a specified number of bytes from the left side of a string.

Syntax

LeftB$(string, length)

Parameters

Return Value

Returns a String containing the leftmost length bytes from string. If length is 0, returns an empty string. If length is greater than or equal to the byte length of string, returns the entire string.

Remarks

The LeftB$ function is used with byte data contained in a string. Instead of specifying the number of characters to return, length specifies the number of bytes.

This function is particularly useful when working with: - Binary data stored in strings - ANSI strings where you need byte-level control - Double-byte character set (DBCS) strings - Legacy file formats that use byte-oriented data - Network protocols that specify byte lengths

LeftB$ is the byte-oriented version of Left$. While Left$ counts characters, LeftB$ counts bytes. In single-byte character sets (like standard ASCII), these are equivalent, but in DBCS systems (like Japanese, Chinese, or Korean Windows), characters may occupy multiple bytes.

The LeftB$ function always returns a String. The LeftB function returns a Variant.

Typical Uses

Example 1: Extracting Binary Header

Dim data As String
data = binaryData
header = LeftB$(data, 4)  ' Get first 4 bytes

Example 2: Reading Fixed-Byte Records

Dim record As String
record = GetRecord()
idBytes = LeftB$(record, 8)  ' First 8 bytes = ID

Example 3: Processing DBCS Strings

Dim jpText As String
jpText = "日本語"  ' Japanese text
bytes = LeftB$(jpText, 4)  ' Get first 4 bytes (may be 2 DBCS chars)

Example 4: Protocol Header Extraction

Dim packet As String
packet = ReceivePacket()
magic = LeftB$(packet, 2)  ' 2-byte magic number

Common Usage Patterns

Extracting File Signature

Dim fileData As String
Open fileName For Binary As #1
fileData = Input$(LOF(1), #1)
Close #1
signature = LeftB$(fileData, 4)
If signature = "MZ" & Chr$(0) & Chr$(0) Then
Debug.Print "Executable file"
End If

Reading Binary Structure

Dim buffer As String
buffer = GetBinaryData()
version = LeftB$(buffer, 2)  ' 2-byte version field

Processing Network Data

Dim netData As String
netData = Socket.Receive()
header = LeftB$(netData, 16)  ' 16-byte protocol header

Validating Byte Prefix

If LeftB$(data, 3) = Chr$(0xFF) & Chr$(0xFE) & Chr$(0xFD) Then
Debug.Print "Valid magic bytes"
End If

Extracting BMP Header

Dim bmpData As String
Open "image.bmp" For Binary As #1
bmpData = Input$(54, #1)  ' BMP header is 54 bytes
Close #1
fileType = LeftB$(bmpData, 2)  ' "BM" for BMP files

Reading Length-Prefixed Data

Dim message As String
message = buffer
lenBytes = LeftB$(message, 4)
msgLen = CLng(AscB(MidB$(lenBytes, 1, 1))) + _
CLng(AscB(MidB$(lenBytes, 2, 1))) * 256

Processing DBCS Carefully

Dim text As String
text = dbcsString
' Be careful not to split DBCS characters
If LenB(text) > 10 Then
truncated = LeftB$(text, 10)
End If

Comparing Byte Sequences

Dim data1 As String, data2 As String
If LeftB$(data1, 8) = LeftB$(data2, 8) Then
Debug.Print "Headers match"
End If

Extracting GUID Bytes

Dim guidStr As String
guidStr = GetGUIDBytes()
data1 = LeftB$(guidStr, 4)   ' First DWORD
data2 = MidB$(guidStr, 5, 2) ' First WORD

Processing Binary Chunks

Dim chunk As String
Dim offset As Long
offset = 1
Do While offset <= LenB(binaryData)
chunk = MidB$(binaryData, offset, 512)
If LenB(chunk) = 0 Then Exit Do
processChunk LeftB$(chunk, 512)
offset = offset + 512
Loop

Best Practices

  1. Use LeftB$ when working with binary data or byte-oriented protocols
  2. Be careful with DBCS strings - splitting may corrupt characters
  3. Always validate byte length before extraction to avoid errors
  4. Use LenB to get byte length, not Len
  5. Prefer LeftB$ over Left$ for binary file operations
  6. Remember that byte positions are 1-based, not 0-based
  7. Use with InputB$ when reading binary files
  8. Test DBCS string operations on appropriate language systems
  9. Combine with AscB and ChrB$ for byte-level manipulation
  10. Document whether your code expects SBCS or DBCS strings

Performance Considerations

Character Set Behavior

Environment Byte per Char Notes
English Windows 1 byte LeftB$ and Left$ behave identically
DBCS Windows 1-2 bytes LeftB$ may split multi-byte characters
Unicode VB6 2 bytes Internal strings are Unicode but converted
Binary Data N/A LeftB$ treats data as raw bytes

Common Pitfalls

Limitations

← Back to String | View all functions