Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, January 20, 2020 6:56 PM
I need to convert the data in the Text box into a HEX number and send it through serial port.
Here is the code to send through Serial Port that I am using. port.Write(textBox1.Text);
If I enter 1000 in the text box, I see in the serial terminal program HEX values 31 30 30 30. Which is correct.
This data will be read by the micro-controller. I need to convert this number into Hex format before sending and send only 2 bytes 03 E8.
Please help. Thank you.
All replies (3)
Monday, January 20, 2020 8:17 PM
Your text box provides ASCII characters. You need to individually convert each character to a Hex string. https://www.includehelp.com/c/convert-ascii-string-to-hexadecimal-string-in-c.aspx provides an example.
Marty G.
Tuesday, January 21, 2020 2:17 AM
Hi RAFFI SEMERJIAN,
Welcome to MSDN forum.
You could refer to codes below and check if it works.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string input = "1000";
char[] values = input.ToCharArray();
foreach (char letter in values)
{
// Get the integral value of the character.
int value = Convert.ToInt32(letter);
// Convert the integer value to a hexadecimal value in string form.
Console.Write($"{value:X}" + $" ");
}
Console.WriteLine();
Console.ReadKey();
}
}
}
Hope this could help you and any feedback will be expected.
Best Regards,
Tianyu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Monday, February 3, 2020 10:44 AM
Hi RAFFI SEMERJIAN,
Sorry to disturb you.
For your requirement, you could refer this sample to implement it:
class Sample
{
SerialPort serialport = new SerialPort();
void Main(string[] args)
{
string input = "1000";
string hexstring = StringToHexString(input, Encoding.UTF32);
byte[] sendvalue = strToToHexByte(hexstring);
serialport.Write(sendvalue, 0, sendvalue.Length);
Console.ReadKey();
}
private string StringToHexString(string s, Encoding encode)
{
byte[] b = encode.GetBytes(s);
string result = string.Empty;
for (int i = 0; i < b.Length; i++)
{
result += " " + Convert.ToString(b[i], 16);
}
return result;
}
private static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
}
Hope it could help you.
Best Regards,
Dylan
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected]