Conversion methods between string and (Unicode) hexadecimal numbers include (C # and VB6)

Source: Internet
Author: User
Tags goto
Many people will encounter character conversion problems when they send text messages. That is, the problem of converting Chinese characters into Unicode encoding.
Code in the 1.c#

<summary>
< function:encode>
Function: Converts the string content into 16 data encoding, the inverse process is decode
Parameter description:
Strencode the original string that needs to be transformed
The process of conversion is to convert characters directly into Unicode characters, such as the number "3"-->0033, the Chinese character "I"-->u+6211
The process of function decode is the inverse process of encode.
</summary>
<param name= "Strencode" ></param>
<returns></returns>
public static string Encode (String strencode)
{
String strreturn = "";//Store converted Encoding
foreach (Short shortx in Strencode.tochararray ())
{
Strreturn + = Shortx. ToString ("X4");
}
return strreturn;
}
<summary>
< function:decode>
function: Converting 16 data encoding into a string is an inverse process of encode
</summary>
<param name= "Strdecode" ></param>
<returns></returns>
public static string Decode (String strdecode)
{
String sresult = "";
for (int i = 0; i < STRDECODE.LENGTH/4; i++)
{
Sresult + = (char) short. Parse (Strdecode.substring (i * 4, 4), global::system.globalization.numberstyles.hexnumber);
}
return sresult;
}
Code in the 2.VB6
'*******************************************************************
' < function:encode>
' Function: Converts the string content into 16-encoded data, and the inverse process is decode
' Parameter description:
' Strsource the original string to be transformed
Public Function Encode (Strencode As String) as String
Dim I as Long
Dim chrtmp$
Dim bytelower$, byteupper$
Dim strreturn$ ' Store converted Encoding

For i = 1 to Len (Strencode)
chrtmp$ = Mid (Strencode, I, 1)
bytelower$ = hex$ (AscB (midb$ (chrtmp$, 1, 1))
If Len (bytelower$) = 1 Then bytelower$ = "0" & bytelower$
byteupper$ = hex$ (AscB (midb$ (chrtmp$, 2, 1))
If Len (byteupper$) = 1 Then byteupper$ = "0" & byteupper$
strreturn$ = strreturn$ & byteupper$ & bytelower$
Next

Encode = strreturn$
End Function
' </function:encode>
'*******************************************************************


'*******************************************************************
' < function:decode>
' Function: Converting 16-encoded data into strings is an inverse process of encode
Public Function Decode (Strdecode As String) as String
Dim I as Long
Dim strcode$ ' Store converted Encoding
Dim chrtmp$

On Error GoTo Errproc

If Len (Strdecode) Mod 4 <> 0 Then GoTo Errproc
For i = 1 to Len (strdecode) Step 4
Strcode = mid$ (Strdecode, I, 4)
chrtmp$ = ChrW ("&h" & Strcode)
If chrtmp$ = "?" Then If strcode <> "003F" Then GoTo Errproc
Decode = Decode & chrtmp$
Next

Exit Function
Errproc:
Decode = Strdecode
Dealwithevents "unresolved message:" & Strdecode
End Function
' </function:decode>
'*******************************************************************
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.