VB6的UTF8编码解码
'UTF-8编码
Public
Function
UTF8Encode(
ByVal
szInput
As
String
)
As
String
Dim
wch
As
String
Dim
uch
As
String
Dim
szRet
As
String
Dim
x
As
Long
Dim
inputLen
As
Long
Dim
nAsc
As
Long
Dim
nAsc2
As
Long
Dim
nAsc3
As
Long
If
szInput =
""
Then
UTF8Encode = szInput
Exit
Function
End
If
inputLen = Len(szInput)
For
x = 1
To
inputLen
'得到每个字符
wch = Mid(szInput, x, 1)
'得到相应的UNICODE编码
nAsc = AscW(wch)
'对于<0的编码 其需要加上65536
If
nAsc < 0
Then
nAsc = nAsc + 65536
'对于<128位的ASCII的编码则无需更改
If
(nAsc
And
&HFF80) = 0
Then
szRet = szRet & wch
Else
If
(nAsc
And
&HF000) = 0
Then
'真正的第二层编码范围为000080 - 0007FF
'Unicode在范围D800-DFFF中不存在任何字符,基本多文种平面中约定了这个范围用于UTF-16扩展标识辅助平面(两个UTF-16表示一个辅助平面字符).
'当然,任何编码都是可以被转换到这个范围,但在unicode中他们并不代表任何合法的值。
uch =
"%"
& Hex(((nAsc \ 2 ^ 6))
Or
&HC0) & Hex(nAsc
And
&H3F
Or
&H80)
szRet = szRet & uch
Else
'第三层编码00000800 – 0000FFFF
'首先取其前四位与11100000进行或去处得到UTF-8编码的前8位
'其次取其前10位与111111进行并运算,这样就能得到其前10中最后6位的真正的编码 再与10000000进行或运算来得到UTF-8编码中间的8位
'最后将其与111111进行并运算,这样就能得到其最后6位的真正的编码 再与10000000进行或运算来得到UTF-8编码最后8位编码
uch =
"%"
& Hex((nAsc \ 2 ^ 12)
Or
&HE0) &
"%"
& _
Hex((nAsc \ 2 ^ 6)
And
&H3F
Or
&H80) &
"%"
& _
Hex(nAsc
And
&H3F
Or
&H80)
szRet = szRet & uch
End
If
End
If
Next
UTF8Encode = szRet
End
Function
'UTF-8解码(2-25更改,采用递归方法,可以对一串字符串解码,仅仅为演示此算法,请不要随意调用)
'形式类如department=%E4%B9%B3%E8%85%BA'%E5%A4%96%E7%A7%91
Public
Function
UTF8BadDecode(
ByVal
code
As
String
)
As
String
If
code =
""
Then
Exit
Function
End
If
Dim
tmp
As
String
Dim
decodeStr
As
String
Dim
codelen
As
Long
Dim
result
As
String
Dim
leftStr
As
String
leftStr = Left(code, 1)
If
leftStr =
""
Then
UTF8BadDecode =
""
Exit
Function
ElseIf
leftStr <>
"%"
Then
UTF8BadDecode = leftStr + UTF8BadDecode(Right(code, Len(code) - 1))
ElseIf
leftStr =
"%"
Then
codelen = Len(code)
If
(Mid(code, 2, 1) =
"C"
Or
Mid(code, 2, 1) =
"B"
)
Then
decodeStr = Replace(Mid(code, 1, 6),
"%"
,
""
)
tmp = c10ton(Val(
"&H"
& Hex(Val(
"&H"
& decodeStr)
And
&H1F3F)))
tmp =
String
(16 - Len(tmp),
"0"
) & tmp
UTF8BadDecode = UTF8BadDecode & ChrW(Val(
"&H"
& c2to16(Mid(tmp, 3, 4)) & c2to16(Mid(tmp, 7, 2) & Mid(tmp, 11, 2)) & Right(decodeStr, 1))) & UTF8BadDecode(Right(code, codelen - 6))
ElseIf
(Mid(code, 2, 1) =
"E"
)
Then
decodeStr = Replace(Mid(code, 1, 9),
"%"
,
""
)
tmp = c10ton((Val(
"&H"
& Mid(Hex(Val(
"&H"
& decodeStr)
And
&HF3F3F), 2, 3))))
tmp =
String
(10 - Len(tmp),
"0"
) & tmp
UTF8BadDecode = ChrW(Val(
"&H"
& (Mid(decodeStr, 2, 1) & c2to16(Mid(tmp, 1, 4)) & c2to16(Mid(tmp, 5, 2) & Right(tmp, 2)) & Right(decodeStr, 1)))) & UTF8BadDecode(Right(code, codelen - 9))
Else
UTF8BadDecode = Chr(Val(
"&H"
& (Mid(code, 2, 2)))) & UTF8BadDecode(Right(code, codelen - 3))
End
If
End
If
End
Function
'UTF-8解码(3-12更改,可以解多个字符串 可供正常使用)
Public
Function
UTF8Decode(
ByVal
code
As
String
)
As
String
If
code =
""
Then
UTF8Decode =
""
Exit
Function
End
If
Dim
tmp
As
String
Dim
decodeStr
As
String
Dim
codelen
As
Long
Dim
result
As
String
Dim
leftStr
As
String
leftStr = Left(code, 1)
While
(code <>
""
)
codelen = Len(code)
leftStr = Left(code, 1)
If
leftStr =
"%"
Then
If
(Mid(code, 2, 1) =
"C"
Or
Mid(code, 2, 1) =
"B"
)
Then
decodeStr = Replace(Mid(code, 1, 6),
"%"
,
""
)
tmp = c10ton(Val(
"&H"
& Hex(Val(
"&H"
& decodeStr)
And
&H1F3F)))
tmp =
String
(16 - Len(tmp),
"0"
) & tmp
UTF8Decode = UTF8Decode & UTF8Decode & ChrW(Val(
"&H"
& c2to16(Mid(tmp, 3, 4)) & c2to16(Mid(tmp, 7, 2) & Mid(tmp, 11, 2)) & Right(decodeStr, 1)))
code = Right(code, codelen - 6)
ElseIf
(Mid(code, 2, 1) =
"E"
)
Then
decodeStr = Replace(Mid(code, 1, 9),
"%"
,
""
)
tmp = c10ton((Val(
"&H"
& Mid(Hex(Val(
"&H"
& decodeStr)
And
&HF3F3F), 2, 3))))
tmp =
String
(10 - Len(tmp),
"0"
) & tmp
UTF8Decode = UTF8Decode & ChrW(Val(
"&H"
& (Mid(decodeStr, 2, 1) & c2to16(Mid(tmp, 1, 4)) & c2to16(Mid(tmp, 5, 2) & Right(tmp, 2)) & Right(decodeStr, 1))))
code = Right(code, codelen - 9)
End
If
Else
UTF8Decode = UTF8Decode & leftStr
code = Right(code, codelen - 1)
End
If
Wend
End
Function
'gb2312编码
Public
Function
GBKEncode(szInput)
As
String
Dim
i
As
Long
Dim
startIndex
As
Long
Dim
endIndex
As
Long
Dim
x()
As
Byte
x = StrConv(szInput, vbFromUnicode)
startIndex = LBound(x)
endIndex = UBound(x)
For
i = startIndex
To
endIndex
GBKEncode = GBKEncode &
"%"
& Hex(x(i))
Next
End
Function
'GB2312编码
Public
Function
GBKDecode(
ByVal
code
As
String
)
As
String
code = Replace(code,
"%"
,
""
)
Dim
bytes(1)
As
Byte
Dim
index
As
Long
Dim
length
As
Long
Dim
codelen
As
Long
codelen = Len(code)
While
(codelen > 3)
For
index = 1
To
2
bytes(index - 1) = Val(
"&H"
& Mid(code, index * 2 - 1, 2))
Next
index
GBKDecode = GBKDecode & StrConv(bytes, vbUnicode)
code = Right(code, codelen - 4)
codelen = Len(code)
Wend
End
Function
'二进制代码转换为十六进制代码
Public
Function
c2to16(
ByVal
x
As
String
)
As
String
Dim
i
As
Long
i = 1
For
i = 1
To
Len(x)
Step
4
c2to16 = c2to16 & Hex(c2to10(Mid(x, i, 4)))
Next
End
Function
'二进制代码转换为十进制代码
Public
Function
c2to10(
ByVal
x
As
String
)
As
String
c2to10 = 0
If
x =
"0"
Then
Exit
Function
Dim
i
As
Long
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
'10进制转n进制(默认2)
Public
Function
c10ton(
ByVal
x
As
Integer
,
Optional
ByVal
n
As
Integer
= 2)
As
String
Dim
i
As
Integer
i = x \ n
If
i > 0
Then
If
x
Mod
n > 10
Then
c10ton = c10ton(i, n) + chr(x
Mod
n + 55)
Else
c10ton = c10ton(i, n) +
CStr
(x
Mod
n)
End
If
Else
If
x > 10
Then
c10ton = chr(x + 55)
Else
c10ton =
CStr
(x)
End
If
End
If
End
Function
VB6的UTF8编码解码的更多相关文章
- Qt Creator无法用“UTF-8”编码解码
在Qt Creator 里打开其他编辑器的代码时有时会提示: 无法用"UTF-8"编码解码 在文件上右键使用NotePad++编辑器打开: 选择->格式-&g ...
- 特殊字符(包括emoji)梳理和UTF8编码解码原理(转)
转自:https://www.jianshu.com/p/57c27d67a8a8 背景知识 emoji表情符号,是20世纪90年代由NTT Docomo栗田穣崇(Shigetaka Kurit)创建 ...
- javascript中的Base64.UTF8编码与解码详解
javascript中的Base64.UTF8编码与解码详解 本文给大家介绍的是javascript中的Base64.UTF8编码与解码的函数源码分享以及使用范例,十分实用,推荐给小伙伴们,希望大家能 ...
- JavaScript进行UTF-8编码与解码
JavaScript本身可通过charCodeAt方法得到一个字符的Unicode编码,并通过fromCharCode方法将Unicode编码转换成对应字符. 但charCodeAt方法得到的应该是一 ...
- Python8_关于编码解码和utf-8
关于编码:ASCII码是早期的编码规范,只能表示128个字符.7位二进制数表示 扩展ASCII码,由于ASCII码不够用,ASCII表扩充到256个符号,不同的国家有不同的标准:8位二进制数 Unic ...
- 编码解码--三种常见字符编码简介:ASCII、Unicode和UTF-8
什么是字符编码? 计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte),所以,一个字节能表示的最大的整数就是255( ...
- URI编码解码和base64
概述 对于uri的编解码,在js中有3对函数,分别是escape/unescape,encodeURI/decodeURI,encodeURIComponent/decodeURIComponent. ...
- [转]utf8编码原理详解
from : http://blog.csdn.net/baixiaoshi/article/details/40786503 很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态 ...
- java中文乱码解决之道(五)-----java是如何编码解码的
在上篇博客中LZ阐述了java各个渠道转码的过程,阐述了java在运行过程中那些步骤在进行转码,在这些转码过程中如果一处出现问题就很有可能会产生乱码!下面LZ就讲述java在转码过程中是如何来进行编码 ...
随机推荐
- 【poj3415-Common Substrings】sam子串计数
题意: 给出两个串,问这两个串的所有的子串中(重复出现的,只要是位置不同就算两个子串),长度大于等于k的公共子串有多少个. 题解: 这题好像大神们都用后缀数组做..然而我在sam的题表上看到这题,做 ...
- javascript的阻塞机制
javascript的阻塞机制 浏览器在执行javascript代码时,不能同时做其它事情,当遇到javascript时,浏览器会下载js文件,解析并执行该文件,而在这期间页面的渲染是完全被阻塞的,因 ...
- 从setting文件导包
两种方式 from project.settings.py import s3_key 第二种: from django.conf import setting s3_key = settings.S ...
- linux设备驱动中的并发控制【转】
转自:http://www.cnblogs.com/plinx/archive/2013/01/28/2873121.html 在linux内核中,主要的静态发生于以下几种情况: 1.对称多处理器(S ...
- sql ---- count 误区
select count(字段名) from table group by 字段; 分组后的内容统计. count统计的字段是 不包括空的数据.
- javascript三种嵌入方式
什么是JavaScript? JavaScript是运行在浏览器端的脚步语言,JavaScript主要解决的是前端与用户交互的问题,包括使用交互与数据交互,JavaScript是浏览器解释执行的. J ...
- selenium+python自动化77-autoit文件上传【转载】
前言 关于非input文件上传,点上传按钮后,这个弹出的windows的控件了,已经跳出三界之外了,不属于selenium的管辖范围(selenium不是万能的,只能操作web上元素).autoit工 ...
- VS中使用Gulp
关于gulp资料可以访问:http://www.gulpjs.com.cn/,本篇主要讲解在VS中使用gulp对js和css进行压缩合并 1.下载node.js,gulp依赖于node.js,可以访问 ...
- 整数快速乘法/快速幂+矩阵快速幂+Strassen算法
快速幂算法可以说是ACM一类竞赛中必不可少,并且也是非常基础的一类算法,鉴于我一直学的比较零散,所以今天用这个帖子总结一下 快速乘法通常有两类应用:一.整数的运算,计算(a*b) mod c 二.矩 ...
- 百度Map-JSAPI-覆盖物范围查询标记
1.单点标记并添加说明信息 2.可视化区域范围 3.矩形覆盖物经纬度范围 说明:该项目为javaweb项目,标记点信息为数据库中存储信息] 参考API链接 http://api.map.baidu.c ...