Vb.net converts Chinese characters to GB2312 encoding, for example, "Guangdong" to % B9 % E3 % B6 % AB

Source: Internet
Author: User

GB2312 is a subset of GBK encoding. Use the GBK encoding function.
'Gbk encoding (results are separated by percent signs %)
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) 'characters and strings are stored in unicode Encoding in vb2008.

EndIndex = x. Length-1
For I = startIndex To endIndex
Ret_GBKEncode & = "%" & Hex (x (I ))
Next
Return ret_GBKEncode
End Function

'Gbk Decoding
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
'Convert to bytecode
For I As Integer = 1 To n
SBytes (I-1) = CByte ("& H" & sInput. Substring (2 * I-2, 2 ))
Next
'Convert bytecode into a string
Ret_GBKDecode = System. Text. Encoding. Default. GetString (sBytes)
Return ret_GBKDecode
End Function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.