I want to test AI programming, but Claude-Code blocks Chinese users. I would be very grateful if you could implement a function ReadTextContent using Claude-Code. The problem is as follows:
Code:
Please complete a function in VB6 to read file content from a file, with the function declaration as follows:
Public Function ReadTextContent(FileName As String, Optional ByVal IsRaw16Bit As Boolean = False, Optional ByVal CodePage As MSCodePages) As String
End Function
Note: the constants of MSCodePages are defined as follows:
Enum MSCodePages
CP_ACP = 0 ' Default ANSI code page.
CP_UTF8 = 65001 ' UTF8.
CP_UTF16_LE = 1200 ' UTF16 - little endian.
CP_UTF16_BE = 1201 ' UTF16 - big endian.
CP_UTF32_LE = 12000 ' UTF32 - little endian.
CP_UTF32_BE = 12001 ' UTF32 - big endian.
CP_UNKNOWN = -1
End Enum
If Claude-Code can complete the above function, please post the result. I'll compare the performance of the code generated by Claude-Code with the performance of RC6.ReadTextContent. Thanks!
Note:
Please do not modify the code generated by Claude-Code. I want to know how accurate the code generated by Claude-Code is.
Last edited by SearchingDataOnly; May 14th, 2026 at 07:19 AM.
I don't have a Claude subscription, but I ran it past ChatGPT to see what it would come up with. Below is directly from ChatGPT and is unaltered/untested...you can tell I've been coding with it since it uses my "l_" prefix style Interesting that it picked that up from me.
--------------------------------- START OF CHATGPT OUTPUT
I’ll treat this as production VB6 rather than a toy example: byte-safe file reading, BOM detection, optional raw UTF-16 handling, and explicit code page conversion without depending on ADODB.Stream.
One important VB6 wrinkle: with the exact signature shown, an omitted CodePage will be indistinguishable from CP_ACP because CP_ACP = 0. The implementation below still honors BOMs first, so UTF-8/UTF-16 files will read correctly even when CodePage is omitted.
Thought for 51s
Here is my best-effort VB6 implementation. It reads the file as bytes, detects BOMs, supports ANSI/UTF-8/UTF-16/UTF-32, and returns a normal VB6 Unicode String.
Code:
Option Explicit
Private Const MB_ERR_INVALID_CHARS As Long = &H8&
Private Const ERR_BASE As Long = vbObjectError + 513
Private Declare Function MultiByteToWideChar Lib "kernel32" ( _
ByVal CodePage As Long, _
ByVal dwFlags As Long, _
ByRef lpMultiByteStr As Any, _
ByVal cbMultiByte As Long, _
ByVal lpWideCharStr As Long, _
ByVal cchWideChar As Long _
) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByVal Destination As Long, _
ByRef Source As Any, _
ByVal Length As Long _
)
Public Function ReadTextContent( _
FileName As String, _
Optional ByVal IsRaw16Bit As Boolean = False, _
Optional ByVal CodePage As MSCodePages _
) As String
Dim l_FileNum As Integer
Dim l_FileLen As Long
Dim la_Bytes() As Byte
Dim l_CodePage As MSCodePages
Dim l_Offset As Long
Dim l_HasBom As Boolean
On Error GoTo CleanFail
l_FileNum = FreeFile
Open FileName For Binary Access Read As #l_FileNum
l_FileLen = LOF(l_FileNum)
If l_FileLen = 0 Then
Close #l_FileNum
ReadTextContent = vbNullString
Exit Function
End If
ReDim la_Bytes(0 To l_FileLen - 1) As Byte
Get #l_FileNum, , la_Bytes
Close #l_FileNum
l_FileNum = 0
l_CodePage = CodePage
l_Offset = 0
' ---------------------------------------------------------------------
' BOM detection.
'
' Important:
' UTF-32 LE BOM starts FF FE 00 00, which also starts like UTF-16 LE,
' so check UTF-32 before UTF-16.
' ---------------------------------------------------------------------
If l_FileLen >= 4 Then
If la_Bytes(0) = &HFF And la_Bytes(1) = &HFE And _
la_Bytes(2) = &H0 And la_Bytes(3) = &H0 Then
l_CodePage = CP_UTF32_LE
l_Offset = 4
l_HasBom = True
ElseIf la_Bytes(0) = &H0 And la_Bytes(1) = &H0 And _
la_Bytes(2) = &HFE And la_Bytes(3) = &HFF Then
l_CodePage = CP_UTF32_BE
l_Offset = 4
l_HasBom = True
End If
End If
If Not l_HasBom Then
If l_FileLen >= 3 Then
If la_Bytes(0) = &HEF And la_Bytes(1) = &HBB And la_Bytes(2) = &HBF Then
l_CodePage = CP_UTF8
l_Offset = 3
l_HasBom = True
End If
End If
End If
If Not l_HasBom Then
If l_FileLen >= 2 Then
If la_Bytes(0) = &HFF And la_Bytes(1) = &HFE Then
l_CodePage = CP_UTF16_LE
l_Offset = 2
l_HasBom = True
ElseIf la_Bytes(0) = &HFE And la_Bytes(1) = &HFF Then
l_CodePage = CP_UTF16_BE
l_Offset = 2
l_HasBom = True
End If
End If
End If
' ---------------------------------------------------------------------
' If no BOM was found, IsRaw16Bit means "treat the file bytes as raw
' little-endian UTF-16", which is how VB6 stores String data internally.
'
' With the exact function declaration supplied, omitted CodePage defaults
' to 0, which is CP_ACP. Therefore, CP_UNKNOWN can only be detected if the
' caller explicitly passes CP_UNKNOWN.
' ---------------------------------------------------------------------
If Not l_HasBom Then
If IsRaw16Bit Then
l_CodePage = CP_UTF16_LE
ElseIf l_CodePage = CP_UNKNOWN Then
l_CodePage = CP_ACP
End If
End If
Select Case l_CodePage
Case CP_ACP, CP_UTF8
ReadTextContent = BytesToUnicodeByCodePage(la_Bytes, l_Offset, l_FileLen - l_Offset, l_CodePage)
Case CP_UTF16_LE
ReadTextContent = Utf16LeBytesToString(la_Bytes, l_Offset, l_FileLen - l_Offset)
Case CP_UTF16_BE
ReadTextContent = Utf16BeBytesToString(la_Bytes, l_Offset, l_FileLen - l_Offset)
Case CP_UTF32_LE
ReadTextContent = Utf32BytesToString(la_Bytes, l_Offset, l_FileLen - l_Offset, True)
Case CP_UTF32_BE
ReadTextContent = Utf32BytesToString(la_Bytes, l_Offset, l_FileLen - l_Offset, False)
Case Else
Err.Raise ERR_BASE + 1, "ReadTextContent", _
"Unsupported code page: " & CStr(l_CodePage)
End Select
Exit Function
CleanFail:
On Error Resume Next
If l_FileNum <> 0 Then
Close #l_FileNum
End If
Err.Raise Err.Number, Err.Source, Err.Description
End Function
Private Function BytesToUnicodeByCodePage( _
ByRef Bytes() As Byte, _
ByVal Offset As Long, _
ByVal ByteCount As Long, _
ByVal CodePage As Long _
) As String
Dim l_Flags As Long
Dim l_Chars As Long
Dim l_Result As String
If ByteCount <= 0 Then
BytesToUnicodeByCodePage = vbNullString
Exit Function
End If
If CodePage = CP_UTF8 Then
l_Flags = MB_ERR_INVALID_CHARS
Else
l_Flags = 0
End If
l_Chars = MultiByteToWideChar( _
CodePage, _
l_Flags, _
Bytes(Offset), _
ByteCount, _
0, _
0 _
)
' Some older Windows configurations are fussy about MB_ERR_INVALID_CHARS.
' Retry UTF-8 without the strict flag before giving up.
If l_Chars = 0 And l_Flags <> 0 Then
l_Flags = 0
l_Chars = MultiByteToWideChar( _
CodePage, _
l_Flags, _
Bytes(Offset), _
ByteCount, _
0, _
0 _
)
End If
If l_Chars = 0 Then
Err.Raise ERR_BASE + 2, "ReadTextContent", _
"Unable to convert file content using code page " & CStr(CodePage) & "."
End If
l_Result = String$(l_Chars, vbNullChar)
l_Chars = MultiByteToWideChar( _
CodePage, _
l_Flags, _
Bytes(Offset), _
ByteCount, _
StrPtr(l_Result), _
l_Chars _
)
If l_Chars = 0 Then
Err.Raise ERR_BASE + 3, "ReadTextContent", _
"Unable to convert file content using code page " & CStr(CodePage) & "."
End If
BytesToUnicodeByCodePage = l_Result
End Function
Private Function Utf16LeBytesToString( _
ByRef Bytes() As Byte, _
ByVal Offset As Long, _
ByVal ByteCount As Long _
) As String
Dim l_Result As String
If ByteCount <= 0 Then
Utf16LeBytesToString = vbNullString
Exit Function
End If
If (ByteCount Mod 2) <> 0 Then
Err.Raise ERR_BASE + 4, "ReadTextContent", _
"Invalid UTF-16 little-endian content. Byte count is odd."
End If
l_Result = String$(ByteCount \ 2, vbNullChar)
CopyMemory StrPtr(l_Result), Bytes(Offset), ByteCount
Utf16LeBytesToString = l_Result
End Function
Private Function Utf16BeBytesToString( _
ByRef Bytes() As Byte, _
ByVal Offset As Long, _
ByVal ByteCount As Long _
) As String
Dim la_Swapped() As Byte
Dim ii As Long
If ByteCount <= 0 Then
Utf16BeBytesToString = vbNullString
Exit Function
End If
If (ByteCount Mod 2) <> 0 Then
Err.Raise ERR_BASE + 5, "ReadTextContent", _
"Invalid UTF-16 big-endian content. Byte count is odd."
End If
ReDim la_Swapped(0 To ByteCount - 1) As Byte
For ii = 0 To ByteCount - 1 Step 2
la_Swapped(ii) = Bytes(Offset + ii + 1)
la_Swapped(ii + 1) = Bytes(Offset + ii)
Next ii
Utf16BeBytesToString = Utf16LeBytesToString(la_Swapped, 0, ByteCount)
End Function
Private Function Utf32BytesToString( _
ByRef Bytes() As Byte, _
ByVal Offset As Long, _
ByVal ByteCount As Long, _
ByVal IsLittleEndian As Boolean _
) As String
Dim l_Units As Long
Dim la_Words() As Integer
Dim l_WordCount As Long
Dim ii As Long
Dim d_CodePoint As Double
Dim l_CodePoint As Long
Dim l_Temp As Long
Dim l_HighSurrogate As Long
Dim l_LowSurrogate As Long
Dim l_Result As String
If ByteCount <= 0 Then
Utf32BytesToString = vbNullString
Exit Function
End If
If (ByteCount Mod 4) <> 0 Then
Err.Raise ERR_BASE + 6, "ReadTextContent", _
"Invalid UTF-32 content. Byte count is not divisible by 4."
End If
l_Units = ByteCount \ 4
' Worst case: every UTF-32 code point becomes a UTF-16 surrogate pair.
ReDim la_Words(0 To (l_Units * 2) - 1) As Integer
For ii = 0 To ByteCount - 1 Step 4
If IsLittleEndian Then
d_CodePoint = CDbl(Bytes(Offset + ii)) + _
CDbl(Bytes(Offset + ii + 1)) * 256# + _
CDbl(Bytes(Offset + ii + 2)) * 65536# + _
CDbl(Bytes(Offset + ii + 3)) * 16777216#
Else
d_CodePoint = CDbl(Bytes(Offset + ii + 3)) + _
CDbl(Bytes(Offset + ii + 2)) * 256# + _
CDbl(Bytes(Offset + ii + 1)) * 65536# + _
CDbl(Bytes(Offset + ii)) * 16777216#
End If
If d_CodePoint > CDbl(&H10FFFF) Then
Err.Raise ERR_BASE + 7, "ReadTextContent", _
"Invalid UTF-32 content. Code point is greater than U+10FFFF."
End If
l_CodePoint = CLng(d_CodePoint)
If l_CodePoint >= &HD800& And l_CodePoint <= &HDFFF& Then
Err.Raise ERR_BASE + 8, "ReadTextContent", _
"Invalid UTF-32 content. Code point is in the UTF-16 surrogate range."
End If
If l_CodePoint < &H10000 Then
la_Words(l_WordCount) = UShortToInteger(l_CodePoint)
l_WordCount = l_WordCount + 1
Else
l_Temp = l_CodePoint - &H10000
l_HighSurrogate = &HD800& + (l_Temp \ &H400&)
l_LowSurrogate = &HDC00& + (l_Temp And &H3FF&)
la_Words(l_WordCount) = UShortToInteger(l_HighSurrogate)
l_WordCount = l_WordCount + 1
la_Words(l_WordCount) = UShortToInteger(l_LowSurrogate)
l_WordCount = l_WordCount + 1
End If
Next ii
If l_WordCount = 0 Then
Utf32BytesToString = vbNullString
Exit Function
End If
l_Result = String$(l_WordCount, vbNullChar)
CopyMemory StrPtr(l_Result), la_Words(0), l_WordCount * 2
Utf32BytesToString = l_Result
End Function
Private Function UShortToInteger(ByVal Value As Long) As Integer
If Value > &H7FFF& Then
UShortToInteger = CInt(Value - &H10000)
Else
UShortToInteger = CInt(Value)
End If
End Function
One small improvement I’d make to the declaration, if you’re allowed to change it, is this:
Code:
Public Function ReadTextContent( _
FileName As String, _
Optional ByVal IsRaw16Bit As Boolean = False, _
Optional ByVal CodePage As MSCodePages = CP_UNKNOWN _
) As String
Without = CP_UNKNOWN, omitted CodePage becomes 0, which is also CP_ACP, so the function cannot know whether the caller intentionally requested ANSI or simply omitted the argument. The implementation above works around that reasonably by letting BOM detection take priority.
' Win32 API declarations (place in a module)
Private Declare Function MultiByteToWideChar Lib "kernel32" ( _
ByVal CodePage As Long, _
ByVal dwFlags As Long, _
ByVal lpMultiByteStr As Long, _
ByVal cchMultiByte As Long, _
ByVal lpWideCharStr As Long, _
ByVal cchWideChar As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, Source As Any, ByVal Length As Long)
Public Function ReadTextContent(FileName As String, _
Optional ByVal IsRaw16Bit As Boolean = False, _
Optional ByVal CodePage As MSCodePages = CP_ACP) As String
Dim iFile As Integer
Dim lFileLen As Long
Dim abBytes() As Byte
Dim lBOMLen As Long
Dim lDetected As MSCodePages
' --- 1. Read entire file into byte array ---
iFile = FreeFile
Open FileName For Binary Access Read As #iFile
lFileLen = LOF(iFile)
If lFileLen = 0 Then
Close #iFile
ReadTextContent = ""
Exit Function
End If
ReDim abBytes(0 To lFileLen - 1)
Get #iFile, , abBytes
Close #iFile
' --- 2. Raw 16-bit shortcut (no BOM handling, straight copy into BSTR) ---
If IsRaw16Bit Then
' Treat bytes as raw UTF-16 LE pairs; truncate odd trailing byte if any
Dim lChars As Long
lChars = lFileLen \ 2
Dim sRaw As String
sRaw = String(lChars, vbNullChar)
CopyMemory ByVal StrPtr(sRaw), abBytes(0), lChars * 2
ReadTextContent = sRaw
Exit Function
End If
' --- 3. BOM detection (overrides CodePage parameter) ---
lDetected = CP_UNKNOWN
lBOMLen = 0
If lFileLen >= 4 Then
If abBytes(0) = &HFF And abBytes(1) = &HFE And _
abBytes(2) = 0 And abBytes(3) = 0 Then
lDetected = CP_UTF32_LE : lBOMLen = 4
ElseIf abBytes(0) = 0 And abBytes(1) = 0 And _
abBytes(2) = &HFE And abBytes(3) = &HFF Then
lDetected = CP_UTF32_BE : lBOMLen = 4
End If
End If
If lDetected = CP_UNKNOWN And lFileLen >= 3 Then
If abBytes(0) = &HEF And abBytes(1) = &HBB And abBytes(2) = &HBF Then
lDetected = CP_UTF8 : lBOMLen = 3
End If
End If
If lDetected = CP_UNKNOWN And lFileLen >= 2 Then
If abBytes(0) = &HFF And abBytes(1) = &HFE Then
lDetected = CP_UTF16_LE : lBOMLen = 2
ElseIf abBytes(0) = &HFE And abBytes(1) = &HFF Then
lDetected = CP_UTF16_BE : lBOMLen = 2
End If
End If
' Fall back to caller-supplied CodePage if no BOM found
If lDetected = CP_UNKNOWN Then lDetected = CodePage
' --- 4. Decode payload (after BOM) ---
Dim lPayload As Long
lPayload = lFileLen - lBOMLen
If lPayload <= 0 Then
ReadTextContent = ""
Exit Function
End If
Select Case lDetected
' ----------------------------------------------------------------
Case CP_UTF16_LE
' Native VB6 format: straight copy
Dim lU16Chars As Long
lU16Chars = lPayload \ 2
Dim sU16 As String
sU16 = String(lU16Chars, vbNullChar)
CopyMemory ByVal StrPtr(sU16), abBytes(lBOMLen), lU16Chars * 2
ReadTextContent = sU16
' ----------------------------------------------------------------
Case CP_UTF16_BE
' Swap every byte pair, then copy
Dim i As Long
Dim lBEChars As Long
lBEChars = lPayload \ 2
Dim sBE As String
sBE = String(lBEChars, vbNullChar)
Dim abSwapped() As Byte
ReDim abSwapped(0 To lBEChars * 2 - 1)
For i = 0 To lBEChars - 1
abSwapped(i * 2) = abBytes(lBOMLen + i * 2 + 1)
abSwapped(i * 2 + 1) = abBytes(lBOMLen + i * 2)
Next i
CopyMemory ByVal StrPtr(sBE), abSwapped(0), lBEChars * 2
ReadTextContent = sBE
' ----------------------------------------------------------------
Case CP_UTF32_LE, CP_UTF32_BE
' Convert each UTF-32 codepoint to UTF-16 LE manually.
' Codepoints <= U+FFFF -> single UTF-16 unit
' Codepoints > U+FFFF -> surrogate pair
Dim lCP32Count As Long
lCP32Count = lPayload \ 4
Dim abU16Out() As Byte
ReDim abU16Out(0 To lCP32Count * 4 - 1) ' worst case (all surrogates)
Dim lOutIdx As Long
lOutIdx = 0
Dim j As Long
For j = 0 To lCP32Count - 1
Dim lBase As Long
lBase = lBOMLen + j * 4
Dim cp As Long
If lDetected = CP_UTF32_LE Then
' little endian: byte0 is LSB
cp = CLng(abBytes(lBase)) Or _
(CLng(abBytes(lBase + 1)) * &H100&) Or _
(CLng(abBytes(lBase + 2)) * &H10000) Or _
(CLng(abBytes(lBase + 3)) * &H1000000)
Else
' big endian: byte0 is MSB
cp = (CLng(abBytes(lBase)) * &H1000000) Or _
(CLng(abBytes(lBase + 1)) * &H10000) Or _
(CLng(abBytes(lBase + 2)) * &H100&) Or _
CLng(abBytes(lBase + 3))
End If
' Clamp to valid Unicode range
If cp < 0 Or cp > &H10FFFF Then cp = &HFFFD ' replacement char
If cp <= &HFFFF Then
abU16Out(lOutIdx) = CByte(cp And &HFF)
abU16Out(lOutIdx + 1) = CByte((cp \ &H100) And &HFF)
lOutIdx = lOutIdx + 2
Else
' Surrogate pair
Dim cpAdj As Long
cpAdj = cp - &H10000
Dim wHigh As Long, wLow As Long
wHigh = &HD800 + (cpAdj \ &H400)
wLow = &HDC00 + (cpAdj And &H3FF)
abU16Out(lOutIdx) = CByte(wHigh And &HFF)
abU16Out(lOutIdx + 1) = CByte((wHigh \ &H100) And &HFF)
abU16Out(lOutIdx + 2) = CByte(wLow And &HFF)
abU16Out(lOutIdx + 3) = CByte((wLow \ &H100) And &HFF)
lOutIdx = lOutIdx + 4
End If
Next j
Dim lU32ResultChars As Long
lU32ResultChars = lOutIdx \ 2
Dim sU32 As String
sU32 = String(lU32ResultChars, vbNullChar)
CopyMemory ByVal StrPtr(sU32), abU16Out(0), lOutIdx
ReadTextContent = sU32
' ----------------------------------------------------------------
Case CP_UTF8
' Use Win32 to convert UTF-8 -> UTF-16 LE
Dim lU8WideLen As Long
lU8WideLen = MultiByteToWideChar(65001, 0, VarPtr(abBytes(lBOMLen)), lPayload, 0, 0)
If lU8WideLen > 0 Then
Dim sU8 As String
sU8 = String(lU8WideLen, vbNullChar)
MultiByteToWideChar 65001, 0, VarPtr(abBytes(lBOMLen)), lPayload, StrPtr(sU8), lU8WideLen
ReadTextContent = sU8
End If
' ----------------------------------------------------------------
Case Else
' CP_ACP or any other ANSI code page: let Win32 handle it
Dim lACPWideLen As Long
lACPWideLen = MultiByteToWideChar(lDetected, 0, VarPtr(abBytes(lBOMLen)), lPayload, 0, 0)
If lACPWideLen > 0 Then
Dim sACP As String
sACP = String(lACPWideLen, vbNullChar)
MultiByteToWideChar lDetected, 0, VarPtr(abBytes(lBOMLen)), lPayload, StrPtr(sACP), lACPWideLen
ReadTextContent = sACP
End If
End Select
End Function
Here’s a complete implementation. I’ll make a few assumptions explicit:
• If IsRaw16Bit = True, the file is read as raw UTF-16 LE (VB6’s native String format) without BOM processing — useful when you already know it’s a raw binary Unicode dump.
• If IsRaw16Bit = False, BOM detection takes priority over the CodePage parameter; if no BOM is found, CodePage is used as fallback.
• UTF-32 is converted via Win32 MultiByteToWideChar after manually swapping BE to LE where needed.
A few notes worth calling out:
BOM priority over CodePage — This matches what virtually every real-world text editor does. If you want to force a specific encoding even when a BOM is present, you’d need to add a ByVal IgnoreBOM As Boolean = False parameter and skip the detection block.
UTF-32 via manual loop, not MultiByteToWideChar — Windows doesn’t support UTF-32 in MultiByteToWideChar at all, so the surrogate-pair math has to be done by hand. The lOutIdx tracks actual bytes written since surrogates make the output larger than the input codepoint count.
The CLng(abBytes(...)) * &H1000000 trick — VB6 integer literals are Integer by default up to 32767, so without &H1000000 (Long literal) the shift would overflow. Worth being aware of if you ever modify that arithmetic.
CP_UNKNOWN as fallback — If neither a BOM is detected nor a meaningful CodePage is passed, it falls through to the Case Else which calls MultiByteToWideChar with -1. Windows will reject that and return 0 characters, giving you an empty string rather than garbage or a crash. You may want to handle that edge case more explicitly depending on your use.????????????????
————
Directly from Claude with the given prompt
Last edited by Arnoutdv; May 14th, 2026 at 08:07 AM.
Thank you very much for your help. I tested the code generated by ChatGPT and Claude-Code respectively, and both of them worked accurately. This is really amazing.
Regarding performance, in IDE mode, the performance of the code from RC6, ChatGPT, and ClaudeCode is almost the same. In binary mode, the RC6 code is slightly faster, about 3%-5% faster. This difference in performance is almost negligible.