how to convert string to byte array in C#

Status
Not open for further replies.

ep.hobbyiest

Full Member level 4
Joined
Jul 24, 2014
Messages
212
Helped
1
Reputation
2
Reaction score
1
Trophy points
18
Visit site
Activity points
1,487
how to convert the string to byte array.
i have string with content as "Edaboard"
and i want that string in SerialDataBuffer[] array. so to convert this string in the byte array form.
 

is this byte array i can send over USB now?

- - - Updated - - -

Recently i tried this one
Code:
string data = "EDAboard";
 byte[] SerialDataBuffer = Encoding.ASCII.UTF8.GetBytes(data);

but this is not work....
 

what happens?

you need to copy the serialDataBuffer to the USB outbuffer, e.g. something like
Code:
OUTBuffer[0] = 0x00;	//The first byte is the "Report ID" and does not get sent over the USB bus.  Always set = 0.
OUTBuffer[1] = 0x37;	//READ_POT command (see the firmware source code), gets 10-bit ADC Value
 for (uint i = 2; i < 65; i++)
     OUTBuffer[i] = SeralDataBuffer[i-2];
if (WriteFile(WriteHandleToUSBDevice, OUTBuffer, 65, ref BytesWritten, IntPtr.Zero))	//Blocking function, unless an "overlapped" structure is used
the first byte is not sent, the second is a command to the target (in this case READ_POT) the rest is data to send
 
now if i entered string in textbox then how to get it in array?
i tried above methods now its working. i do this same way as above but not working.
 

i tried this one also but not doing anything.
Other things working good like send string after clicking one button abd reading ADC.
But only this task giving problem.
 

can you print out the array contents to the console to check what is happening, e.g.
Code:
           byte[] bArray = Encoding.ASCII.GetBytes(textBox1.Text);
            for (int i=0; i < bArray.Length; i++)
                Console.WriteLine(" {0}", bArray[i]);
it should print the ASCII code of the characters, e.g. abc gives 97 98 99
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…