Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Anyone have a clever hex->byte conversion routine?


  • Please log in to reply
11 replies to this topic

#1 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 06 December 2010 - 02:58 AM

it doesn't appear that netmf supports int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber); .. so does anyone have a clever hex to byte (or int or whatever) conversion routine handy, so that if I feed it the string "ff" it returns the number 255 in some form? Thanks!!

#2 Chuckie

Chuckie

    New Member

  • Members
  • Pip
  • 6 posts

Posted 06 December 2010 - 03:42 PM

byte b = (byte)Convert.ToInt32("ff", 16);


#3 Frank26080115

Frank26080115

    Member

  • Members
  • PipPip
  • 22 posts

Posted 07 December 2010 - 09:52 PM

Question, since the ARM7 is 32 bit, is Convert.ToInt32 more efficient than Convert.ToInt8 ?

#4 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 10 December 2010 - 02:02 AM

What namespace is that under? I'm thinking the netduino does not support Convert.ToInt32?

#5 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 10 December 2010 - 03:01 AM

I tried really hard to be clever and this is what I came up with:

        public static byte[] GetByteArray(string s)
        {
            return System.Text.Encoding.UTF8.GetBytes(s);
        }
        public static byte GetByteFromHex(string s)
        {
            return GetByteFromHex(GetByteArray(s));
        }
        public static byte GetByteFromHex(byte[] ba)
        {
            return (byte)((((ba[0] > 96) ? ba[0] - 87 : ba[0] - 48) * 16) + ((ba[1] > 96) ? ba[1] - 87 : ba[1] - 48));
        }


#6 Chuckie

Chuckie

    New Member

  • Members
  • Pip
  • 6 posts

Posted 10 December 2010 - 05:51 AM

What namespace is that under? I'm thinking the netduino does not support Convert.ToInt32?


It's supported on netduino. I've been using this for easy hex string -> int -> byte conversion on both the netduino and the GHI boards. The System.Convert class is in mscorlib.dll but "hidden" in the IDE. You won't see it pop up in intellisense because of the way the EditorBrowsable attribute is configured for the System.Convert class in NETMF:

[EditorBrowsable(EditorBrowsableState.Never)]
public static class Convert
{
    ...
    public static int ToInt32(string hexNumber, int fromBase);
    ...
}

That attribute value makes the Convert class not show up when you start typing in the IDE, but it is still available for you to use. If you just type "System.Convert.", you will see that the helpful .ToXXXX() methods are there for you to use...the Convert class is hidden, but its methods are not. I'm not sure why this useful class is concealed from the editor...maybe there is a good reason for it.

#7 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 10 December 2010 - 01:07 PM

Thank you for the explanation, Chuckie. It seems hard enough for me to find stuff normally, and now I'm up against hidden stuff?? :unsure:

#8 nturpin77

nturpin77

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationNewfoundland, Canada

Posted 19 January 2011 - 03:32 PM

Gday

I tried using the ToInt32 routine and I mustve not used it properly because VC is po-ed at me. Basically Im taking a series of HEX values from a Pitch Roll IMU and I want to convert them to "normal" numbers LOL. See attached code and sample data.

The error Im getting on the Convert line is "the best overloaded method match for 'System.Convert.ToInt32(string,int)' has some invalid arguments." Not sure what this means but Im sure its nothing good.

int CHRtoRead = serialPort2.BytesToRead;
                if (CHRtoRead > 0)
                {
                    //blink the LED to show we got data
                    led.Write(true);
                    Thread.Sleep(100);
                    led.Write(false);
                    // get the waiting data
                    byte[] CHRbuffer = new byte[CHRtoRead];
                    serialPort2.Read(CHRbuffer, 0, CHRbuffer.Length);
                    byte CHRData = (byte)Convert.ToInt32(CHRbuffer,16);
                    
                    serialPort1.Write(CHRbuffer, 0, CHRbuffer.Length);

                }

Please disregard the GPS string since Im using a HEX view in my terminal com viewer to view the HEX data from the IMU so it chops up my GPS data nicely LOL

Sample Data


24 47 50 47 47 41 2c 31 35 33 30 34 30 2e 32 30 $GPGGA,153040.20
30 2c 34 37 33 35 2e 31 33 33 30 2c 4e 2c 30 35 0,4735.1330,N,05
32 34 34 2e 32 32 38 31 2c 57 2c 31 2c 39 2c 30 244.2281,W,1,9,0
2e 39 34 2c 31 30 32 2e 35 2c 4d 2c 31 34 2e 34 .94,102.5,M,14.4
2c 4d 2c 2c 2a 34 36 0d 0a 73 6e 70 b7 20 ff fe ,M,,*46..snp· ÿþ


Thanks

#9 Chuckie

Chuckie

    New Member

  • Members
  • Pip
  • 6 posts

Posted 19 January 2011 - 04:07 PM

The build error is because you're passing the wrong data type in the first parameter to Convert.ToInt32():

byte CHRData = (byte)Convert.ToInt32(CHRbuffer,16);

CHRbuffer is a byte array, but you need to pass a string instead. If the device is sending you a string of hex values, you need to split up that string into individual hex byte representations ("24", "47", "50", etc.) and pass each one to Convert.ToInt32(). Hope that makes sense.

byte b = (byte)Convert.ToInt32("24", 16);  // b = 36


#10 nturpin77

nturpin77

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationNewfoundland, Canada

Posted 19 January 2011 - 05:39 PM

Yeah that makes sense for sure. So basically what Ive ineffectively tried to do is pass the WHOLE array full of HEX data to try and be converted all at once and what I really need to do is pass each hex value to its own string and process the data from there?

#11 Chuckie

Chuckie

    New Member

  • Members
  • Pip
  • 6 posts

Posted 19 January 2011 - 05:45 PM

Exactly.

#12 asciiman

asciiman

    Advanced Member

  • Members
  • PipPipPip
  • 56 posts
  • LocationTurkey

Posted 29 January 2014 - 06:05 PM

hi sir thx  

Chuckie

 

Stupid Microsoft






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.