ToString format

hey guys, I’m having a bit of a problem. In the full blown .net framework you can do a .ToString(“X2”) format and it works just fine. However when I try this in the .net MicroFramework it throws up an error.


// this code works fine in the regular framework but not in netmf
int y = 13;
Debug.Print(y.ToString("X2"));

What are you trying to do?

Debug.Print(y.ToString("X2"));

Convert y into Hexadecimal??

That’s what X2 does Sam,

you get:

    #### Exception System.ArgumentException - 0x00000000 (1) ####
    #### Message: 
    #### System.Number::ValidateFormat [IP: 0085] ####
    #### System.Number::Format [IP: 0008] ####
    #### System.Int32::ToString [IP: 000d] ####
    #### MFConsoleApplication1.Program::Main [IP: 000c] ####
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

or if you try to run it in the immediate window:

?y.ToString("X2")
Could not evaluate expression

@ hobeau
If that the case you can either write your own conversion method to do so,
or
you can Goggle the web to see if there is something available, for example
using the key words “decimal to hex in C#”…

Sam, that google search unfortunately will probably just lead him to the string format method, if it doesn’t the code would have been written by someone who doesnt know it exists… do you really want code from that someone? haha.

I just made a suggestion to him without having seen his reply yet.
I did not if he is an experience programmer or just start to do some programming.

When he replies may be I got to know what he’s really needed.
And may be able to answer his question better.
That’s all, Mark. I didn’t mean to lead him into the difficult place at all.

Hey guys thanks for all the responses. Yes I’m trying to convert it to Hex. According to Visual Studio intellisense there is a string parameter that can be passed to the .ToString() method and it is called format but I’m guessing that this is a toned down version in the netmf. Looks like I’m going to have to do it the old fashioned way… why does that way always seem harder?

Hey guys I found it [url]Expert .NET Micro Framework - Jens Khner - Google Books

Apparently you do have to write a custom one. Here’s the one they provide, seems to work great.


public static string ByteToHex(byte b)
        {
            const string hex = "0123456789ABCDEF";
            int lowNibble = b & 0x0F;
            int highNibble = (b & 0xF0) >> 4;
            string s = new string(new char[] { hex[highNibble], hex[lowNibble] });
            return s;
        }

@ hobeau
I was unable to get on to the internet all day yesterday(Saturday).

Glad that you find something that can solve your problem.
that method seem to be far more practical than mine.

using System;
using Microsoft.SPOT;

namespace MFConsoleApplication1
{
    public class Program
    {
        public static void Main()
        {
            int y = 13;
            Utils.toHex(y);
        }

    }

    public class Utils
    {
        public static void toHex(int n)
        {
            string r;
            int tmp;

            try
            {
                if (n == 0)
                {
                    tmp = 0;
                    r = tmp.ToString();
                    Debug.Print(r);
                }
                else
                {
                    tmp = n % 16;

                    n = n / 16;

                    toHex(n);

                    switch (tmp)
                    {
                        case 10:
                            r = "A";
                            Debug.Print(r);
                            break;
                        case 11:
                            r = "B";
                            Debug.Print(r);
                            break;
                        case 12:
                            r = "C";
                            Debug.Print(r);
                            break;
                        case 13:
                            r = "D";
                            Debug.Print(r);
                            break;
                        case 14:
                            r = "E";
                            Debug.Print(r);
                            break;
                        case 15:
                            r = "F";
                            Debug.Print(r);
                            break;
                        default:
                            r = tmp.ToString();
                            Debug.Print(r);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.ToString());
            }

        }
    }
}

Yeah I’ve tested it a lot and it works really well. Can’t take credit for it unfortunately lol.

Hi all,

I got issues with ToString format also.

I’m not doing hex print to string, I’m printing normal numbers like:


double value = 0.001;
stringa = (value).ToString("0.000");

when i run it in my G400 it say:


 #### Exception System.ArgumentException - 0x00000000 (1) ####
    #### Message: 
    #### System.Number::ValidateFormat [IP: 003f] ####
    #### System.Number::Format [IP: 0008] ####
    #### System.Double::ToString [IP: 002e] ####
    #### Osre.GentiliLib.GentiliUtils::ValueToDisplay [IP: 0234] ####
    #### Osre.GentiliLib.GentiliMaster::WriteDisplay [IP: 0041] ####
    #### MFConsoleGentiliTest.Program::Main [IP: 0062] ####
Eccezione first-chance di tipo 'System.ArgumentException' in mscorlib.dll
Eccezione non gestita di tipo 'System.ArgumentException' in mscorlib.dll

It only works without “0.000” format in tostring… it’s not working with “#.###” either.

What can I do?

:open_mouth:

NETMF does not Support all Format strings.
But
value.ToString(“F3”);
should work and give you a similar (if not the same) result.

Hep, and I am still looking for the documents about that…?

@ njbuch - Found it at: [url]Microsoft Learn: Build skills that open doors in your career

1 Like

One thing to note is that NETMF 4.4 now supports the “X” format in the ToString() method.

1 Like

@ Bec a Fuel - Byte Supports ToString(“X2”) at least since V4.3.

1 Like