2008/02/06

Recent entries from same category

  1. 書評「プログラミング言語大全」
  2. Google Developers Expert (Go) になりました。
  3. 1つの横着から隠しファイルが生まれた瞬間
  4. 書評:[試して理解]Linuxのしくみ
  5. GoCon に初参加してきた。

たぶんこんなんでいけるはず。
Control-C - ロックスターになりたい
あと日本語文字化けする。VBからSJISで送ってサーバでutf8で読んでるとかだろうか。
パッチ
diff -u winsource.orig/modUrlEncode.bas winsource/modUrlEncode.bas
--- winsource.orig/modUrlEncode.bas Tue Jan 08 02:14:04 2008
+++ winsource/modUrlEncode.bas  Wed Feb 06 18:12:35 2008
@@ -1,6 +1,57 @@
 Attribute VB_Name = "modUrlEncode"
 Option Explicit
 
+Private Declare Function WideCharToMultiByte Lib "kernel32" ( _
+    ByVal CodePage As Long, _
+    ByVal dwFlags As Long, _
+    ByVal lpWideCharStr As Long, _
+    ByVal cchWideChar As Long, _
+    ByRef lpMultiByteStr As Any, _
+    ByVal cchMultiByte As Long, _
+    ByVal lpDefaultChar As String, _
+    ByVal lpUsedDefaultChar As Long) As Long
+    
+Private Const CP_UTF8 = 65001
+
+Private Function EncodeUTF8(ByRef strUni As String) As Byte()
+    On Error GoTo ErrHandler
+
+    Dim lngUniLen As Long
+    Dim lngBufLen As Long
+    Dim lngRtn As Long
+    Dim bytOut() As Byte
+
+    lngUniLen = Len(strUni)
+    If lngUniLen = 0 Then
+        Exit Function
+    End If
+    lngBufLen = lngUniLen * 5
+    ReDim bytOut(lngBufLen - 1)
+    lngRtn = WideCharToMultiByte( _
+        CP_UTF8, _
+        0, _
+        StrPtr(strUni), _
+        lngUniLen, _
+        bytOut(0), _
+        lngBufLen, _
+        vbNullString, _
+        0)
+    If lngRtn Then
+        ReDim Preserve bytOut(lngRtn - 1)
+        EncodeUTF8 = bytOut
+    End If
+    
+    Exit Function
+ErrHandler:
+End Function
+
+Private Function SafeUBound(ByRef arr() As Byte) As Long
+    On Error Resume Next
+    Dim lngLen As Long
+    lngLen = UBound(arr) - LBound(arr) + 1
+    SafeUBound = lngLen
+End Function
+
 Public Function UrlEncode(ByRef strSource As String) As String
 
  Dim lngLength As Long                                          '???????(S-JIS ???)?????
@@ -11,10 +62,13 @@
  Dim lngReadCount As Long                                       'bytSource ??????????
  Dim lngWriteCount As Long                                      'strBuffer ??????????
  
-    lngLength = LenB(StrConv(strSource, vbFromUnicode))         'ANSI/S-JIS ???????????
-    If Not CBool(lngLength) Then Exit Function                  '0 ????????????
-    ReDim bytSource(lngLength - 1)                              'ANSI/S-JIS ???????????????
-    bytSource = StrConv(strSource, vbFromUnicode)               'ANSI/S-JIS ???? bytSource ???
+    'lngLength = LenB(StrConv(strSource, vbFromUnicode))         'ANSI/S-JIS ???????????
+    'If Not CBool(lngLength) Then Exit Function                  '0 ????????????
+    'ReDim bytSource(lngLength - 1)                              'ANSI/S-JIS ???????????????
+    'bytSource = StrConv(strSource, vbFromUnicode)               'ANSI/S-JIS ???? bytSource ???
+    bytSource = EncodeUTF8(strSource)
+    lngLength = SafeUBound(bytSource)
+    If Not CBool(lngLength) Then Exit Function
    
     strBuffer = String$(lngLength * 3, vbNullChar)              'URL ???????????????????
     strSingleHex = "%00"                                        '16 ?????????????????????

あ、Control-Cのアカウント持ってなかった
Posted at by | Edit