MidB$ Function
Returns a String containing a specified number of bytes from a string.
Syntax
MidB$(string, start[, length])
Parameters
string: Required. String expression from which bytes are returned. IfstringcontainsNull,Nullis returned.start: Required. Byte position instringat which the part to be taken begins. Ifstartis greater than the number of bytes instring,MidB$returns a zero-length string ("").length: Optional. Number of bytes to return. If omitted or if there are fewer thanlengthbytes in the text (including the byte atstart), all bytes fromstartto the end of the string are returned.
Return Value
Returns a String containing the specified number of bytes from string, starting at the byte position start. If length is omitted, returns all bytes from start to the end. Returns an empty string if start is greater than the byte length of the string.
Remarks
The MidB$ function is used with byte data contained in a string. Instead of specifying character positions and counts, start and length specify byte positions and byte counts.
This function is particularly useful when working with: - Binary data stored in strings - ANSI strings requiring byte-level manipulation - Double-byte character set (DBCS) strings - Fixed-position binary protocols - File format parsing at byte level - Network packet extraction
MidB$ is the byte-oriented version of Mid$. While Mid$ counts characters, MidB$ 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 MidB$ function always returns a String. The MidB function returns a Variant.
Note: Byte positions are 1-based, not 0-based. The first byte is at position 1.
Typical Uses
Example 1: Extracting Binary Field
Dim record As String
record = GetBinaryRecord()
field = MidB$(record, 5, 4) ' Get 4 bytes starting at byte 5
Example 2: Parsing Protocol Header
Dim packet As String
packet = ReceivePacket()
version = MidB$(packet, 1, 2) ' First 2 bytes
msgType = MidB$(packet, 3, 1) ' Third byte
Example 3: Reading Fixed-Position Data
Dim data As String
data = binaryData
id = MidB$(data, 1, 8) ' Bytes 1-8: ID
name = MidB$(data, 9, 32) ' Bytes 9-40: Name
Example 4: Extracting GUID Components
Dim guidBytes As String
guidBytes = GetGUID()
data1 = MidB$(guidBytes, 1, 4) ' First DWORD
data2 = MidB$(guidBytes, 5, 2) ' First WORD
data3 = MidB$(guidBytes, 7, 2) ' Second WORD
Common Usage Patterns
Parsing Binary Structure
Dim buffer As String
buffer = GetBinaryData()
magic = MidB$(buffer, 1, 4) ' Magic number
version = MidB$(buffer, 5, 2) ' Version
flags = MidB$(buffer, 7, 1) ' Flags byte
dataLen = MidB$(buffer, 8, 4) ' Data length
Reading File Header
Dim fileData As String
Open fileName For Binary As #1
fileData = Input$(100, #1)
Close #1
signature = MidB$(fileData, 1, 4)
If signature = "RIFF" Then
fileSize = MidB$(fileData, 5, 4)
End If
Processing Network Packet
Dim packet As String
packet = Socket.Receive()
header = MidB$(packet, 1, 16)
payload = MidB$(packet, 17) ' Rest of packet
Extracting Length-Prefixed String
Dim message As String
message = buffer
lenByte = MidB$(message, 1, 1)
strLen = AscB(lenByte)
text = MidB$(message, 2, strLen)
Reading BMP Image Data
Dim bmpData As String
Open "image.bmp" For Binary As #1
bmpData = Input$(LOF(1), #1)
Close #1
fileType = MidB$(bmpData, 1, 2) ' "BM"
fileSize = MidB$(bmpData, 3, 4) ' File size
offset = MidB$(bmpData, 11, 4) ' Pixel data offset
Chunked Binary Processing
Dim data As String, chunk As String
Dim pos As Long
data = GetBinaryData()
pos = 1
Do While pos <= LenB(data)
chunk = MidB$(data, pos, 512)
ProcessChunk chunk
pos = pos + 512
Loop
Extracting Record Fields
Dim record As String
record = ReadBinaryRecord()
' Fixed-position record layout
customerID = MidB$(record, 1, 10)
orderDate = MidB$(record, 11, 8)
amount = MidB$(record, 19, 8)
Parsing IPv4 Address Bytes
Dim ipBytes As String
ipBytes = GetIPv4Bytes()
octet1 = AscB(MidB$(ipBytes, 1, 1))
octet2 = AscB(MidB$(ipBytes, 2, 1))
octet3 = AscB(MidB$(ipBytes, 3, 1))
octet4 = AscB(MidB$(ipBytes, 4, 1))
Reading Bitmap Header Fields
Dim dibHeader As String
dibHeader = MidB$(bmpData, 15, 40) ' DIB header
width = MidB$(dibHeader, 5, 4)
height = MidB$(dibHeader, 9, 4)
bitsPerPixel = MidB$(dibHeader, 15, 2)
Protocol Message Parsing
Dim msg As String
msg = ReceiveMessage()
msgID = MidB$(msg, 1, 4)
timestamp = MidB$(msg, 5, 8)
sender = MidB$(msg, 13, 16)
payload = MidB$(msg, 29) ' Remaining bytes
Related Functions
MidB: Variant version that returns aVariantMid$: Character-based version that counts charactersLeftB$: Returns bytes from the left side of a stringRightB$: Returns bytes from the right side of a stringLenB: Returns the number of bytes in a stringInStrB: Finds byte position of a substringAscB: Returns the byte value at a positionChrB$: Returns a string containing a single byte
Best Practices
- Use
MidB$when working with binary data or byte-oriented protocols - Be careful with DBCS strings - splitting may corrupt multi-byte characters
- Always validate byte positions and lengths before extraction
- Use
LenBto get byte length, notLen - Remember that byte positions are 1-based, not 0-based
- Prefer
MidB$overMid$for binary file operations - Combine with
AscBandChrB$for byte-level manipulation - Document byte offsets and field sizes for binary structures
- Test DBCS string operations on appropriate language systems
- Use constants for magic numbers and field offsets
Performance Considerations
MidB$is slightly faster thanMid$for binary data- No performance penalty for requesting bytes beyond string end
- Direct byte extraction is faster than loops with
AscB - Extracting large byte ranges is efficient
- Minimal overhead compared to
Mid$in SBCS environments
Character Set Behavior
| Environment | Bytes per Char | Notes |
|---|---|---|
| English Windows | 1 byte | MidB$ and Mid$ behave identically |
| DBCS Windows | 1-2 bytes | MidB$ may split multi-byte characters |
| Unicode VB6 | 2 bytes | Internal strings are Unicode but converted |
| Binary Data | N/A | MidB$ treats data as raw bytes |
Common Pitfalls
- Using
MidB$on DBCS strings without checking character boundaries - Confusing byte positions with character positions
- Using 0-based indexing (VB6 strings are 1-based)
- Not handling
Nullstring values (causes runtime error) - Passing negative or zero start position (causes runtime error)
- Using
MidB$for text processing (useMid$instead) - Forgetting that VB6 strings are internally Unicode
- Assuming one byte equals one character in all locales
- Not validating that
startposition is within bounds
Limitations
- May corrupt DBCS characters if not used carefully
- Returns
Nullif the string argument isNull - Start position must be positive (1 or greater)
- Length parameter cannot be negative
- Not suitable for modern Unicode string processing
- Limited to VB6's internal string representation
- May produce unexpected results with emoji or complex Unicode
- Cannot modify the original string (read-only operation)