Click here to Skip to main content
16,016,605 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to send 26 byte hex value using textbox through serialport(rs232)/rs422
Posted
Updated 10-Jan-16 21:55pm
v2
Comments
Sinisa Hajnal 11-Jan-16 2:41am    
Do you mean you want to set the length on the control? Or really a number 26? In hexadecimal?! Please explain further. Thank you.
Siddaram K.Patil 11-Jan-16 2:43am    
Actually i want to send 26 byte hex value through serialport or comport
Richard MacCutchan 11-Jan-16 3:20am    
All characters are already hex values. Please edit your question and add a properly detailed explanation of what you are trying to do.
Siddaram K.Patil 11-Jan-16 3:26am    
actually i want to send 26 byte hex value using textbox through serialport or comport communication
Richard MacCutchan 11-Jan-16 3:47am    
So what part of Please edit your question and add a properly detailed explanation of what you are trying to do. did you fail to understand? I guess, all of it.

1 solution

Looking at this from the point of view of conversion of a string in hex format to byte[] and conversion of byte[] to hex format string with the requirement to optionally use a format where hex bytes are separated by dashes:
C#
private static string dash = "-";

private static byte[] ByteAryFromHexString(string hx)
{
    // has dashes ? delete them
    if (hx.Contains(dash)) hx = hx.Replace(dash, "");
    
    // unnecessary ?
    hx = hx.ToUpperInvariant();
    
    byte[] result = new byte[hx.Length / 2];
    
    for (int i = 0; i < result.Length; i++)
    {
     result[i] = Convert.ToByte(hx.Substring(i*2,2), 16);
    }
    
    return result;
}

// the 'withDash parameter controls whether dashes are in the result : it's set to 'false by default
private static string HexStringFromByteArray(byte[] bytes, bool withDash = false)
{
    var result = BitConverter.ToString(bytes);
    
    if (withDash) return result;
    
    return (result.Replace(dash, ""));
}
Test:
C#
string hx1 = @"AA-55-55-55-15-55-6A-AA-FF-FF-00-00-FF-FF-00-10-00-06-08-8F-00-E2-A0-80-08-D3";

string hx2 = @"AA55555515556AAAFFFF0000FFFF00100006088F00E2A08008D3";

private void Test()
{
    // set break-point on next line
    // and single-step through the code

    byte[] b1 = ByteAryFromHexString(hx1);
    byte[] b2 = ByteAryFromHexString(hx2);

    // do b1 and b2 have identical values ?
    string enc1 = Encoding.UTF8.GetString(b1);
    string enc2 = Encoding.UTF8.GetString(b2);
    bool i1 = enc1 == enc2;

    string s1 = HexStringFromByteArray(b1, true);
    string s2 = HexStringFromByteArray(b2, false);

    // s1 and s2 should not be the same !
    bool i2 = s1 =!= s2;
}
Note: Hex format String to Byte[] conversion is one place .NET doesn't really offer a really fast built-in conversion. There is a great deal of debate about the fastest way to achieve that: you can find extensive discussions, and performance measurements, of various methods on threads on StackOverFlow like this one: [^].
 
Share this answer
 
v3
Comments
[no name] 11-Jan-16 12:16pm    
A 5 for your help. Personally I think converting this size of packages will usually not become a bottle neck in Serial communication. But good you mentioned it, who knows whether same Serial protocol will be used in future by usb2serial and maybe other package sizes.
BillWoodruff 11-Jan-16 12:37pm    
Thanks !

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