VB6Parse / Library / Conversion / oct_dollar

VB6 Library Reference

Oct$ Function

The Oct$ function in Visual Basic 6 returns a string representing the octal (base-8) value of a number. The function name stands for "Octal String".

Syntax

Oct$(number)

Parameters

Return Value

Returns a String representing the octal value of the number. The returned string contains only the digits 0-7, without a leading "0" or "&O" prefix.

Behavior and Characteristics

Data Type Handling

Range Considerations

Common Usage Patterns

1. Basic Octal Conversion

Dim octStr As String
octStr = Oct$(64)  ' Returns "100"
octStr = Oct$(8)   ' Returns "10"
octStr = Oct$(511) ' Returns "777"

2. Converting Negative Numbers

Dim octStr As String
octStr = Oct$(-1)  ' Returns "177777" (Integer range, two's complement)

3. File Permission Representation

Function FormatPermissions(permissions As Integer) As String
' Unix-style file permissions (e.g., 755, 644)
FormatPermissions = Oct$(permissions)
End Function

Dim perms As String
perms = FormatPermissions(&H1ED)  ' Returns "755"

4. Bit Mask Display

Dim flags As Integer
Dim octDisplay As String
flags = &H1FF
octDisplay = "Flags: " & Oct$(flags)  ' "Flags: 777"

5. Color Component Extraction (Octal)

Dim colorValue As Long
Dim component As Integer
colorValue = &HFF8040
component = (colorValue And &HFF)
Debug.Print Oct$(component)  ' Shows octal representation

6. Data Structure Field Values

Type SystemFlags
ReadWrite As Integer
Execute As Integer
End Type

Dim sysFlags As SystemFlags
sysFlags.ReadWrite = &O644  ' Octal literal
Debug.Print "RW: " & Oct$(sysFlags.ReadWrite)

7. Debugging Bit Patterns

Sub ShowBitPattern(value As Integer)
Debug.Print "Decimal: " & value
Debug.Print "Octal: " & Oct$(value)
Debug.Print "Hex: " & Hex$(value)
End Sub

8. Network Protocol Values

Dim socketMode As Integer
socketMode = &O666  ' Read/write for all
Debug.Print "Mode: " & Oct$(socketMode)

9. Conversion Table Generation

Sub GenerateOctalTable()
Dim i As Integer
For i = 0 To 64
Debug.Print i & " = " & Oct$(i)
Next i
End Sub

10. Configuration Value Formatting

Function SaveConfigValue(value As Integer) As String
' Store configuration as octal string
SaveConfigValue = "CONFIG=" & Oct$(value)
End Function

Best Practices

When to Use Oct$

  1. Unix-style Permissions: Representing file or directory permissions (e.g., 755, 644)
  2. Bit Pattern Analysis: When examining data in groups of 3 bits
  3. Legacy System Integration: Working with systems that use octal notation
  4. Debugging: Displaying bit patterns in a more compact form than binary
  5. Configuration Files: Storing numeric values in octal format

Formatting Output

' Add prefix for clarity
Debug.Print "Octal: &O" & Oct$(value)

' Pad with leading zeros
Debug.Print Right$("000" & Oct$(value), 3)

Type Safety

' Explicitly convert to ensure correct range
Dim longValue As Long
longValue = 1000000
Debug.Print Oct$(longValue)  ' Uses Long range

Performance Considerations

Octal Literals in VB6

VB6 supports octal literals using the &O prefix:

Dim octValue As Integer
octValue = &O777  ' Octal literal (equals 511 decimal)
Debug.Print Oct$(octValue)  ' Returns "777"

Common Pitfalls

1. No Direct Reverse Function

VB6's Val() function does not parse octal strings. You need a custom function:

Function OctVal(octStr As String) As Long
Dim i As Integer
Dim result As Long
For i = 1 To Len(octStr)
result = result * 8 + Val(Mid$(octStr, i, 1))
Next i
OctVal = result
End Function

2. Two's Complement Representation

Negative numbers produce two's complement octal strings:

Debug.Print Oct$(-1)   ' "177777" (for Integer)
Debug.Print Oct$(-100) ' Not intuitive without understanding two's complement

3. Floating-Point Rounding

Debug.Print Oct$(8.5)  ' "10" (rounds to 8)
Debug.Print Oct$(8.6)  ' "11" (rounds to 9)

4. Leading Zeros Not Included

Debug.Print Oct$(8)  ' "10", not "010"
' Pad manually if needed
Debug.Print Right$("000" & Oct$(8), 3)  ' "010"

5. No Prefix in Output

Unlike some languages, VB6's Oct$ doesn't include the &O prefix:

Debug.Print Oct$(64)  ' "100", not "&O100"

Limitations

← Back to Conversion | View all functions