Пример #1
0
        static public string EncodeToHexStr(string strToHex)
        {
            //char[] charrToHex = strToHex.ToCharArray();

            if (strToHex == string.Empty)
            {
                return(string.Empty);
            }
            try
            {
                int           intNum1  = strToHex.Length;
                StringBuilder sbResult = new StringBuilder(strToHex + strToHex + strToHex + strToHex);
                //sbResult.Length=intNum1*4;
                //sbResult.EnsureCapacity(intNum1*4);

                intNum1--;

                for (int intCounter1 = 0; intCounter1 <= intNum1; intCounter1++)
                {
                    char chStringChar = strToHex[intCounter1];
                    //byte[] btarrArray = new byte[];
                    UInt16 uintChar = (UInt16)chStringChar;

                    byte bt1 = 0;
                    byte bt2 = 0;
                    byte bt3 = 0;
                    byte bt4 = 0;

                    TEXCSMath.UInt32ToFourBytes(uintChar, out bt1, out bt2, out bt3, out bt4);


                    string strChar1 = bt1.ToString("X");
                    string strChar2 = bt2.ToString("X");
                    string strChar3 = bt3.ToString("X");
                    string strChar4 = bt4.ToString("X");

                    sbResult[intCounter1 * 4]       = strChar1[0];
                    sbResult[(intCounter1 * 4) + 1] = strChar2[0];
                    sbResult[(intCounter1 * 4) + 2] = strChar3[0];
                    sbResult[(intCounter1 * 4) + 3] = strChar4[0];

                    //chStringChar.
                }

                return(sbResult.ToString());
            }
            catch (Exception exc)
            {
                EXException.ThrowEXException("Πρόβλημα στην κωδικοποίηση σε HEX αλφαριθμητικό.", exc);
                return(string.Empty);
            }
        }
Пример #2
0
        static public string DecodeHexStrToStr(string strHex)
        {
            if (strHex == string.Empty)
            {
                return(string.Empty);
            }
            try
            {
                int intNum1 = strHex.Length;
                EXException.CheckEXError((intNum1 % 4) == 0, "EX {30CF8C85-C636-4f3e-B97E-3D3E02B433FE}");
                StringBuilder sbResult = new StringBuilder(new string(' ', intNum1 / 4));
                //sbResult.Length=intNum1*4;
                //sbResult.EnsureCapacity(intNum1*4);

                intNum1--;

                for (int intCounter1 = 0; intCounter1 <= intNum1; intCounter1 = intCounter1 + 4)
                {
                    string chStringChar1 = new string(strHex[intCounter1], 1);
                    string chStringChar2 = new string(strHex[intCounter1 + 1], 1);
                    string chStringChar3 = new string(strHex[intCounter1 + 2], 1);
                    string chStringChar4 = new string(strHex[intCounter1 + 3], 1);

                    //byte[] btarrArray = new byte[];

                    byte bt1 = Convert.ToByte(chStringChar1, 16);
                    byte bt2 = Convert.ToByte(chStringChar2, 16);
                    byte bt3 = Convert.ToByte(chStringChar3, 16);
                    byte bt4 = Convert.ToByte(chStringChar4, 16);

                    UInt32 uintChar = 0;
                    TEXCSMath.FourBytesToUInt32(bt1, bt2, bt3, bt4, out uintChar);

                    sbResult[intCounter1 / 4] = (char)uintChar;
                    //chStringChar.
                }

                return(sbResult.ToString());
            }
            catch (Exception exc)
            {
                EXException.ThrowEXException("Πρόβλημα στην αποκωδικοποίηση του HEX αλφαριθμητικού.", exc);
                return(string.Empty);
            }
        }