网络编程
位置:首页>> 网络编程>> Asp编程>> asp中字符编码转换的10个函数[荐]

asp中字符编码转换的10个函数[荐]

 来源:asp之家 发布时间:2007-11-11 10:32:00 

标签:编码,字符,utf8,转换

 这10个asp处理网页编码转换的函数,不知何时收藏在我的电脑中,今天刚好看到了,拿出来与大家分享,这里各种常见的网页编码问题已经都有了,让我们远离乱码的困扰!
 

 1、UTF转GB---将UTF8编码文字转换为GB编码文字


function UTF2GB(UTFStr) 
for Dig=1 to len(UTFStr) 
  '如果UTF8编码文字以%开头则进行转换
  if mid(UTFStr,Dig,1)="%" then 
     'UTF8编码文字大于8则转换为汉字
    if len(UTFStr) >= Dig+8 then 
       GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9)) 
       Dig=Dig+8 
    else 
      GBStr=GBStr & mid(UTFStr,Dig,1) 
    end if 
  else 
     GBStr=GBStr & mid(UTFStr,Dig,1) 
  end if 
next 
UTF2GB=GBStr 
end function 


2.UTF8编码文字将转换为汉字

function ConvChinese(x) 
   A=split(mid(x,2),"%") 
   i=0 
   j=0 
  for i=0 to ubound(A) 
     A(i)=c16to2(A(i)) 
  next 
  for i=0 to ubound(A)-1 
    DigS=instr(A(i),"0") 
    Unicode="" 
    for j=1 to DigS-1 
      if j=1 then 
        A(i)=right(A(i),len(A(i))-DigS) 
        Unicode=Unicode & A(i) 
      else 
         i=i+1 
         A(i)=right(A(i),len(A(i))-2) 
         Unicode=Unicode & A(i) 
      end if 
    next 
    if len(c2to16(Unicode))=4 then 
       ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode))) 
    else 
       ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode))) 
    end if 
  next 
end function 


3.二进制代码转换为十六进制代码

function c2to16(x)
   i=1 
   for i=1 to len(x) step 4 
      c2to16=c2to16 & hex(c2to10(mid(x,i,4))) 
   next 
end function 


4.二进制代码转换为十进制代码

function c2to10(x)
   c2to10=0 
   if x="0" then exit function 
     i=0 
   for i= 0 to len(x) -1 
      if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i) 
   next 
end function 


5.十六进制代码转换为二进制代码


function c16to2(x) 
    i=0 
    for i=1 to len(trim(x)) 
      tempstr= c10to2(cint(int("&h" & mid(x,i,1)))) 
      do while len(tempstr)<4 
         tempstr="0" & tempstr 
      loop 
      c16to2=c16to2 & tempstr 
   next 
end function 


0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com