C#十六进制到字节数组循环

时间:2018-05-28 16:00:43

标签: c#

我有以下功能:

    public void SetTagData(string _data)
    {
        string data = _data;

        byte[] ba = Encoding.Default.GetBytes(data);
        string hexString = BitConverter.ToString(ba);
        hexString = hexString.Replace("-", "");

        var blockStart = 0;
        var bufferHexBlocks = String.Empty;

        try
        {
            for (var i = 0; i < hexString.Length; i++)
            {
                var byteList = new List<byte>();
                byte[] datablockKey = ConvertHelpers.ConvertHexStringToByteArray(i.ToString().PadLeft(2, '0'));
                var block = hexString.Substring(blockStart, 8);
                byte[] datablockValue = ConvertHelpers.ConvertHexStringToByteArray(block);
                byteList.AddRange(datablockKey);
                byteList.AddRange(datablockValue);

                _reader.Protocol("wb", byteList.ToArray());
                blockStart += 8;
            }
        }
        catch (Exception ex)
        {
          console.log(ex.message);
        }
    }

进来的数据是一串十六进制字符串。我需要将此十六进制字符串拆分为8个字符的批处理,将00的递增0填充十六进制数附加到1f,并将此新字符串作为字节数组发送到_reader.Protocol函数,它接受字符串wb作为第一个参数,块接受第二个参数。

例如输入数据是:

string data = "3930313B36313B5350542D53504C3B3830303B3B352E373B3B303B303B3B3B34353036383B4E3B4E3B"

我需要将以下内容发送到_reader.Protocol对象: (递增的填充十六进制01, 02, 03, ... , 0f)和数据字符串的前8个字符,然后是下一个字符串,依此类推。 [013930313B][0236313B53]

我想我已经越来越近......但是错过了什么......

我目前的问题是我无法弄清楚如何在8个块中循环,如果十六进制字符串是82个字符而不是80个字符(8的倍数),那么我将如何获取最后两个字符没有获得IndexOutofRange异常。

注意:这适用于Windows CE应用程序,因此请不要使用新的C#功能。

2 个答案:

答案 0 :(得分:1)

This below will work fine in conjunction with this answer and the sample string data given.

public static byte[] Parse(string data)
{
            var count = data.Length / 8; //Might be worth throwing exception with any remainders unless you trust the source.
            var needle = 0;
            List<byte> result = new List<byte>(); //Inefficient but I'm being lazy 

            for (int i = 0; i < count; i++)
            {
                char[] buffer = new char[8];
                data.CopyTo(needle, buffer, 0, buffer.Length);

                //To get around the odd number when adding the prefixed count byte, send the hex string to the convert method separately.
                var bytes = ConvertHexStringToByteArray(new string(buffer)); //Taken From https://stackoverflow.com/a/8235530/6574422

                //As the count is less than 255, seems safe to parse to single byte
                result.Add(byte.Parse((i + 1).ToString()));
                result.AddRange(bytes);

                needle += 8;
            }

            return result.ToArray();
}

答案 1 :(得分:0)

我已经弄明白了。它可能不是最有效的解决方案,但它可以正常工作。我在for循环中使用了for循环。

如果有人对此感兴趣,请输入最终代码:

    public void SetTagData(string _data)
    {
        string data = _data;

        byte[] ba = Encoding.Default.GetBytes(data);
        string hexString = BitConverter.ToString(ba);
        hexString = hexString.Replace("-", "");

        var blockStart = 0;

        try
        {
            _reader.Protocol("s");

            for(var count = 0; count < 16; count++)  
            {
                var byteList = new List<byte>();
                byte[] datablockKey = ConvertHelpers.ConvertHexStringToByteArray(count.ToString("X2"));
                byteList.AddRange(datablockKey);
                for (var innerCount = 0; innerCount < 4; innerCount++)
                {
                    var block = String.Empty;
                    if (!String.IsNullOrEmpty(hexString.Substring(blockStart, 2)))
                    {
                        block = hexString.Substring(blockStart, 2);
                    }
                    else
                    {
                        block = "20";
                    }
                    byte[] datablockValue = ConvertHelpers.ConvertHexStringToByteArray(block);
                    byteList.AddRange(datablockValue);
                    blockStart += 2;
                }
                _reader.Protocol("wb", byteList.ToArray());
            }
        }
        catch (Exception)
        {
        }
    }