: 1 |
|
: Unicode Win1251
|
: 16.10.08 15:10
|
|
: Yanex | Web-: Progr.Do.am | ICQ: 387761649
|
Visual Basic 6.0 Ошибка ? StrConv - , .
|
: 1 :
: 8 : 42
|
| | #1
|
: 16.10.08 15:57
|
:
-
- Option Explicit
- Enum Code
- Win = 1
- Dos = 2
- Koi = 3
- Iso = 5
- End Enum
- Function Recode(Char As String, Src As Code, Dest As Code) As String
- Const wDos As String = ""
- Const wIso As String = "ע"
- Const wKoi As String = ""
- Const wWin As String = ""
- Const NotRecodedChar As String = "?"
-
- If Src = Dest Then
- Recode = Char
- Exit Function
- End If
-
- Dim t As String, i As Long, tt As String, a As Long, ss As String, ch As String
- If Src = Win Then
- t = Char
- Else
- Select Case Src
- Case Koi: ss = wKoi
- Case Dos: ss = wDos
- Case Iso: ss = wIso
- End Select
- For i = 1 To Len(Char)
- ch = Mid(Char, i, 1)
- If Asc(ch) < 128 Then
- t = t & ch
- Else
- a = InStr(1, ss, ch, vbBinaryCompare)
- If a = 0 Then
- t = t & NotRecodedChar
- Else
- t = t & Mid$(wWin, a, 1)
- End If
- End If
- Next i
- End If
-
- If Dest = Win Then
- Recode = t
- Else
- Select Case Dest
- Case Koi: ss = wKoi
- Case Dos: ss = wDos
- Case Iso: ss = wIso
- End Select
- For i = 1 To Len(Char)
- ch = Mid(t, i, 1)
- If Asc(ch) < 128 Then
- tt = tt & ch
- Else
- a = InStr(1, wWin, ch, vbBinaryCompare)
- If a = 0 Then
- tt = tt & NotRecodedChar
- Else
- tt = tt & Mid$(ss, a, 1)
- End If
- End If
- Next i
- Recode = tt
- End If
- End Function
-
|
: 2 : Winand
: 87 : 2795
|
Web-: winandfx.narod.ru | | #2
|
: 16.10.08 17:05
|
?- Option Explicit
- Private Const CP_UTF8 = 65001
- Private Const CP_ACP = 0
- Private Declare Function GetACP Lib "Kernel32" () As Long
- Private Declare Function WideCharToMultiByte Lib "Kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, lpUsedDefaultChar As Long) As Long
- 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 Function WToA(ByVal st As String, Optional ByVal cpg As Long = -1, Optional lFlags As Long = 0) As String
- Dim stBuffer As String
- Dim cwch As Long
- Dim pwz As Long
- Dim pwzBuffer As Long
- Dim lpUsedDefaultChar As Long
-
- If cpg = -1 Then cpg = GetACP()
- pwz = StrPtr(st)
- cwch = WideCharToMultiByte(cpg, lFlags, pwz, -1, 0&, 0&, ByVal 0&, ByVal 0&)
- stBuffer = String$(cwch + 1, vbNullChar)
- pwzBuffer = StrPtr(stBuffer)
- cwch = WideCharToMultiByte(cpg, lFlags, pwz, -1, pwzBuffer, Len(stBuffer), ByVal 0&, ByVal 0&)
- WToA = Left$(stBuffer, cwch - 1)
- End Function
-
- Private Function AToW(ByVal st As String, Optional ByVal cpg As Long = -1, Optional lFlags As Long = 0) As String
- Dim stBuffer As String
- Dim cwch As Long
- Dim pwz As Long
- Dim pwzBuffer As Long
-
- If cpg = -1 Then cpg = GetACP()
- pwz = StrPtr(st)
- cwch = MultiByteToWideChar(cpg, lFlags, pwz, -1, 0&, 0&)
- stBuffer = String$(cwch + 1, vbNullChar)
- pwzBuffer = StrPtr(stBuffer)
- cwch = MultiByteToWideChar(cpg, lFlags, pwz, -1, pwzBuffer, Len(stBuffer))
- AToW = Left$(stBuffer, cwch - 1)
- End Function
-
- Public Function EncodeUTF8(ByVal cnvUni As String) As String
- If cnvUni = vbNullString Then Exit Function
- EncodeUTF8 = StrConv(WToA(cnvUni, CP_UTF8), vbUnicode)
- End Function
-
- Public Function DecodeUTF8(ByVal cnvUni As String) As String
- If cnvUni = vbNullString Then Exit Function
- DecodeUTF8 = AToW(WToA(cnvUni, CP_ACP), CP_UTF8)
- End Function
|
: 4 : Winand
: 87 : 2795
|
Web-: winandfx.narod.ru | | #4
|
: 18.10.08 18:38
|
:
- Option Explicit
- Public Const ISUTF8_DEFAULT_READSIZE = 1024
-
- Public Function Utf8Encode(ByRef strUnicode As String, ByRef bytUtf8() As Byte) As Long
- Dim lngUnicodeLength As Long
- Dim lngUtf8Size As Long
- Dim lngCharCode As Long
- Dim i As Long
-
- lngUnicodeLength = Len(strUnicode)
- If Not CBool(lngUnicodeLength) Then Exit Function
- ReDim bytUtf8((lngUnicodeLength * 3) - 1)
-
- For i = 1 To lngUnicodeLength Step 1
- lngCharCode = CLng(AscW(Mid$(strUnicode, i, 1))) And &HFFFF&
- If lngCharCode <= &H7F& Then
- bytUtf8(lngUtf8Size) = CByte(lngCharCode)
- lngUtf8Size = lngUtf8Size + 1
- ElseIf (lngCharCode >= &H80&) And (lngCharCode <= &H7FF&) Then
-
- bytUtf8(lngUtf8Size) = CByte((lngCharCode \ &H40&) Or &HC0&)
-
- bytUtf8(lngUtf8Size + 1) = CByte((lngCharCode And &H3F&) Or &H80&)
- lngUtf8Size = lngUtf8Size + 2
- ElseIf (lngCharCode >= &H800&) And (lngCharCode <= &HFFFF&) Then
-
- bytUtf8(lngUtf8Size) = CByte((lngCharCode \ &H1000&) Or &HE0&)
-
- bytUtf8(lngUtf8Size + 1) = CByte(((lngCharCode And &HFC0&) \ &H40&) Or &H80&)
-
- bytUtf8(lngUtf8Size + 2) = CByte((lngCharCode And &H3F&) Or &H80&)
- lngUtf8Size = lngUtf8Size + 3
- End If
- Next i
-
- If lngUtf8Size Then
- ReDim Preserve bytUtf8(lngUtf8Size - 1)
- Utf8Encode = lngUtf8Size
- End If
- End Function
-
- Public Function Utf8Decode(ByRef bytUtf8() As Byte, Optional ByRef strDefaultChar As String) As String
- Dim lngUtf8Size As Long
- Dim lngDefaultCharLength As Long
- Dim strBuffer As String
- Dim lngWriteLength As Long
- Dim bytUcs2Char(1) As Byte
- Dim i As Long
-
- On Error GoTo ExitFunction
- lngUtf8Size = UBound(bytUtf8)
- On Error GoTo 0
- lngDefaultCharLength = Len(strDefaultChar)
- If lngDefaultCharLength Then
- strBuffer = String$((lngUtf8Size + 1) * lngDefaultCharLength, vbNullChar)
- Else
- strBuffer = String$(lngUtf8Size + 1, vbNullChar)
- End If
- lngWriteLength = 1
-
- Do While i <= lngUtf8Size
- If bytUtf8(i) <= &H7F Then
- Mid(strBuffer, lngWriteLength, 1) = ChrW$(bytUtf8(i))
- lngWriteLength = lngWriteLength + 1
- i = i + 1
- ElseIf (bytUtf8(i) >= &HC2 And bytUtf8(i) <= &HDF) Then
- If (i + 1) <= lngUtf8Size Then
- If (bytUtf8(i + 1) >= &H80 And bytUtf8(i + 1) <= &HBF) Then
- bytUcs2Char(0) = ((bytUtf8(i) And &H3) * &H40) Or (bytUtf8(i + 1) And &H3F)
- bytUcs2Char(1) = (bytUtf8(i) And &H1C) \ &H4
- Mid(strBuffer, lngWriteLength, 1) = bytUcs2Char
- lngWriteLength = lngWriteLength + 1
- i = i + 2
- Else
- If lngDefaultCharLength Then GoSub SetDefaultChar
- i = i + 1
- End If
- Else
- If lngDefaultCharLength Then GoSub SetDefaultChar
- i = i + 1
- End If
- ElseIf (bytUtf8(i) >= &HE0 And bytUtf8(i) <= &HEF) Then
- If (i + 2) <= lngUtf8Size Then
- If (bytUtf8(i + 1) >= &H80 And bytUtf8(i + 1) <= &HBF) And _
- (bytUtf8(i + 2) >= &H80 And bytUtf8(i + 2) <= &HBF) Then
- bytUcs2Char(0) = ((bytUtf8(i + 1) And &H3) * &H40) Or (bytUtf8(i + 2) And &H3F)
- bytUcs2Char(1) = ((bytUtf8(i) And &HF) * &H10) Or ((bytUtf8(i + 1) And &H3C) \ &H4)
- Mid(strBuffer, lngWriteLength, 1) = bytUcs2Char
- lngWriteLength = lngWriteLength + 1
- i = i + 3
- Else
- If lngDefaultCharLength Then GoSub SetDefaultChar
- i = i + 1
- End If
- Else
- If lngDefaultCharLength Then GoSub SetDefaultChar
- i = i + 1
- End If
- Else
- If lngDefaultCharLength Then GoSub SetDefaultChar
- i = i + 1
- End If
- Loop
-
- Utf8Decode = Left$(strBuffer, lngWriteLength - 1)
-
- Exit Function
- SetDefaultChar:
- Mid(strBuffer, lngWriteLength, lngDefaultCharLength) = strDefaultChar
- lngWriteLength = lngWriteLength + lngDefaultCharLength
- Return
- ExitFunction:
- End Function
-
- Public Function IsUtf8(ByRef bytArray() As Byte, Optional ByVal lngReadSize As Long = ISUTF8_DEFAULT_READSIZE) As Boolean
- Dim lngArraySize As Long
- Dim lngReadPosition As Long
- Dim lngUtf8ByteSize As Long
- Dim lngIsUtf8 As Long
- Dim i As Long
-
- If Not CBool(lngReadSize) Then GoTo ExitFunction
- On Error GoTo ExitFunction
- lngArraySize = UBound(bytArray) + 1
- On Error GoTo 0
- If lngReadSize > lngArraySize Then lngReadSize = lngArraySize
-
- Do While lngReadPosition < lngReadSize
- If bytArray(lngReadPosition) <= &H7F Then
- lngReadPosition = lngReadPosition + 1
- ElseIf bytArray(lngReadPosition) < &HC0 Then
- Exit Function
- ElseIf (bytArray(lngReadPosition) >= &HC0) And (bytArray(lngReadPosition) <= &HFD) Then
- If (bytArray(lngReadPosition) And &HFC) = &HFC Then
- lngUtf8ByteSize = 5
- ElseIf (bytArray(lngReadPosition) And &HF8) = &HF8 Then
- lngUtf8ByteSize = 4
- ElseIf (bytArray(lngReadPosition) And &HF0) = &HF0 Then
- lngUtf8ByteSize = 3
- ElseIf (bytArray(lngReadPosition) And &HE0) = &HE0 Then
- lngUtf8ByteSize = 2
- ElseIf (bytArray(lngReadPosition) And &HC0) = &HC0 Then
- lngUtf8ByteSize = 1
- End If
- If (lngReadPosition + lngUtf8ByteSize) >= lngReadSize Then Exit Do
- For i = (lngReadPosition + 1) To (lngReadPosition + lngUtf8ByteSize) Step 1
- If Not ((bytArray(i) >= &H80) And (bytArray(i) <= &HBF)) Then Exit Function
- Next i
- lngIsUtf8 = lngIsUtf8 + 1
- lngReadPosition = lngReadPosition + lngUtf8ByteSize + 1
- Else
- lngReadPosition = lngReadPosition + 1
- End If
- Loop
-
- If lngIsUtf8 > 1 Then IsUtf8 = True
- ExitFunction:
- End Function
sne:
- Private Function WinToUTF8(ByRef inString As String, _
- ByVal lMaxSize As Long) As String
-
- If inString = vbNullString Then Exit Function
-
- Dim hMemLock1 As Long, hMemLock2 As Long
- Dim iStrSize As Long
-
- hMemLock1 = GlobalAlloc(GPTR, lMaxSize)
- hMemLock2 = GlobalAlloc(GPTR, lMaxSize)
-
- iStrSize = MultiByteToWideChar(0&, 0&, inString, -1, hMemLock1, lMaxSize)
- iStrSize = WideCharToMultiByte(65001, 0&, hMemLock1, iStrSize, hMemLock2, lMaxSize, 0&, 0&)
-
- If Len(iStrSize) Then
- WinToUTF8 = String$(iStrSize - vbNull, 0&)
- Call CopyMemory(ByVal WinToUTF8, ByVal hMemLock2, iStrSize - vbNull)
- End If
-
- Call GlobalFree(hMemLock1)
- Call GlobalFree(hMemLock2)
- End Function
-
- Private Function UTF8ToWin(ByRef inString As String, _
- ByVal lMaxSize As Long) As String
-
- If inString = vbNullString Then Exit Function
-
- Dim hMemLock1 As Long, hMemLock2 As Long
- Dim iStrSize As Long
-
- hMemLock1 = GlobalAlloc(GPTR, lMaxSize)
- hMemLock2 = GlobalAlloc(GPTR, lMaxSize)
-
- iStrSize = MultiByteToWideChar(CP_UTF8, 0&, inString, -1, hMemLock1, lMaxSize)
- iStrSize = WideCharToMultiByte(0&, 0&, hMemLock1, -1, hMemLock2, iStrSize, 0&, 0&)
-
- If Len(iStrSize) Then
- UTF8ToWin = String$(iStrSize - vbNull, 0&)
- Call CopyMemory(ByVal UTF8ToWin, ByVal hMemLock2, iStrSize - vbNull)
- End If
-
- Call GlobalFree(hMemLock1)
- Call GlobalFree(hMemLock2)
- End Function
|
: 1 |