ASP binary and String Conversion, alternative perfect solution, ADODB. Stream implementation by shawl. Qiu

Source: Internet
Author: User

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

  1. <%
  2. Private function btsascii (BIN)
  3. 'Convert binary to string (BMP | GIF | PNG | JPG)
  4. Dim I, ibyt, sbyt, blen: blen = lenb (BIN)
  5. For I = 1 to blen
  6. Sbyt = midb (bin, I, 1): ibyt = ASCB (sbyt)
  7. If ibyt <128 then
  8. Btsascii = btsascii & CHR (ibyt)
  9. Else: I = I + 1
  10. If I <= blen then btsascii = btsascii & CHR (ASCW (sbyt & sbyt ))
  11. End if
  12. Next 'shawl. Qiu Code'
  13. End Function
  14. %>

2. gb2312 Character Set binary transfer string Function

    Linenum

  1. <%
  2. Private function btsgb2312 (BIN)
  3. 'Convert binary to string | gb2312 Encoding
  4. Dim I, ibyt, sbyt, blen: blen = lenb (BIN)
  5. For I = 1 to blen
  6. Sbyt = midb (bin, I, 1): ibyt = ASCB (sbyt)
  7. If ibyt <128 then
  8. Btsgb2312 = btsgb2312 & CHR (ibyt)
  9. Else
  10. Btsgb2312 = btsgb2312 & CHR (ASCW (midb (bin, I + 1, 1) & sbyt ))
  11. I = I + 1
  12. End if
  13. Next 'shawl. Qiu Code'
  14. End Function
  15. %>

3. ADODB. Stream string to binary stream function and binary stream string function and demonstration operation

    Linenum

  1. <%
  2. Dim STR: Str = "ADODB. Stream achieves mutual conversion between binary and string by shawl. Qiu"
  3. Response. Write "string to binary response. binarywrite STB (STR," "UTF-8" "): <br/>"
  4. Response. binarywrite STB (STR, "UTF-8 ")
  5. Response. write "<p/> binary to string response. write BTS (midb (STB (STR, "" UTF-8 ""), 1), "" UTF-8 "") <br/>"
  6. Response. Write BTS (midb (STB (STR, "UTF-8"), 1), "UTF-8 ")
  7. Function STB (STR, charset)
  8. '''''''''''''''''''''''''''''
  9. 'String to binary function by shawl. Qiu
  10. 'Http://blog.csdn.net/btbtd
  11. ''''''''''''''''
  12. 'Parameter description
  13. ''''''''''
  14. 'Str: string to be converted to binary
  15. 'Charset: the default Character Set of the string. If this parameter is not specified, the default value is gb2312.
  16. ''''''''''
  17. 'Sample call: Response. binarywrite STB (STR, "UTF-8 ")
  18. '''''''''''''''''''''''''''''
  19. Dim STM _
  20. Set STM _ = Createobject ("ADODB. Stream ")
  21. With STM _
  22. . Type = 2
  23. If charset <> "" then
  24. . Charset = charset
  25. Else
  26. . Charset = "gb2312"
  27. End if
  28. . Open
  29. . Writetext Str
  30. . Position = 0
  31. . Type = 1
  32. STB =. Read
  33. . Close
  34. End with 'shawl. Qiu Code'
  35. Set STM _ = nothing
  36. End Function
  37. Function BTS (STR, charset)
  38. '''''''''''''''''''''''''''''
  39. 'Binary to string function by shawl. Qiu
  40. 'Http://blog.csdn.net/btbtd
  41. ''''''''''''''''
  42. 'Parameter description
  43. ''''''''''
  44. 'Str: binary data to be converted to a string
  45. 'Charset: the default Character Set of the string. If this parameter is not specified, the default value is gb2312.
  46. ''''''''''
  47. 'Sample call: Response. Write BTS (midb (STB (STR, "UTF-8"), 1), "UTF-8 ")
  48. '''''''''''''''''''''''''''''
  49. 'Note: the binary string must be read with midb (binarystring, 1) (custom read length ).
  50. '''''''''''''''''''''''''''''
  51. Dim STM _
  52. Set STM _ = Createobject ("ADODB. Stream ")
  53. With STM _
  54. . Type = 2
  55. . Open
  56. . Writetext Str
  57. . Position = 0
  58. If charset <> "" then
  59. . Charset = charset
  60. Else
  61. . Charset = "gb2312"
  62. End if
  63. BTS =. readtext
  64. . Close
  65. End with 'shawl. Qiu Code'
  66. Set STM _ = nothing
  67. End Function
  68. %>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.