VB6Parse / Library / String / ascb

VB6 Library Reference

AscB Function

Returns an Integer representing the byte value (ANSI code) of the first byte in a string. The "B" suffix indicates this is the byte (ANSI) version of the Asc function.

Syntax

AscB(string)

Parameters

Returns

Returns an Integer (0-255) representing the byte value of the first byte in the string.

Remarks

Typical Uses

  1. Byte-level text analysis - Examine individual bytes in ANSI strings
  2. Binary data processing - Extract byte values from binary strings
  3. File format parsing - Read byte values from file headers or data structures
  4. Legacy protocol support - Work with protocols that use ANSI byte values
  5. Character encoding detection - Analyze byte patterns in text
  6. Checksum calculations - Use byte values for checksums or hash calculations
  7. Low-level string comparison - Compare strings at the byte level

Basic Examples

' Example 1: Simple byte value
Dim byteVal As Integer
byteVal = AscB("A")  ' Returns 65
' Example 2: Extended ANSI character
Dim code As Integer
code = AscB("é")  ' Returns 233 (in Windows-1252 code page)
' Example 3: First byte of multi-byte character
' In DBCS (Double Byte Character Set) systems
Dim firstByte As Integer
firstByte = AscB("中")  ' Returns first byte only (varies by encoding)
' Example 4: Control character
Dim tabByte As Integer
tabByte = AscB(vbTab)  ' Returns 9

Common Patterns

Validate ASCII Range

Function IsASCII(char As String) As Boolean
If Len(char) = 0 Then Exit Function
IsASCII = (AscB(char) < 128)
End Function

Check for Control Characters

Function IsControlChar(char As String) As Boolean
If Len(char) = 0 Then Exit Function
Dim byteVal As Integer
byteVal = AscB(char)
IsControlChar = (byteVal < 32 Or byteVal = 127)
End Function

Compare Byte Values

Function CompareBytes(str1 As String, str2 As String) As Integer
If Len(str1) = 0 Or Len(str2) = 0 Then Exit Function
CompareBytes = AscB(str1) - AscB(str2)
End Function

Extract Byte Array

Function GetByteArray(text As String) As Variant
Dim bytes() As Integer
Dim i As Long

If Len(text) = 0 Then Exit Function

ReDim bytes(1 To Len(text))
For i = 1 To Len(text)
bytes(i) = AscB(Mid(text, i, 1))
Next i

GetByteArray = bytes
End Function

Calculate Simple Checksum

Function SimpleChecksum(text As String) As Long
Dim i As Long
Dim checksum As Long

For i = 1 To Len(text)
checksum = checksum + AscB(Mid(text, i, 1))
Next i

SimpleChecksum = checksum Mod 256
End Function

Detect Line Endings

Function DetectLineEnding(text As String) As String
Dim i As Long
Dim byteVal As Integer

For i = 1 To Len(text)
byteVal = AscB(Mid(text, i, 1))
If byteVal = 13 Then  ' CR
If i < Len(text) And AscB(Mid(text, i + 1, 1)) = 10 Then
DetectLineEnding = "CRLF"
Else
DetectLineEnding = "CR"
End If
Exit Function
ElseIf byteVal = 10 Then  ' LF
DetectLineEnding = "LF"
Exit Function
End If
Next i
End Function

Hex Dump Generator

Function ByteToHex(char As String) As String
If Len(char) = 0 Then Exit Function
Dim byteVal As Integer
byteVal = AscB(char)
ByteToHex = Right("0" & Hex(byteVal), 2)
End Function

Case-Insensitive Byte Compare

Function ByteEqualsIgnoreCase(char1 As String, char2 As String) As Boolean
If Len(char1) = 0 Or Len(char2) = 0 Then Exit Function

Dim byte1 As Integer, byte2 As Integer
byte1 = AscB(char1)
byte2 = AscB(char2)

' Convert uppercase to lowercase (65-90 to 97-122)
If byte1 >= 65 And byte1 <= 90 Then byte1 = byte1 + 32
If byte2 >= 65 And byte2 <= 90 Then byte2 = byte2 + 32

ByteEqualsIgnoreCase = (byte1 = byte2)
End Function

Filter Printable Characters

Function FilterPrintable(text As String) As String
Dim result As String
Dim i As Long
Dim byteVal As Integer

