VB, VBS, ASP, VBA ADODB 활용Stream GB2312 및 UTF8 인코딩 변환

앞서 말했듯이, ADODB를 사용하고 싶다.Stream이 GB2312와 UTF8 인코딩 변환을 실현하였으나 찾지 못했습니다. 을 찾아보았습니다. VB와 VBA에서 모두 문제가 없지만 ASP와 VBS 아래에서 문제가 되지 않았습니다. 그 이유는 제가 계속 고민하고 있는 adoStream입니다.Write bytes 실패!
그래서 VBS에서 Byte 그룹을 정의하는 방법을 찾아봤어요(https://www.jb51.net/article/33247.htm), 위의 코드를 개조하여 드디어 VBS와 ASP에서 사용할 수 있는 ADODB를 실현하였다.Stream GB2312 및 UTF8 인코딩 변환:
ps:GB2312에서 UTF-8로 전환하면 하나의Variant 수조를 얻을 수 있다.Base64나 MD5 인코딩에 사용할 때 UTF-8의 표지 헤더'&HEFBBBF'가 필요하지 않기 때문에 출력된 수조 개조는 헤더 3자 바이트 &HEFBBBBF를 삭제했고 원문 코드에서 얻은byte 수조는base64와 MD5 인코딩에 직접 사용할 수 없다. 즉:ustr(0) 오류 보고,ascb(midb(ustr, i+1,1)를 사용해야 직접 조작할 수 있는 그룹을 얻을 수 있습니다. 참조: e(19).UTF-8 GB2312에서 얻은 것은 유니버설 문자열입니다
'- ------------------------------------------- -
'      :GB2312   UTF8         
'- ------------------------------------------- -
Public Function GB2312ToUTF8(strIn)
    Dim adoStream, ustr, i, outarr()
    Set adoStream = CreateObject("ADODB.Stream")
    adoStream.Charset = "utf-8"
    adoStream.Type = 2 'adTypeText
    adoStream.Open
    adoStream.WriteText strIn
    adoStream.Position = 0
    adoStream.Type = 1 'adTypeBinary
    'msgbox adoStream.size    
    ustr = adoStream.Read()
    'redim Preserve ustr(adoStream.size-1)
    adoStream.Close
    'WScript.Echo VarType(ustr), TypeName(ustr),"ustr"
    'msgbox ascb(midb(ustr,3,1))
    ReDim outarr(UBound(ustr) - 3)
    For i = 3 To UBound(ustr)
        outarr(i - 3) = ascb(midb(ustr,i+1,1))
    Next
    GB2312ToUTF8 = outarr
    'WScript.Echo VarType(outarr), TypeName(outarr)
    set adoStream=nothing
End Function

public function Varr2hexstr(a)  '-------   Variant           
	dim i,S
    For i = 0 To UBound(a)
        S=S & Right("00" & Hex(a(i)), 2)
    Next
    Varr2hexstr=S
End Function

public function HexStr2ByteArr(S) '-------           Bytes   ( ,   ADODB.Stream.Write)
	Dim xmldoc, node, bytes
	Set xmldoc = CreateObject("Msxml2.DOMDocument") 
	Set node = xmldoc.CreateElement("binary") 
	node.DataType = "bin.hex" 
	'demon.tw         
	'64 65 6D 6F 6E 2E 74 77
	'node.Text = "64656D6F6E2E7477"
	node.Text = S
	bytes = node.NodeTypedValue 
	'WScript.Echo VarType(bytes), TypeName(bytes),"bytes"
    set xmldoc=nothing
    set node=nothing	
	HexStr2ByteArr=bytes
End Function

'- ------------------------------------------- -
'      :UTF8   GB2312 As String
'- ------------------------------------------- -
Public Function UTF8ToGB2312(varIn)
    Dim adoStream,bytes,S
	
	S="EFBBBF" & Varr2hexstr(varIn)
	bytes=HexStr2ByteArr(s)
        
    Set adoStream = CreateObject("ADODB.Stream")
    adoStream.Charset = "utf-8"
    adoStream.Type = 1 'adTypeBinary
    adoStream.Open
    adoStream.Write bytes
    adoStream.Position = 0
    adoStream.Type = 2 'adTypeText
    UTF8ToGB2312 = adoStream.ReadText()
    adoStream.Close
    
    set adoStream=nothing

End Function

Msxml2. 사용DOMDocument은 VBS에서 Byte 그룹을 만들고 그 결과는 ADODB에 사용할 수 있습니다.Stream.Write .
사용 예는 다음과 같습니다.
    Dim a,d,e
    a = "123  ,?αabc"
    e = GB2312ToUTF8(a)
	msgbox e(19)
    d = UTF8ToGB2312(e)    
    msgbox d

주의: chrB, ascB, CByte는 VBS와 ASP에서 8209 Bytes () 의 진짜 바이트 그룹을 얻을 수 없습니다.
자세한 내용은 다음을 참조하십시오.
Dim x(9), i 
For i = 0 To 9 
x(i) = ascb(chrb(65+i)) 
Next
WScript.Echo VarType(x), TypeName(x),"x"

여기!

좋은 웹페이지 즐겨찾기