在c#中将ascii代码转换为十六进制字符串 [英] Convert ascii code to hex string in c#

查看:781
本文介绍了在c#中将ascii代码转换为十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何将!q44499:²?о888?=º88Š转换为19217134343439393AB205D0BE383838133DBA38388A。我试过Encoding.Default.GetBytes(!q44499:²?о888?=º88Š)。但对于字符²,我得到3f,而不是b2。请帮助。

Hi,

How to convert "!q44499:²о888=º88Š" to "19217134343439393AB205D0BE383838133DBA38388A". I have tried Encoding.Default.GetBytes("!q44499:²о888=º88Š"). But for character "²" I get "3f", instead of "b2".Please help.

推荐答案

也许这个答案可行???



Maybe this answer would work???

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
class Convert
{

    public void DoConversion()
    {
        string str = "test";
        byte[] byteArray = null;
        System.Text.StringBuilder hexNumbers = new System.Text.StringBuilder();
        byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
        for (int i = 0; i <= byteArray.Length - 1; i++) {
            hexNumbers.Append(byteArray[i].ToString("x"));
        }
        Interaction.MsgBox(hexNumbers.ToString());
        // Out  put will be 74657374

        // The following code will reverse the operation and verify the output:
        string st = hexNumbers.ToString();
        string com = "";
        for (x = 0; x <= st.Length - 1; x += 2) {
            com += Strings.ChrW(Convert.ToInt32("&H" + st.Substring(x, 2)));
        }
        Interaction.MsgBox(com);
    }
}







http://stackoverflow.com/questions/25129402/how-to-convert-ascii-to-hexdecimal -in-vb-net [ ^ ]


public static string AsciiToHex(string asciiString)
       {
           StringBuilder builder = new StringBuilder();
           foreach (char c in asciiString)
           {
               builder.Append(Convert.ToInt32(c).ToString("X"));
           }
         return builder.ToString();
       }


这篇关于在c#中将ascii代码转换为十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