Click here to Skip to main content
16,014,113 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace checksum
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      public static byte[] FromHex(string hex)
      {
         hex = hex.Replace("-", "");
         byte[] raw = new byte[hex.Length / 2];
         for (int i = 0; i < raw.Length; i++)
         {
            raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
         }
         return raw;
      }

      private string CalculateChecksum(byte[] str)
      {
         int check = 0;
         foreach (byte chData in str)
         {
            check += chData;
         }
         check &= 0xff;
         return check.ToString("X2");
      }

      private void textBox1_TextChanged(object sender, EventArgs e)
      {

      }

      private void button1_Click(object sender, EventArgs e)
      {
         string hex;
         byte[] str;
         hex = textBox1.Text;
         str = FromHex(hex);

         textBox2.Text = CalculateChecksum(str);
      }

      private void textBox2_TextChanged(object sender, EventArgs e)
      {

      }
   }
}
Posted
Updated 15-Oct-14 23:00pm
v3
Comments
BillWoodruff 16-Oct-14 3:15am    
"Now I Need To Add When I Input Any Whitespace Or Comma" What does this mean ? Add what ? How does white-space of comma become part of a byte[] ?
Richard MacCutchan 16-Oct-14 4:25am    
Just take the text as is rather than using the conversion from hex to byte.
Member 11145129 16-Oct-14 4:29am    
it's not working ....showing 8 errors
Richard MacCutchan 16-Oct-14 4:36am    
What is not working, what errors, what code?
Member 11145129 16-Oct-14 4:46am    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace checksum
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

public static byte[] FromHex(string hex)
{




byte[] raw = new byte[hex.Length / 2];
for (int i = 0; i < raw.Length; i++)
{
raw[i] =(hex.Substring(i * 2, 2), 16);
}
return raw;
}


private string CalculateChecksum(byte[] str)
{
int check = 0;
foreach (byte chData in str)
{
check += chData;
}
check &= 0xff;
return check.ToString("X2");
}


private void button1_Click(object sender, EventArgs e)
{
string hex= textBox1.Text;

byte[] str;
str = FromHex(hex);
textBox2.Text = CalculateChecksum(str);
}



private void textBox2_TextChanged(object sender, EventArgs e)
{



}
}
}

I don't think you have quite understood what the difference is between Hex and Byte:
You probably don't want to do have anything to do with Hex as far as your input side is concerned: you want to use the entered data directly as Byte values.
So try just converting the input string to a byte array, and use that instead:
C#
string s = ...
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s);
 
Share this answer
 
Comments
BillWoodruff 16-Oct-14 6:25am    
I think the OP wants to parse a string entered in hexadecimal format in a TextBox to create a byte[], treating each two-char pair as a hex expression of a byte. Why the OP wants to tolerate entering any old character into the TextBox ... a mystery :)
Why not save yourself a lot of trouble and only allow the user to enter valid characters in the TextBox ? Like this:
C#
private string legalHex = "0123456789abcdefABCDEF";

private Keys legalKeys = Keys.Space | Keys.Back | Keys.Delete;

private void HexTextBox_KeyDown(object sender, KeyEventArgs e)
{
    e.SuppressKeyPress = ! (legalHex.Contains((char) e.KeyValue) || legalKeys.HasFlag(e.KeyCode));
}
Note that this allows space characters to be entered, so you'd need to clean-up the string before processing it by removing all space characters:
C#
private void btnProcessHexString_Click(object sender, EventArgs e)
{
    string currentHexString = HexTextBox.Text.Replace(" ", String.Empty);

    if (currentHexString.Length%2 != 0)
    {
        MessageBox.Show("you have not entered an even number of characters);

       return;
    }

    // process legal hex string
}
 
Share this answer
 
v2

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