C# HEX to ASCII String [GOOGLE: HEX to ASCII]

C# HEX to ASCII String [GOOGLE: HEX to ASCII]

C# HEX to ASCII String [GOOGLE: HEX to ASCII]



資料來源: https://stackoverflow.com/questions/5613279/c-sharp-hex-to-ascii


驗證網頁: https://www.rapidtables.com/convert/number/hex-to-ascii.html


/*
//test code
byte[] bModelName = new byte[24];
Array.Copy(byBuf, 12, bModelName, 0, bModelName.Length);//5359535238362D4831202020 0853 0000 △6D6A
m_StrReaderModelName = "";
m_StrReaderModelName =  System.Text.Encoding.Default.GetString(bModelName);
m_StrReaderModelName = CS_Chip.HexStr2AsciiStr(m_StrReaderModelName);
*/

    class CS_Chip
    {
        public static string HexStr2AsciiStr(String hexString)
        {
            //5359535238362D4831202020 -> "SYSR86-H1   "
            try
            {
                string ascii = string.Empty;

                for (int i = 0; i < hexString.Length; i += 2)
                {
                    String hs = string.Empty;

                    hs = hexString.Substring(i, 2);
                    uint decval = System.Convert.ToUInt32(hs, 16);
                    char character = System.Convert.ToChar(decval);
                    ascii += character;

                }

                return ascii;
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }

            return string.Empty;
        }

        public static bool String2ByteArray(String StrData, byte[] pdata, int iLen = 16)//字串轉陣列
        {
            bool blnResult = true;
            if (StrData.Length >= (iLen * 2))
            {
                int j = 0;
                //for (int i = 0; i < StrData.Length; i += 2)
                for (int i = (StrData.Length - 2); i >= 0; i -= 2)
                {
                    try
                    {

                        int buf = Convert.ToInt32(StrData.Substring(i, 2), 16);
                        pdata[j] = Convert.ToByte(buf);
                        j++;
                    }
                    catch
                    {
                        blnResult = false;
                        break;
                    }
                }
            }
            else
            {
                blnResult = false;
            }
            return blnResult;
        }

        public static bool ByteArray2String(byte[] pdata, ref String StrData,bool blnReverse=true)//陣列轉字串
        {
            StrData = "";
            bool blnResult = true;

            if(blnReverse == true)
            {
                Array.Reverse(pdata);
            }

            try
            {
                for(int i=0;i< pdata.Length;i++)
                {
                    StrData+= Convert.ToString(pdata[i], 16).PadLeft(2, '0') + " ";
                }
            }
            catch
            {
                blnResult = false;
            }
            return blnResult;
        }

    }

One thought on “C# HEX to ASCII String [GOOGLE: HEX to ASCII]

  1. C# int 轉 HEX 字串(\XOO)


    using System;

    class Program
    {
    static void Main()
    {
    int num = 255;

    Console.WriteLine(@"int2\x : " + num + " ->" + string.Format(@"\x{0:x2}", 255)); //255 -> \xff

    //---
    int nL = 45;
    int nH = 0;
    string strCmd = "\x1d" + "$" + (char)nL + (char)nH; // 垂直起始位置 (nL+(nH*256))*0.125=60*0.125=7.5mm
    byte[] data = Encoding.GetEncoding("big5").GetBytes(strCmd);
    }
    }

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *