Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
I am trying to send commands to a machine

The machine accepts the packet with a byte array...

I need to build a form that will allow the user to enter the command in char format and then convert it to a byte array for example

I can send the machine

byte[] packet = 
{
((byte) (0x02)),
((byte) (0x05)),
((byte) (0xFF)),

};


I need the user to be able to input for example "0x02 0x07 0x03 0xFF"
and the string from the textbox.text be converted into a byte array with the for 4 byte values that represent the ansi format

I tried without success:

C#
byte[] packet =
{
((byte) (string1)),
((byte) (string2)),
((byte) (string3)),


OP's Answer moved to here:
tried to google it already and frustrating that I can't find anything.
if I use the code from the above link, it interprets each digit and letter with a value
for the example below the byte array had 14 items. I need to get the 3 values. I would probably deliminate the string by spaces and then get the values for each one
In this case
0x02= 2
0xFF = 255
0x03 = 3
or a way that I can get the string and convert the literal string "0x02" and add it to to a byte array with its byte value

System.Text.Encoding enc = System.Text.Encoding.ASCII;
byte[] myByteArray = enc.GetBytes("0x02 0xFF 0x03");
Posted
Updated 16-May-11 11:16am
v3
Comments
yesotaso 16-May-11 16:00pm    
I see plus plus everywhere :)
Keith Barrow 16-May-11 16:27pm    
That's nothing, I see dead pointers. :)
yesotaso 16-May-11 16:36pm    
I simply cannot find words to express how much I rofl having read that Mr. Barrow. Seriously, nice one :)
fjdiewornncalwe 16-May-11 17:15pm    
I spy with my little eye something that is...
Seriously, though. Look at John's answer.
yesotaso 16-May-11 17:25pm    
I respectfully agree with Mr. Simmons. On the other hand, to get things working sometimes it takes little more than just googles.

Checkout:
1- String.Split Method (Char[])[^]
2- Byte.Parse Method (String, NumberStyles)[^] (System.Globalization.NumberStyles.HexNumber for instance for Hex parsing)
3- For further practice System.BitConverter[^] which seems redundant for question but maybe usefull if you are going to use byte stuff.

Edit:
This should work for you: (2nd argument of ToByte is base of converted number)
C#
string input = "0x02 0x07 0x03 0xFF";
            return input.Split(' ').Select(s => Convert.ToByte(s, 16)).ToArray();
 
Share this answer
 
v4
Comments
Albin Abel 16-May-11 19:25pm    
Good one. My 5
yesotaso 16-May-11 20:30pm    
Thanks.
Kim Togo 17-May-11 6:41am    
Nice use of LINQ :-), my 5.
yesotaso 17-May-11 14:24pm    
Thanks.
roman_s 17-May-11 10:31am    
Works Great returns back the byte values in the bit array..

instead of using the method to return the array list I just assigned a byte array list to the string select/linq method

string input = "0x02 0x07 0x03 0xFF";
byte[] bArray = input.Split(' ').Select(s => Convert.ToByte(s, 16)).ToArray();

Thanks to everyone for their help, also the links above were great to learn more about our little value type byte.
http://geekswithblogs.net/TimH/archive/2005/06/27/44849.aspx[^]

Google is free, and ready for you to use.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900