求教vb.net utf-8 解码
- 提问者网友:欲劫无渡
- 2021-01-30 22:11
解码出来的中文为:送达日期 送达时间
请教vb.net 代码 谢谢
- 五星知识达人网友:上分大魔王
- 2021-01-30 22:49
sInput = sInput.Replace("%", "")
sInput = sInput.Replace(":", "")
Dim ret_GBKDecode As String = ""
Dim sLen As Integer = sInput.Length
Dim n As Integer = sLen \ 2
Dim sBytes(0 To n - 1) As Byte
'转化为字节码
For i As Integer = 1 To n
sBytes(i - 1) = CByte("&H" & sInput.Substring(2 * i - 2, 2))
Next
'将字节码转化为字符串
ret_GBKDecode = System.Text.Encoding.UTF8.GetString(sBytes)
Return ret_GBKDecode
End Function
'上面是函数,调用方法
MsgBox(GBKDecode(":%E9%80%81%E8%BE%BE%E6%97%A5%E6%9C%9F%20%E9%80%81%E8%BE%BE%E6%97%B6%E9%97%B4"))
- 1楼网友:独钓一江月
- 2021-01-30 23:38
'utf-8编码 '参数1:sinput 需要进行utf-8编码的字符串 '参数2:schar 可选参数,默认为“=” public function utf8encode(byval sinput as string, optional byval schar as string = "=") as string dim s_utf8bytes() as byte dim s_ret as string = "" s_utf8bytes = system.text.encoding.utf8.getbytes(sinput) '得到utf-8字节数组 for i as integer = 0 to s_utf8bytes.length - 1 if s_utf8bytes(i) <= 127 then s_ret &= chr(s_utf8bytes(i)) else '添加分隔符 if s_utf8bytes.length - i >= 3 then s_ret &= schar & hex(s_utf8bytes(i) \ 16) & hex(s_utf8bytes(i) mod 16) i += 1 s_ret &= schar & hex(s_utf8bytes(i) \ 16) & hex(s_utf8bytes(i) mod 16) i += 1 s_ret &= schar & hex(s_utf8bytes(i) \ 16) & hex(s_utf8bytes(i) mod 16) end if end if next return s_ret end function
'utf-8解码函数 '参数1:sinput 需要解码的utf-8编码串 '参数2:schar 可选参数,默认为“=” public function utf8decode(byval sinput as string, optional byval schar as string = "=") as string dim s_bytes(0 to 2) as byte dim s_ret as string = ""
for i as integer = 0 to sinput.length - 1 if sinput.substring(i, 1) <> schar then s_ret &= sinput.substring(i, 1) else if sinput.length - i >= 9 then s_bytes(0) = cbyte("&h" & sinput.substring(i + 1, 2)) s_bytes(1) = cbyte("&h" & sinput.substring(i + 4, 2)) s_bytes(2) = cbyte("&h" & sinput.substring(i + 7, 2)) s_ret &= system.text.encoding.utf8.getstring(s_bytes) i += 8 end if end if next return s_ret end function #end region
'例如: 1、调用utf8encode编码 sstr=utfencode("吉林") msgbox sstr '弹出信息 “=e5=90=89=e6=9e=97” 2、调用utfdecode解码 sstr=utfdecode("=e9=95=bf=e6=98=a5") msgbox sstr '弹出信息 “长春”