Main Site Documentation

Program error that I do not understang


#1

I hope this is allowed but I get exception in the attached code that I cannot solve.
The code fails in the same way using TinyCLR 1.0.0 or using a VS ConsoleApp.
Lots of folks in here WAY smarter than I.

Where did I go wrong?
I want to use hex strings for a TinyCLR serial app.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string shex = MakeHexString("Will George");
            string utf8 = TextFromHexString(shex);

            string MakeHexString(string str)
            {
                var sb = new StringBuilder();

                var bytes = Encoding.UTF8.GetBytes(str);
                foreach (var x in bytes)
                {
                    sb.Append(x.ToString("X2"));
                }

                // returns {57696C6C2047656F726765}
                return sb.ToString();

            } //End  MakeHexString

            string TextFromHexString(string hexString)
            {
                Console.WriteLine("hexString = " + hexString);

                var bytes = new byte[hexString.Length / 2];
                var bytestemp = new byte();

                // Will George
                // "57696C6C2047656F726765"
                // "57 69 6C 6C 20 47 65 6F 72 67 65" [22 bytes] //Fails at first 6C

                for (var i = 0; i < bytes.Length; i++)
                {
                    string test = hexString.Substring(i * 2, 2);
                    Console.WriteLine("test = " + test);

                    // The following line
                    // System.FormatException: 
                      'Input string was not in a correct format.'
                    
                    // However the first two bytes do not give this exception <<<<<

                    bytestemp = Convert.ToByte(test);

                    Console.WriteLine("bytestemp = " + bytestemp);

                    bytes[i] = bytestemp;
                    Console.WriteLine(bytes[i] + " i= " + i);
                }
		//
                return Encoding.UTF8.GetString(bytes);

            } //End TextFromHexString

        } //main
    } //class
} //namespace 

Any comments? Thanks!


#2

The function you’re using, Convert.ToByte, doesn’t support hex strings. Normally in .NET you could use Convert.ToByte("6C", 16); or byte.TryParse("6C", System.Globalization.NumberStyles.HexNumber, null, out var result) but, unfortunately, neither are supported in TinyCLR.

If you’re looking for an algorithm to get you started, check out the .NET reference source. byte.TryParse eventually calls down to https://referencesource.microsoft.com/#mscorlib/system/number.cs,08960a85829abd75 (you’ll need to adapt it to not use pointers nor the parameters passed in since they don’t exist).


#3

Thanks for the reply!

I have other ways to do it so that is not the issue.

I just found it strange that it would always do two bytes and on the third byte I would get the exception.


#4

My guess for that is that the first two bytes in your example are legitimate base-10 numbers, so they parse into base-10. It’s only the third that was not legal base-10.


#5

Thanks again!