vb.net怎么把汉字转换为GB2312编码,例如“广东”变成%B9%E3%B6%ABGB2312是GBK编码的子集,使用GBK编码函数即可
'GBK编码(结果以百分号%进行分隔)
Public Function GBKEncode(ByVal sInput As String) As String
Dim ret_GBKEncode As String = ""
Dim i As Integer
Dim startIndex As Integer = 0
Dim endIndex As Integer
Dim x() As Byte = System.Text.Encoding.Default.GetBytes(sInput) '字符以及字符串在vb2008中都是以unicode编码存储的
endIndex = x.Length - 1
For i = startIndex To endIndex
ret_GBKEncode = "%"Hex(x(i))
Next
Return ret_GBKEncode
End Function
'GBK解码
Public Function GBKDecode(ByVal sInput As String) As String
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.Default.GetString(sBytes)
Return ret_GBKDecode
End Function
vb.net 如何将编码GB2312转成UTF8Imports System.Text
Public Function StringAsUtf8Bytes(ByVal strData As String) As Byte()
Dim bytes() As Byte
bytes = Encoding.UTF8.GetBytes(strData)
Return bytes
End Function
说明:strData参数是GB2312字符串 , 函数返回UTF8字节数组
如何用VB.net 读写Unicode编码的文本不要用do until , 用for:
Dim arr() As String, i As Integer
【vb.net字符编码 vbnet stringformat】arr = Split(r.ReadLine, " ")
For i = LBound(arr) To UBound(arr)
MsgBox(arr(i))
Next
--------------------
还是用readline,如果你要提取两个非连续空格之间的字符串,可以用
Dim arr() As String
arr = Split(r.ReadLine, " ")
arr()数组里就存了你要的单词 。
快要下班了,明后天不上班,如果16:30之前还搞不定就要下个星期1了 。
----------------------------------
哈 , 是我的失误没想到还有可能有半角的日文 。
用utf-8就OK了 , 代码如下:
(文本文件存的时候不能存成ansi,要存成utf-8,或是Unicode)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim r As System.IO.StreamReader
r = New System.IO.StreamReader("c:\a.txt", System.Text.Encoding.GetEncoding("utf-8"))
Do While Not r.EndOfStream
MsgBox(r.ReadLine)
Loop
End Sub
VB.NET UNICODE码 显示Public Class Form1
Dim b() As Byte
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
b = System.Text.Encoding.Default.GetBytes(TextBox1.Text)
For i = 0 To UBound(b)
TextBox2.AppendText(i.ToString" ")
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = System.Text.Encoding.Default.GetString(b)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "Google free online translation service instantly translates text and web pages 。该翻译器支持: 中文(简体), 中文(繁体), shqip, 日本语, русский, langue fran?aise ..."
End Sub
End ClassVB.Net中用String类型表示字符串,内部采用Unicode编码 。当需要在网络或串口中收发字符串时,就需要在String和Byte数组之间进行转换,这项功能可以通过System.Text.Encoding类实现 。
Private zeroChars() As Char = {ChrW(0)}
Dim descBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(mDescription)
推荐阅读
- 怎么借助别的手机充电,怎么用手机给别人的手机充电
- 微信小程序的js引擎是否统一,微信小程序js的存在和意义
- jsc语言互相调用方法,js怎么调用c语言写的dll
- 阿里云服务器连接,阿里云服务器连接不稳定
- java八皇后代码 python八皇后代码
- angularjs监听事件,angular 监听
- 虚拟机多开怎样设置ip,虚拟机多开用什么网络模式
- 联想怎么加播放速度显卡,联想怎么加播放速度显卡设置
- c与cvb.net的简单介绍