<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="VB6Parse Library Reference - leftb - String">
<title>leftb - String - VB6Parse Library Reference</title>
<link rel="stylesheet" href="../../../assets/css/style.css">
<link rel="stylesheet" href="../../../assets/css/docs-style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
<script src="../../../assets/js/theme-switcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<header class="docs-header">
<div class="container">
<h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/string/index.html">String</a> / leftb</h1>
<p class="tagline">VB6 Library Reference</p>
</div>
</header>
<nav class="docs-nav">
<div class="container">
<a href="../../../index.html">Home</a>
<a href="../../../library/index.html">Library Reference</a>
<a href="../../../documentation.html">Documentation</a>
<a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
<a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
<button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
<span class="theme-icon">🌙</span>
</button>
</div>
</nav>
<main class="container">
<article class="library-item">
<h1 id="leftb-function">LeftB Function</h1>
<p>Returns a Variant (String) containing a specified number of bytes from the left side of a string.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">LeftB(string, length)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>string</code> (Required): String expression from which leftmost bytes are returned</li>
<li>If string contains Null, Null is returned</li>
<li><code>length</code> (Required): Numeric expression indicating how many bytes to return</li>
<li>If 0, empty string ("") is returned</li>
<li>If greater than or equal to number of bytes in string, entire string is returned</li>
<li>Must be non-negative (negative values cause error)</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a Variant containing a String:
- Contains the specified number of bytes from the left side of the string
- Returns empty string if length is 0
- Returns entire string if length >= LenB(string)
- Returns Null if string argument is Null
- Returns Variant type (<code>LeftB$</code> variant returns String type directly)</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>LeftB</code> function extracts bytes from the beginning of a string:
- Returns leftmost bytes up to specified length
- Operates on byte level, not character level
- Particularly useful with double-byte character sets (DBCS)
- Complements <code>RightB</code> function (which returns rightmost bytes)
- Works with <code>MidB</code> function for complete byte-level substring extraction
- Zero-based extraction: LeftB("ABC", 2) returns first 2 bytes
- Safe with lengths exceeding string byte length (returns full string)
- Null propagates through the function
- Negative length raises Error 5 (Invalid procedure call or argument)
- Common for extracting binary data, protocol headers, file signatures
- More efficient than MidB(string, 1, length) for left extraction
- <code>LeftB$</code> variant returns String type (not Variant) for slight performance gain
- Cannot extract from right side (use <code>RightB</code> for that)
- Cannot skip bytes (use <code>MidB</code> for that)
- Does not modify original string (strings are immutable)</p>
<h2 id="differences-from-left-function">Differences from <code>Left</code> Function</h2>
<ul>
<li><code>Left</code> operates on characters, <code>LeftB</code> operates on bytes</li>
<li>In single-byte character sets (SBCS), they are equivalent</li>
<li>In double-byte character sets (DBCS), one character may be multiple bytes</li>
<li><code>LeftB</code> is essential for binary data manipulation</li>
<li><code>LeftB</code> is used with <code>LenB</code> (byte length) rather than Len (character length)</li>
</ul>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Binary Data</strong>: Extract bytes from binary strings</li>
<li><strong>Protocol Headers</strong>: Parse network protocol headers</li>
<li><strong>File Signatures</strong>: Identify file types by magic bytes</li>
<li><strong>DBCS Strings</strong>: Work with Japanese, Chinese, Korean text at byte level</li>
<li><strong>Fixed Byte Records</strong>: Parse fixed-width binary records</li>
<li><strong>Byte Validation</strong>: Check byte prefixes in data</li>
<li><strong>Binary Structures</strong>: Extract fields from binary structures</li>
<li><strong>Network Data</strong>: Process raw network packet data</li>
</ol>
<h2 id="basic-usage-examples">Basic Usage Examples</h2>
<pre><code class="language-vbnet">' Example 1: Basic byte extraction
Dim data As String
data = Chr$(65) & Chr$(66) & Chr$(67) ' "ABC"
Debug.Print LeftB(data, 2) ' First 2 bytes
Debug.Print LeftB(data, 1) ' First byte
' Example 2: File signature checking
Dim fileData As String
Open "test.exe" For Binary As #1
fileData = Input$(2, #1)
Close #1
If LeftB(fileData, 2) = "MZ" Then
Debug.Print "DOS/Windows executable"
End If
' Example 3: Protocol header extraction
Dim packet As String
packet = ReceiveNetworkData()
Dim header As String
header = LeftB(packet, 16) ' 16-byte header
' Example 4: DBCS text handling
Dim japaneseText As String
japaneseText = LoadJapaneseText() ' Load Japanese text
Dim firstBytes As String
firstBytes = LeftB(japaneseText, 4) ' First 4 bytes (may be 2 DBCS chars)</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<pre><code class="language-vbnet">' Pattern 1: Extract binary header
Function GetBinaryHeader(data As String, headerSize As Long) As String
If LenB(data) < headerSize Then
GetBinaryHeader = data
Else
GetBinaryHeader = LeftB(data, headerSize)
End If
End Function
' Pattern 2: Validate magic bytes
Function ValidateMagicBytes(data As String, magic As String) As Boolean
ValidateMagicBytes = (LeftB(data, LenB(magic)) = magic)
End Function
' Pattern 3: Extract record ID
Function GetRecordID(record As String) As String
' First 8 bytes contain record ID
GetRecordID = LeftB(record, 8)
End Function
' Pattern 4: Parse network packet
Sub ParsePacket(packet As String)
Dim header As String
Dim payload As String
header = LeftB(packet, 20) ' 20-byte header
payload = MidB(packet, 21) ' Remaining bytes
' Process header and payload
End Sub</code></pre>
<h2 id="see-also">See Also</h2>
<ul>
<li><code>LeftB$</code>: String-returning variant of <code>LeftB</code></li>
<li><code>RightB</code>: Returns rightmost bytes from string</li>
<li><code>RightB$</code>: String-returning variant of <code>RightB</code></li>
<li><code>MidB</code>: Returns bytes from middle of string</li>
<li><code>MidB$</code>: String-returning variant of <code>MidB</code></li>
<li><code>LenB</code>: Returns byte length of string</li>
<li><code>Left</code>: Character-based left extraction</li>
<li><code>Right</code>: Character-based right extraction</li>
<li><code>Mid</code>: Character-based middle extraction</li>
</ul>
</article>
<div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
<p>
<a href="index.html">← Back to String</a> |
<a href="../index.html">View all functions</a>
</p>
</div>
</main>
<footer>
<div class="container">
<p>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>