For i = 1 To Len(text)
byteVal = AscB(Mid(text, i, 1))
If byteVal >= 32 And byteVal <= 126 Then
result = result & Mid(text, i, 1)
End If
Next i

FilterPrintable = result
End Function

Encode for URL

Function NeedsURLEncoding(char As String) As Boolean
If Len(char) = 0 Then Exit Function

Dim byteVal As Integer
byteVal = AscB(char)

' Check if character needs encoding
If (byteVal >= 48 And byteVal <= 57) Or _
(byteVal >= 65 And byteVal <= 90) Or _
(byteVal >= 97 And byteVal <= 122) Then
NeedsURLEncoding = False
Else
NeedsURLEncoding = True
End If
End Function

Advanced Examples

Binary Data Parser

Function ParseBinaryHeader(data As String) As Variant
' Parse a binary file header (example: BMP format)
Dim header As Variant
ReDim header(1 To 4)

If Len(data) < 4 Then Exit Function

' Read signature bytes
header(1) = AscB(Mid(data, 1, 1))  ' 'B' = 66
header(2) = AscB(Mid(data, 2, 1))  ' 'M' = 77

' Read size bytes (little-endian)
header(3) = AscB(Mid(data, 3, 1))
header(4) = AscB(Mid(data, 4, 1))

ParseBinaryHeader = header
End Function

XOR Encryption/Decryption

Function XOREncrypt(text As String, key As String) As String
Dim result As String
Dim i As Long, keyPos As Long
Dim textByte As Integer, keyByte As Integer

If Len(text) = 0 Or Len(key) = 0 Then Exit Function

keyPos = 1
For i = 1 To Len(text)
textByte = AscB(Mid(text, i, 1))
keyByte = AscB(Mid(key, keyPos, 1))

result = result & ChrB(textByte Xor keyByte)

keyPos = keyPos + 1
If keyPos > Len(key) Then keyPos = 1
Next i

XOREncrypt = result
End Function

CSV Field Parser with Byte Analysis

Function ParseCSVField(field As String) As String
Dim result As String
Dim i As Long
Dim byteVal As Integer
Dim inQuotes As Boolean

For i = 1 To Len(field)
byteVal = AscB(Mid(field, i, 1))

Select Case byteVal
Case 34  ' Double quote
inQuotes = Not inQuotes
Case 44  ' Comma
If Not inQuotes Then Exit Function
result = result & Chr(byteVal)
Case Else
result = result & Chr(byteVal)
End Select
Next i

ParseCSVField = result
End Function

Character Set Validator

Function ValidateCharacterSet(text As String, validSet As String) As Boolean
Dim i As Long, j As Long
Dim textByte As Integer
Dim found As Boolean

For i = 1 To Len(text)
textByte = AscB(Mid(text, i, 1))
found = False

For j = 1 To Len(validSet)
If textByte = AscB(Mid(validSet, j, 1)) Then
found = True
Exit For
End If
Next j

If Not found Then
ValidateCharacterSet = False
Exit Function
End If
Next i

ValidateCharacterSet = True
End Function

Error Handling

Function SafeAscB(text As String) As Integer
On Error GoTo ErrorHandler

If Len(text) = 0 Then
SafeAscB = -1  ' Error indicator
Exit Function
End If

SafeAscB = AscB(text)
Exit Function

ErrorHandler:
SafeAscB = -1
End Function

Performance Notes

Best Practices

  1. Validate input - Always check for empty strings before calling AscB
  2. Use for byte operations - Prefer AscB over Asc when working with binary data
  3. Handle errors - Wrap AscB calls in error handlers when processing untrusted input
  4. Document byte values - Use constants or comments to explain non-obvious byte values
  5. Consider encoding - Be aware of system code page when working with extended ANSI
  6. Use with ChrB - Pair with ChrB for byte-to-character conversions
  7. Test edge cases - Verify behavior with empty strings, control characters, and extended ANSI
Function Returns Character Set Use Case
Asc Integer (0-255 or Unicode) System default General character codes
AscB Integer (0-255) ANSI byte value Byte-level operations
AscW Integer (0-65535) Unicode code point International text
ChrB String (ANSI) ANSI (inverse) Convert byte to character

Common Byte Values Reference

Some commonly used byte values with AscB:

Platform Notes

Limitations

← Back to String | View all functions