返回列表 回复 发帖

[转帖]UTF-8 -> GB2312 转换

  1. <HTML>
  2. <HEAD>
  3. <META name=VI60_defaultClientScript content=VBScript>
  4. <META NAME="GENERATOR" Content="Microsoft Visual Studio
  5. 6.0">
  6. <TITLE></TITLE>
  7. <meta http-equiv="Content-Type" content="text/html;
  8. charset=gb2312"></HEAD>
  9. <BODY><pre>
  10. 输入:"http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-
  11. 8&q=%E5%85%B3%E9%94%AE%E5%AD%97&btnG=Google+Search"
  12. 输出:关键字</pre>
  13. <SCRIPT LANGUAGE=vbscript>
  14. <!--
  15. mystr="http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=%E5%85%B3%E9%94%AE%E5%AD%97&btnG=Google+Search";
  16. function getutf8(x)
  17. ';这个函数是用来得到%号的部分,
  18. ';输入条件是
  19. ';""http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=%E5%85%B3%E9%94%AE%E5%AD%97&btnG=Google+Search";
  20. dim first,last
  21. A=split(x,"&")';定义一个临时数组
  22. dim i:i=0';临时的指针
  23. for i=0 to ubound(A)
  24. if instr(A(i),"%")>0 then
  25. first=instr(A(i),"%")
  26. last=InStrRev(A(i),"%")
  27. getutf8=getutf8 & mid(A(i),first,last-first+3)
  28. end if
  29. next
  30. getutf8=right(getutf8,len(getutf8)-1)';去掉左边的%
  31. ';msgbox getutf8
  32. end function
  33. msgbox U8toU(getutf8(mystr))
  34. function c16to2(x)
  35. ';这个函数是用来转换16进制到2进制的,可以是任何长度的,一般转换UTF-8的时候是两个长度,比如A9
  36. ';比如:输入“C2”,转化成“11000010”,其中1100是"c"是10进制的12(1100),那么2(10)不足4位要补齐成(0010)。
  37. dim tempstr
  38. dim i:i=0';临时的指针
  39. for i=1 to len(trim(x))
  40. tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
  41. do while len(tempstr)<4
  42. tempstr="0" & tempstr';如果不足4位那么补齐4位数
  43. loop
  44. c16to2=c16to2 & tempstr
  45. next
  46. end function
  47. ';document.write hex(asc("字")) & "<br/>"
  48. function U8toU(x)
  49. ';输入一堆有%分隔的字符串,先分成数组,根据utf8规则来判断补齐规则
  50. ';输入:关 E5 85 B3  键  E9 94 AE字   E5 AD 97
  51. ';输出:关 B9D8  键  BCFC 字   D7D6
  52. dim WeiS';要判断第一个编码的位数
  53. dim Unicode';二进制的Unicode码
  54. dim alpha';定义单个字符
  55. A=split(x,"%")';定义一个临时数组
  56. dim i:i=0';临时的指针
  57. dim j:j=0';临时的指针
  58. for i=0 to ubound(A)
  59. A(i)=c16to2(A(i))';第一次循环,先转换成2进制再说
  60. next
  61. for i=0 to ubound(A)-1
  62. WeiS=instr(A(i),"0")';判断第一次出现0的位置,
  63. ';可能是1(单字节),3(3-1字节),4,5,6,7不可能是2和大于7
  64. ';理论上到7,实际不会超过3。
  65. Unicode=""
  66. for j=1 to WeiS-1
  67. if j=1 then
  68. A(i)=right(A(i),len(A(i))-WeiS)';第一个去掉最左边的WeiS个
  69. Unicode=Unicode & A(i)
  70. else
  71. i=i+1
  72. A(i)=right(A(i),len(A(i))-2)';其余去掉最左边的两个
  73. Unicode=Unicode & A(i)
  74. end if
  75. next
  76. if len(c2to16(Unicode)) =4 then
  77. U8toU=U8toU & chrw(int("&H" & c2to16(Unicode)))';总算完了,妈的!!
  78. else
  79. U8toU=U8toU & chr(int("&H" & c2to16(Unicode)))';总算完了,妈的!!
  80. end if
  81. next
  82. end function
  83. ';msgbox c2to16("11100101")
  84. function c2to16(x)
  85. ';2进制到16进制的转换,每4个0或1转换成一个16进制字母,输入长度当然不可能不是4的倍数了
  86. dim i:i=1';临时的指针
  87. for i=1 to len(x)  step 4
  88. c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
  89. next
  90. end function
  91. function c2to10(x)
  92. ';单纯的2进制到10进制的转换,不考虑转16进制所需要的4位前零补齐。
  93. ';因为这个函数很有用!以后也会用到,做过通讯和硬件的人应该知道。
  94. ';这里用字符串代表二进制
  95. c2to10=0
  96. if x="0" then exit function      ';如果是0的话直接得0就完事
  97. dim i : i=0                  ';临时的指针
  98. for i= 0 to len(x)-1            ';否则利用8421码计算,这个从我最开始学计算机的时候就会,好怀念当初教我们的谢道建老先生啊!
  99. if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
  100. next
  101. end function
  102. function c10to2(x)';10进制到2进制的转换
  103. ';这个函数在计算16位到2位转换时候用到了,
  104. ';没有做在16位里面是因为这个函数只是单纯10-2转换,不涉及16进制由4个2进制补齐空位,将来可以用到任何地方
  105. ';比如输入2,输出“10”而不是“0010”
  106. ';首先判断正负符号
  107. dim mysign:mysign=sgn(x)';定义一个符号标记
  108. x=abs(x)
  109. ';然后判断有几位,至少一位
  110. dim WeiS:WeiS=1
  111. do
  112. if x<2^WeiS then
  113. exit do
  114. else
  115. WeiS=WeiS+1
  116. end if
  117. loop
  118. dim tempnum:tempnum=x';定义一个临时的数字
  119. dim i:i=0';临时的指针
  120. for i= WeiS to 1 step-1
  121. if tempnum>=2^(i-1) then
  122. tempnum=tempnum-2^(i-1)
  123. c10to2=c10to2 & "1"   
  124. else
  125. c10to2=c10to2 & "0"
  126. end if
  127. next
  128. if mysign=-1 then c10to2="-" & c10to2';加上正负符号
  129. end function
  130. -->
  131. </SCRIPT>
  132. </BODY>
  133. </HTML>
复制代码

                     我是一个呼吸着现在的空气而生活在过去的人
               这样的注定孤独,孤独的身处闹市却犹如置身于荒漠
                                     我已习惯了孤独,爱上孤独
                                 他让我看清了自我,还原了自我
                             让我再静静的沉思中得到快乐和满足
                                   再孤独的世界里我一遍又一遍
                                   不厌其烦的改写着自己的过去
                                             延伸到现在与未来
                                       然而那只是泡沫般的美梦
                                 产生的时刻又伴随着破灭的到来
                         在灰飞烟灭的瞬间我看到的是过程的美丽
                                      而不是结果的悲哀。。。
返回列表