LeftB$ Function
Returns a String containing a specified number of bytes from the left side of a string.
Syntax
LeftB$(string, length)
Parameters
string: Required. String expression from which the leftmost bytes are returned. IfstringcontainsNull,Nullis returned.length: Required. Numeric expression indicating how many bytes to return. If 0, a zero-length string ("") is returned. If greater than or equal to the number of bytes instring, the entire string is returned.
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
Related Functions
LeftB: Variant version that returns aVariantLeft$: Character-based version that counts charactersRightB$: Returns bytes from the right side of a stringMidB$: Returns bytes from the middle of a stringLenB: Returns the number of bytes in a stringAscB: Returns the byte value of the first byteChrB$: Returns a string containing a single byte
Best Practices
- Use
LeftB$when working with binary data or byte-oriented protocols - Be careful with DBCS strings - splitting may corrupt characters
- Always validate byte length before extraction to avoid errors
- Use
LenBto get byte length, notLen - Prefer
LeftB$overLeft$for binary file operations - Remember that byte positions are 1-based, not 0-based
- Use with
InputB$when reading binary files - Test DBCS string operations on appropriate language systems
- Combine with
AscBandChrB$for byte-level manipulation - Document whether your code expects SBCS or DBCS strings
Performance Considerations
LeftB$is slightly faster thanLeft$for binary data- No performance penalty for requesting more bytes than available
- More efficient than character-by-character byte extraction
- Direct byte access is faster than converting to byte arrays
- Minimal overhead compared to
Left$in SBCS environments
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
- Using
LeftB$on DBCS strings without checking character boundaries - Confusing byte length (
LenB) with character length (Len) - Assuming one byte equals one character in all locales
- Not handling
Nullstring values (causes runtime error) - Passing negative length values (causes runtime error)
- Using
LeftB$for text processing (useLeft$instead) - Forgetting that VB6 strings are internally Unicode
- Splitting surrogate pairs in Unicode environments
- Using with text functions instead of byte functions
Limitations
- Cannot specify starting byte position (use
MidB$instead) - May corrupt DBCS characters if not used carefully
- Returns
Nullif the string argument isNull - Length parameter cannot be
Null - Not suitable for modern Unicode string processing
- Limited to VB6's internal string representation
- May produce unexpected results with emoji or complex Unicode