Conversion method between string and hex

Source: Internet
Author: User

Code in 1.c#

///<summary>
///< functions: Encode>
///function: Converts a string content into a 16-encoded data encoding, and its inverse procedure is decode
///parameter description: br>///Strencode The original string that needs to be transformed
///the process of converting a character directly into a Unicode character, such as the number "3"-->0033, the Chinese character "I"-->u+6211
the///function Dec The process of ODE is the inverse process of encode.
///</summary>
///<param name= "Strencode" ></PARAM>
///<returns></ret       Urns>
public static string Encode (String strencode)
{
String strreturn = ";/store converted encoding
foreach (Short shortx in Strencode.tochararray ())
{
Strreturn = = Shortx. ToString ("X4");     
}
Return Strreturn

///<summary>
///< functions: Decode>
///effect: Converting 16-encoded data encoding to a string is the reverse process of encode
///</sum Mary>
///<param name= "Strdecode" ></PARAM>
///<returns></returns>
Publ IC 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

'*******************************************************************

'<函数:Encode>
'作用:将字符串内容转化为16进制数据编码,其逆过程是Decode
'参数说明:
'strSource 需要转化的原始字符串
Public Function Encode(strEncode As String) As String
  Dim i As Long
  Dim chrTmp$
  Dim ByteLower$, ByteUpper$
  Dim strReturn$ '存储转换后的编码
  
  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
'</函数:Encode>

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.