-1

I have the following function:

    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);
        }
    }

The data coming in is a bunch of hex as a string. I need to split this hex string into batches of 8 characters, append an incrementing 0 padded hex number from 00 to 1f and send this new string as a byte array to the _reader.Protocol function, which accepts a string wb as first parameter and the block as the second.

For example incoming data is:

string data = "3930313B36313B5350542D53504C3B3830303B3B352E373B3B303B303B3B3B34353036383B4E3B4E3B"

I need to send the following to the _reader.Protocol object: (incremented padded hex 01, 02, 03, ... , 0f) and the first 8 characters of the data string, then the next, and so on as a byte array. [013930313B], [0236313B53], etc.

I think I'm getting close... but missing something...

My problem at the moment is that I can't figure out how to loop in blocks of 8 and if the hex string is say 82 characters instead of 80 (multiple of 8), then how would I grab the last two characters without getting a IndexOutofRange exception.

Note: This is for a Windows CE application, so no new C# features please.

PiotrG
  • 743
  • 6
  • 24
  • 1
    The elusive question @ScottChamberlain just below the post, "I need to split this hex string into batches of 8 characters, append an incrementing 0 padded hex num ..." – Ravi Tiwari May 28 '18 at 17:52
  • @RaviTiwari that is not a question, that is a project goal. A question would be "I can't figure out how to group the data in to 8 character blocks" or "I can't figure out how to transform a series of 8 hex characters in to a byte array" or "I can't figure out how to append a incrementing counter to the front of a byte array". – Scott Chamberlain May 28 '18 at 17:55
  • I am sorry I was not clear about what I am having trouble with. I will edit my question. – PiotrG May 28 '18 at 21:47

2 Answers2

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();
}
Kitson88
  • 2,889
  • 5
  • 22
  • 37
0

I'm figured it out. It might not be the most efficient solution but it works just fine. I did it using a for loop inside a for loop.

In case anyone is interested here is the final code:

    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)
        {
        }
    }
PiotrG
  • 743
  • 6
  • 24