ASP binary and String Conversion, alternative perfect solution, ADODB. Stream implementation by shawl. Qiu
Abstract:
This document describes how to use the ASP built-in component ADODB. Stream to convert binary to string and binary to any character set encoding.
Note:
To implement the binary transfer string operation, it can be said that it is easy to convert the binary transfer string of the US-ASCII character set, where such standard information is everywhere.
A bit difficult is the binary transfer string of the gb2312 character set. Each Chinese Character Set of the gb2312 Character Set occupies two bytes, which is easy to calculate because of relevant information.
What is difficult is the binary transfer string of the Unicode Character Set. Each Chinese Character Set occupies three bytes of space. I rummaged through the relevant authoritative websites, no site has an introduction to the Unicode Character Set and binary stream, so it is difficult. if there are related standard documents, it is easy to do anything, as long as the sprout article needs to pass through.
Finally, after nearly ten hours of struggle, I finally found that using the ASP ADODB. Stream built-in component can easily handle the conversion of binary streams and strings of any encoding .:)
Directory:
1. ASCII character set binary transfer string Function
2. gb2312 Character Set binary transfer string Function
3. ADODB. Stream string to binary stream function and binary stream string function and demonstration operation
Shawl. Qiu
2006-09-26
Http://blog.csdn.net/btbtd
1. ASCII character set binary transfer string Function
Linenum
- <%
- Private function btsascii (BIN)
- 'Convert binary to string (BMP | GIF | PNG | JPG)
- Dim I, ibyt, sbyt, blen: blen = lenb (BIN)
- For I = 1 to blen
- Sbyt = midb (bin, I, 1): ibyt = ASCB (sbyt)
- If ibyt <128 then
- Btsascii = btsascii & CHR (ibyt)
- Else: I = I + 1
- If I <= blen then btsascii = btsascii & CHR (ASCW (sbyt & sbyt ))
- End if
- Next 'shawl. Qiu Code'
- End Function
- %>
2. gb2312 Character Set binary transfer string Function
Linenum
- <%
- Private function btsgb2312 (BIN)
- 'Convert binary to string | gb2312 Encoding
- Dim I, ibyt, sbyt, blen: blen = lenb (BIN)
- For I = 1 to blen
- Sbyt = midb (bin, I, 1): ibyt = ASCB (sbyt)
- If ibyt <128 then
- Btsgb2312 = btsgb2312 & CHR (ibyt)
- Else
- Btsgb2312 = btsgb2312 & CHR (ASCW (midb (bin, I + 1, 1) & sbyt ))
- I = I + 1
- End if
- Next 'shawl. Qiu Code'
- End Function
- %>
3. ADODB. Stream string to binary stream function and binary stream string function and demonstration operation
Linenum
- <%
- Dim STR: Str = "ADODB. Stream achieves mutual conversion between binary and string by shawl. Qiu"
- Response. Write "string to binary response. binarywrite STB (STR," "UTF-8" "): <br/>"
- Response. binarywrite STB (STR, "UTF-8 ")
- Response. write "<p/> binary to string response. write BTS (midb (STB (STR, "" UTF-8 ""), 1), "" UTF-8 "") <br/>"
- Response. Write BTS (midb (STB (STR, "UTF-8"), 1), "UTF-8 ")
- Function STB (STR, charset)
- '''''''''''''''''''''''''''''
- 'String to binary function by shawl. Qiu
- 'Http://blog.csdn.net/btbtd
- ''''''''''''''''
- 'Parameter description
- ''''''''''
- 'Str: string to be converted to binary
- 'Charset: the default Character Set of the string. If this parameter is not specified, the default value is gb2312.
- ''''''''''
- 'Sample call: Response. binarywrite STB (STR, "UTF-8 ")
- '''''''''''''''''''''''''''''
- Dim STM _
- Set STM _ = Createobject ("ADODB. Stream ")
- With STM _
- . Type = 2
- If charset <> "" then
- . Charset = charset
- Else
- . Charset = "gb2312"
- End if
- . Open
- . Writetext Str
- . Position = 0
- . Type = 1
- STB =. Read
- . Close
- End with 'shawl. Qiu Code'
- Set STM _ = nothing
- End Function
- Function BTS (STR, charset)
- '''''''''''''''''''''''''''''
- 'Binary to string function by shawl. Qiu
- 'Http://blog.csdn.net/btbtd
- ''''''''''''''''
- 'Parameter description
- ''''''''''
- 'Str: binary data to be converted to a string
- 'Charset: the default Character Set of the string. If this parameter is not specified, the default value is gb2312.
- ''''''''''
- 'Sample call: Response. Write BTS (midb (STB (STR, "UTF-8"), 1), "UTF-8 ")
- '''''''''''''''''''''''''''''
- 'Note: the binary string must be read with midb (binarystring, 1) (custom read length ).
- '''''''''''''''''''''''''''''
- Dim STM _
- Set STM _ = Createobject ("ADODB. Stream ")
- With STM _
- . Type = 2
- . Open
- . Writetext Str
- . Position = 0
- If charset <> "" then
- . Charset = charset
- Else
- . Charset = "gb2312"
- End if
- BTS =. readtext
- . Close
- End with 'shawl. Qiu Code'
- Set STM _ = nothing
- End Function
- %>