0

我确实有一个编码函数可以创建一个命令,我假设我有一个字节字符串输出,例如 (0010010210004443331012011101000)

我确实需要创建解码函数来从这个字节中提取一些数据让我们说从位置 8 到 12 并且在提取这个之后检查 Array 的第一个字节是否为 00 然后返回我以 ASCII 提取数据

这是我的解码

这是我的完全错误的解码

public byte[] Decode(string Resp)
{

    string NewResp = Resp;
        string SubResp = NewResp.Substring(65, 185);
        Console.WriteLine("Substring: {0}", SubResp);

        MessageBox.Show(SubResp);
    return null;
}

这是编码

 {


        return bCommand;
    }
4

2 回答 2

0

你有像

string hexstr= "68696d616e736875";
string subHexstr= hexstr.Substring(2,3); // Lets say index from 3rd byte to 5th byte.
byte[] by=new byte[subHexstr.Length];
        int j = 0;
        for (int i = 0; i < by.Length; i++)
        {
            by[i] = byte.Parse(subHexstr.Substring(j, 2), System.Globalization.NumberStyles.HexNumber);
            j = j + 2;
        }

if(by[0]!=0x00)
{ 
  string asciiStr= Encoding.Ascii.GetString(by);
}
于 2012-12-18T05:27:29.133 回答
0
    class Program
    {
        static void Main(string[] args)
        {
            byte[] encoded =
                {
                    0x01, 0x00, 0x00, 0x24, 0x02, 0x43, 0x31, 0x31, 0x00, 0x00, 0x01, 0x43, 0x49, 0x53, 0x2D,
                    0x34, 0x36, 0x58, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x11
                };

            byte[] toCheck = SubArray<byte>(encoded, 8, 5); // start index at 8 and u need till 12th

            if (toCheck[0] != '\0')
            {
                Console.WriteLine(Encoding.ASCII.GetString(toCheck));
            }

            Console.Read();
        }

        public static T[] SubArray<T>(T[] data, int index, int length)
        {
            T[] result = new T[length];
            Array.Copy(data, index, result, 0, length);
            return result;
        }

    }
于 2012-12-18T04:56:55.610 回答