
正文
vb.net中把汉字转码 vb汉字转换为ascii码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
vb.net怎么把汉字转换为GB2312编码,例如“广东”变成%B9%E3%B6%AB
GB2312是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
相关问答
Q1: vb 怎么把汉字转换成gbk编码
vb 怎么把汉字转换成gbk编码
参考方法如下vb.net中把汉字转码:
面vb.net中把汉字转码的两段VB代码分别针对UTF-8(UTF8EncodeURI)和GB2312(GBKEncodeURI)进行vb.net中把汉字转码了编码的转换。
Private Sub command1_click()
Debug.Print (UTF8EncodeURI("汉字"))
Debug.Print (GBKEncodeURI("汉字"))
End Sub
Function UTF8EncodeURI(szInput)
Dim wch, uch, szRet
Dim x
Dim nAsc, nAsc2, nAsc3
If szInput = "" Then
UTF8EncodeURI = szInput
Exit Function
End If
For x = 1 To Len(szInput)
wch = Mid(szInput, x, 1)
nAsc = AscW(wch)
If nAsc 0 Then nAsc = nAsc + 65536
If (nAsc And HFF80) = 0 Then
szRet = szRet wch
Else
If (nAsc And HF000) = 0 Then
uch = "%" Hex(((nAsc \ 2 ^ 6)) Or HC0) Hex(nAsc And H3F Or H80)
szRet = szRet uch
Else
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
UTF8EncodeURI = szRet
End Function
Function GBKEncodeURI(szInput)
Dim i As Long
Dim x() As Byte
Dim szRet As String
szRet = ""
x = StrConv(szInput, vbFromUnicode)
For i = LBound(x) To UBound(x)
szRet = szRet "%" Hex(x(i))
Next
GBKEncodeURI = szRet
End Function
Q2: 如何用vb.net将汉字转换成拼音阿
public string hz2py(string hz) //获得汉字的区位码
{
byte[] sarr = System.Text.Encoding.Default.GetBytes(hz);
int len = sarr.Length;
if (len1)
{
byte[] array = new byte[2];
array = System.Text.Encoding.Default.GetBytes(hz);
int i1 = (short)(array[0] - '\0');
int i2 = (short)(array[1] - '\0');
//unicode解码方式下的汉字码
// array = System.Text.Encoding.Unicode.GetBytes(hz);
// int i1 = (short)(array[0] - '\0');
// int i2 = (short)(array[1] - '\0');
// int t1 = Convert.ToInt32(i1,16);
// int t2 = Convert.ToInt32(i2,16);
int tmp=i1*256+i2;
string getpychar="*";//找不到拼音码的用*补位
if(tmp=45217tmp=45252){getpychar= "A";}
else if(tmp=45253tmp=45760){getpychar= "B";}
else if(tmp=47761tmp=46317){getpychar= "C";}
else if(tmp=46318tmp=46825){getpychar= "D";}
else if(tmp=46826tmp=47009){getpychar= "E";}
else if(tmp=47010tmp=47296){getpychar= "F";}
else if(tmp=47297tmp=47613){getpychar= "G";}
else if(tmp=47614tmp=48118){getpychar= "H";}
else if(tmp=48119tmp=49061){getpychar= "J";}
else if(tmp=49062tmp=49323){getpychar= "K";}
else if(tmp=49324tmp=49895){getpychar= "L";}
else if(tmp=49896tmp=50370){getpychar= "M";}
else if(tmp=50371tmp=50613){getpychar= "N";}
else if(tmp=50614tmp=50621){getpychar= "O";}
else if(tmp=50622tmp=50905){getpychar= "P";}
else if(tmp=50906tmp=51386){getpychar= "Q";}
else if(tmp=51387tmp=51445){getpychar= "R";}
else if(tmp=51446tmp=52217){getpychar= "S";}
else if(tmp=52218tmp=52697){getpychar= "T";}
else if(tmp=52698tmp=52979){getpychar= "W";}
else if(tmp=52980tmp=53640){getpychar= "X";}
else if(tmp=53689tmp=54480){getpychar= "Y";}
else if(tmp=54481tmp=55289){getpychar= "Z";}
return getpychar;
}
else
{
return hz;
}
}
public string transpy(string strhz) //把汉字字符串转换成拼音码
{
string strtemp="";
int strlen=strhz.Length;
for (int i=0;i=strlen-1;i++)
{
strtemp+=hz2py(strhz.Substring(i,1));
}
return strtemp;
}
create table tabpy(id int identity,b_begin varbinary(2),b_end varbinary(2),word varchar(2))
insert tabpy select 0xB0A1, 0xB0C4,'A'
union all select 0xB0C5, 0xB2C0,'B'
union all select 0xB2C1, 0xB4ED,'C'
union all select 0xB4EE, 0xB6E9,'D'
union all select 0xB6EA, 0xB7A1,'E'
union all select 0xB7A2, 0xB8C0,'F'
union all select 0xB8C1, 0xB9FD,'G'
union all select 0xB9FE, 0xBBF6,'H'
union all select 0xBBF7, 0xBFA5,'J'
union all select 0xBFA6, 0xC0AB,'K'
union all select 0xC0AC, 0xC2E7,'L'
union all select 0xC2E8, 0xC4C2,'M'
union all select 0xC4C3, 0xC5B5,'N'
union all select 0xC5B6, 0xC5BD,'O'
union all select 0xC5BE, 0xC6D9,'P'
union all select 0xC6DA, 0xC8BA,'Q'
union all select 0xC8BB, 0xC8F5,'R'
union all select 0xC8F6, 0xCBF9,'S'
union all select 0xCBFA, 0xCDD9,'T'
union all select 0xCDDA, 0xCEF3,'W'
union all select 0xCEF4, 0xD1B8,'X'
union all select 0xD1B9, 0xD4D0,'Y'
union all select 0xD4D1, 0xD7F9,'Z'
函数:
create function getfirstpy(@a varchar(200))
returns varchar(100)
as
begin
declare @i int,@j int,@result varchar(100)
set @result=''
set @i=len(@a)
set @j=1
while @j=@i
begin
select @result=@result+word from tabpy where cast(substring(@a,@j,1) as varbinary(2)) between b_begin and b_end
set @j=@j+1
end
return @result
end
Q3: VB中如何将汉字转换为Unicode代码?
回答补充: 用Chr()函数把ascii码转化成汉字
用ChrW()函数把Unicode码转化成汉字
(还有之前说的
AscW()函数返回值为负的时候,应加上65536 才得到汉字实际的Unicode码)
----------------------------------------------------------------------------------------
求汉字的Unicode代码 用AscW()函数
AscW("汉")=27721
求汉字的ASCII码 用Asc()函数 返回一个负数值
如Asc("汉")=-17734
PS:PS:改进了下 可转化多个汉字
在窗体上建三个text控件,
功能:text1中按个输入汉字后 text2中按个显示汉字的Unicode代码 text3中显示汉字的ASCII码
原代码如下:
Private Function change(s As String) As String
change = AscW(s)
If change 0 Then
change = change + 65536
End If
End Function
Private Function change2(s As String) As String
change2 = Asc(s)
End Function
Private Sub Text1_Change()
For i = 1 To Len(Text1.Text)
s = s change(Mid(Text1.Text, i, 1)) " "
Next
Text2.Text = s
s = ""
For i = 1 To Len(Text1.Text)
s = s change2(Mid(Text1.Text, i, 1)) " "
Next
Text3.Text = s
End Sub
补充:a 的ASCII码是97
Asc函数返回的是字符串首字母的ASCII码(如字符串只一个字符,即返回该字符的ASCII码)
Asc 函数
返回一个 Integer,代表字符串中首字母的字符代码。
语法
Asc(string)
必要的 string 参数可以是任何有效的字符串表达式。如果 string 中没有包含任何字符,则会产生运行时错误。
PSPS: VB里 目前我还没找到能将之转换成 把汉字转换成西欧文编码的函数 不敢断言没有(有估计也很难找 我搜了好多关键字都没找到)
还有 不只是遨游浏览器,一般的IE浏览器 也有此功能
查看——编码——其他——最下面那小三角号——西欧(ISO)
vb.net中把汉字转码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于vb汉字转换为ascii码、vb.net中把汉字转码的信息别忘了在本站进行查找喔。






