Arduino to netmf need a little help

I’m not Arduino or C++ programmer and having hard time to converting this type of code to netmf



This looks like binary values to me but declared as byte array. If I still need byte array in C# how would I use these values?

I do know how to declare a byte array, ::slight_smile: I just don’t understand how to convert the {B01111110,B01000000,B01000000,B01111000,B01000000,B01000000,B01000000,B01000000} to {0x…, 0x…, and so on} :-[

@ cyberh0me - Thanks!. Only one issue: there are tons of them to convert. So here is my own calculator:

or[code=cs]string.Format(“0x{0:x}”, Convert.ToInt64(“01111110”, 2)) // will output 0x7e [/code

1 Like

you can use 0b10101101 representation in VS. Much easier than storing as text and converting.

1 Like

Hm, my VS doesn’t take “0b00011111”-values as integer :think:… There’s also n Convert-class in my NETMF… Am I doing something wrong? In what namespace are those functions?

@ EvoMotors - This is a complete hack, but since the thought came to mind I figured I would share it. You can use the class I generated below to change the C/C++ code from


byte f[8]={B01111110,B01000000,B01000000,B01111000,B01000000,B01000000,B01000000,B01000000};

to


byte[] f = { Binary.B01111110, Binary.B01000000, Binary.B01000000, Binary.B01111000, Binary.B01000000, Binary.B01000000, Binary.B01000000, Binary.B01000000 };

Here is the generated class


static class Binary
{
    public const byte B00000000 = 0x00,
                      B00000001 = 0x01, B00000010 = 0x02, B00000011 = 0x03, B00000100 = 0x04, B00000101 = 0x05, B00000110 = 0x06, B00000111 = 0x07, B00001000 = 0x08, B00001001 = 0x09, B00001010 = 0x0a, B00001011 = 0x0b, B00001100 = 0x0c, B00001101 = 0x0d, B00001110 = 0x0e, B00001111 = 0x0f,
                      B00010000 = 0x10, B00010001 = 0x11, B00010010 = 0x12, B00010011 = 0x13, B00010100 = 0x14, B00010101 = 0x15, B00010110 = 0x16, B00010111 = 0x17, B00011000 = 0x18, B00011001 = 0x19, B00011010 = 0x1a, B00011011 = 0x1b, B00011100 = 0x1c, B00011101 = 0x1d, B00011110 = 0x1e,
                      B00011111 = 0x1f, B00100000 = 0x20, B00100001 = 0x21, B00100010 = 0x22, B00100011 = 0x23, B00100100 = 0x24, B00100101 = 0x25, B00100110 = 0x26, B00100111 = 0x27, B00101000 = 0x28, B00101001 = 0x29, B00101010 = 0x2a, B00101011 = 0x2b, B00101100 = 0x2c, B00101101 = 0x2d,
                      B00101110 = 0x2e, B00101111 = 0x2f, B00110000 = 0x30, B00110001 = 0x31, B00110010 = 0x32, B00110011 = 0x33, B00110100 = 0x34, B00110101 = 0x35, B00110110 = 0x36, B00110111 = 0x37, B00111000 = 0x38, B00111001 = 0x39, B00111010 = 0x3a, B00111011 = 0x3b, B00111100 = 0x3c,
                      B00111101 = 0x3d, B00111110 = 0x3e, B00111111 = 0x3f, B01000000 = 0x40, B01000001 = 0x41, B01000010 = 0x42, B01000011 = 0x43, B01000100 = 0x44, B01000101 = 0x45, B01000110 = 0x46, B01000111 = 0x47, B01001000 = 0x48, B01001001 = 0x49, B01001010 = 0x4a, B01001011 = 0x4b,
                      B01001100 = 0x4c, B01001101 = 0x4d, B01001110 = 0x4e, B01001111 = 0x4f, B01010000 = 0x50, B01010001 = 0x51, B01010010 = 0x52, B01010011 = 0x53, B01010100 = 0x54, B01010101 = 0x55, B01010110 = 0x56, B01010111 = 0x57, B01011000 = 0x58, B01011001 = 0x59, B01011010 = 0x5a,
                      B01011011 = 0x5b, B01011100 = 0x5c, B01011101 = 0x5d, B01011110 = 0x5e, B01011111 = 0x5f, B01100000 = 0x60, B01100001 = 0x61, B01100010 = 0x62, B01100011 = 0x63, B01100100 = 0x64, B01100101 = 0x65, B01100110 = 0x66, B01100111 = 0x67, B01101000 = 0x68, B01101001 = 0x69,
                      B01101010 = 0x6a, B01101011 = 0x6b, B01101100 = 0x6c, B01101101 = 0x6d, B01101110 = 0x6e, B01101111 = 0x6f, B01110000 = 0x70, B01110001 = 0x71, B01110010 = 0x72, B01110011 = 0x73, B01110100 = 0x74, B01110101 = 0x75, B01110110 = 0x76, B01110111 = 0x77, B01111000 = 0x78,
                      B01111001 = 0x79, B01111010 = 0x7a, B01111011 = 0x7b, B01111100 = 0x7c, B01111101 = 0x7d, B01111110 = 0x7e, B01111111 = 0x7f, B10000000 = 0x80, B10000001 = 0x81, B10000010 = 0x82, B10000011 = 0x83, B10000100 = 0x84, B10000101 = 0x85, B10000110 = 0x86, B10000111 = 0x87,
                      B10001000 = 0x88, B10001001 = 0x89, B10001010 = 0x8a, B10001011 = 0x8b, B10001100 = 0x8c, B10001101 = 0x8d, B10001110 = 0x8e, B10001111 = 0x8f, B10010000 = 0x90, B10010001 = 0x91, B10010010 = 0x92, B10010011 = 0x93, B10010100 = 0x94, B10010101 = 0x95, B10010110 = 0x96,
                      B10010111 = 0x97, B10011000 = 0x98, B10011001 = 0x99, B10011010 = 0x9a, B10011011 = 0x9b, B10011100 = 0x9c, B10011101 = 0x9d, B10011110 = 0x9e, B10011111 = 0x9f, B10100000 = 0xa0, B10100001 = 0xa1, B10100010 = 0xa2, B10100011 = 0xa3, B10100100 = 0xa4, B10100101 = 0xa5,
                      B10100110 = 0xa6, B10100111 = 0xa7, B10101000 = 0xa8, B10101001 = 0xa9, B10101010 = 0xaa, B10101011 = 0xab, B10101100 = 0xac, B10101101 = 0xad, B10101110 = 0xae, B10101111 = 0xaf, B10110000 = 0xb0, B10110001 = 0xb1, B10110010 = 0xb2, B10110011 = 0xb3, B10110100 = 0xb4,
                      B10110101 = 0xb5, B10110110 = 0xb6, B10110111 = 0xb7, B10111000 = 0xb8, B10111001 = 0xb9, B10111010 = 0xba, B10111011 = 0xbb, B10111100 = 0xbc, B10111101 = 0xbd, B10111110 = 0xbe, B10111111 = 0xbf, B11000000 = 0xc0, B11000001 = 0xc1, B11000010 = 0xc2, B11000011 = 0xc3,
                      B11000100 = 0xc4, B11000101 = 0xc5, B11000110 = 0xc6, B11000111 = 0xc7, B11001000 = 0xc8, B11001001 = 0xc9, B11001010 = 0xca, B11001011 = 0xcb, B11001100 = 0xcc, B11001101 = 0xcd, B11001110 = 0xce, B11001111 = 0xcf, B11010000 = 0xd0, B11010001 = 0xd1, B11010010 = 0xd2,
                      B11010011 = 0xd3, B11010100 = 0xd4, B11010101 = 0xd5, B11010110 = 0xd6, B11010111 = 0xd7, B11011000 = 0xd8, B11011001 = 0xd9, B11011010 = 0xda, B11011011 = 0xdb, B11011100 = 0xdc, B11011101 = 0xdd, B11011110 = 0xde, B11011111 = 0xdf, B11100000 = 0xe0, B11100001 = 0xe1,
                      B11100010 = 0xe2, B11100011 = 0xe3, B11100100 = 0xe4, B11100101 = 0xe5, B11100110 = 0xe6, B11100111 = 0xe7, B11101000 = 0xe8, B11101001 = 0xe9, B11101010 = 0xea, B11101011 = 0xeb, B11101100 = 0xec, B11101101 = 0xed, B11101110 = 0xee, B11101111 = 0xef, B11110000 = 0xf0,
                      B11110001 = 0xf1, B11110010 = 0xf2, B11110011 = 0xf3, B11110100 = 0xf4, B11110101 = 0xf5, B11110110 = 0xf6, B11110111 = 0xf7, B11111000 = 0xf8, B11111001 = 0xf9, B11111010 = 0xfa, B11111011 = 0xfb, B11111100 = 0xfc, B11111101 = 0xfd, B11111110 = 0xfe, B11111111 = 0xff;
}

1 Like

How about this ?:


public struct Bin
{
    int num;

    private Bin(int i)
    {
        num = i;
    }

    public static implicit operator int(Bin bin)
    {
        return bin.num;
    }
    public static implicit operator byte(Bin bin)
    {
        return (byte)bin.num;
    }
    public static explicit operator Bin(int i)
    {
        if (i < 0 || i > 11111111 || i.ToString().IndexOfAny("23456789".ToCharArray()) > -1)
            throw new ArgumentException("Binary number can only contain 0's or 1's and must be between 0 and 11111111");

        int dec = 0;
        int pow2 = 1;
        do
        {
            if ((i & 1) == 1)
                dec |= pow2;
            i /= 10;
            pow2 <<= 1;
        }
        while (i > 0);
        return new Bin(dec);
    }
    public override string ToString()
    {
        return "0b" + Convert.ToString(num, 2);
    }
}

Usage:

int x = (Bin)01111110;
byte b = (Bin)10101000 ;
byte[] f = { (Bin)01111110, (Bin)01000000, (Bin)01000000, (Bin)01111000, (Bin)01000000, (Bin)01000000, (Bin)01000000, (Bin)01000000 };

1 Like

A few things I would consider
[ol]If this is something that you will be using in general code, loops etc. I would be concerned about performance esp. in .NETMF
If you only need this to populate the static array as a one off, then this would be an overkill.
There is the question of whether this is abuse of operator overloading, I would be interested in hearing other opinions on this. It feels wrong to me, but I might be too conservative.[/ol]

Having said that, I think it is an imaginative solution, I always like seeing imaginative approaches that could potentially open thought processes for other problems. As a thought exercise I think it is pretty cool.

This is exercise.