Anyone have a clever hex->byte conversion routine?
#1
Posted 06 December 2010 - 02:58 AM
#2
Posted 06 December 2010 - 03:42 PM
byte b = (byte)Convert.ToInt32("ff", 16);
#3
Posted 07 December 2010 - 09:52 PM
#4
Posted 10 December 2010 - 02:02 AM
#5
Posted 10 December 2010 - 03:01 AM
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
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
Posted 10 December 2010 - 01:07 PM
#8
Posted 19 January 2011 - 03:32 PM
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
Posted 19 January 2011 - 04:07 PM
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
Posted 19 January 2011 - 05:39 PM
#11
Posted 19 January 2011 - 05:45 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users