ASP 如何實作 VB 的 StrConv 函數來進行 Unicode 轉換 ( vbFromUnicode / vbUnicode )
VB6 的StrConv 函數說明 :
傳回一特定轉換後的 Variant (String)。
語法
StrConv(string, conversion, LCID)
StrConv 函數的語法有以下的指名引數:
單元 說明
string 必要引數。為欲轉換的字串運算式。
conversion 必要引數:為Integer。其值的和決定轉換的型態。
LCID 選項的。如果與系統LocaleID不同,則為LocaleID(系統LocaleID為缺省值。)
vbUnicode (64) 據系統的預設字元碼對應頁將字串轉成 Unicode。
vbFromUnicode (128) 將字串由 Unicode 轉成系統的預設字元碼對應頁。
ASP 程式碼如下 :
<%
Response.Write ConvUnicode(FromUnicode("強力Power鎯頭Hammer"))
' FromUnicode
Function FromUnicode(strData)
Dim objStm
Set objStm = Server.CreateObject("ADODB.Stream")
objStm.Charset = "Big5"
objStm.Type = 2
objStm.Open
objStm.WriteText strData
objStm.Position = 0
objStm.Charset = "Unicode"
objStm.Type = 1
FromUnicode = MidB(objStm.Read, 1)
End Function
' Unicode
Function ConvUnicode(ByVal strData)
Dim rs, stm, bytAry, intLen
If Len(strData & "") > 0 Then
strData = MidB(strData, 1)
intLen = LenB(strData)
Set rs = Server.CreateObject("ADODB.Recordset")
Set stm = Server.CreateObject("ADODB.Stream")
rs.Fields.Append "X", 205, intLen
rs.Open
rs.AddNew
rs(0).AppendChunk strData & ChrB(0)
rs.Update
bytAry = rs(0).GetChunk(intLen)
stm.Type = 1
stm.Open
stm.Write bytAry
stm.Position = 0
stm.Type = 2
stm.Charset = "Big5"
ConvUnicode = stm.ReadText
End If
On Error Resume Next
stm.Close
Set stm = Nothing
rs.Close
Set rs = Nothing
End Function
%>
'Response.Write ConvUnicode(FromUnicode("強力Power鎯頭Hammer"))
Response.Write ConvUnicode(FromUnicode("我的电脑"))
' FromUnicode
Function FromUnicode(strData)
Dim objStm
Set objStm = Server.CreateObject("ADODB.Stream")
'objStm.Charset = "Unicode"
objStm.Charset = "gb2312"
objStm.Type = 2
objStm.Open
objStm.WriteText strData
objStm.Position = 0
objStm.Charset = "gb2312"
objStm.Type = 1
FromUnicode = MidB(objStm.Read, 1)
End Function
' Unicode
Function ConvUnicode(ByVal strData)
Dim rs, stm, bytAry, intLen
If Len(strData & "") > 0 Then
strData = MidB(strData, 1)
intLen = LenB(strData)
Set rs = Server.CreateObject("ADODB.Recordset")
Set stm = Server.CreateObject("ADODB.Stream")
rs.Fields.Append "X", 205, intLen
rs.Open
rs.AddNew
rs(0).AppendChunk strData & ChrB(0)
rs.Update
bytAry = rs(0).GetChunk(intLen)
stm.Type = 1
stm.Open
stm.Write bytAry
stm.Position = 0
stm.Type = 2
stm.Charset = "gb2312"
ConvUnicode = stm.ReadText
End If
On Error Resume Next
stm.Close
Set stm = Nothing
rs.Close
Set rs = Nothing
End Function
%>