By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

C# (Overflow in Hex? or ASCII?)

P: 1
This C# program is use to convert decimal to hex and send through serial port in ASCII characters. For example: I try to send a string variable in Hex format which i need to convert to a standart ASCII string; eg "70d9b7" where "b7" is the checksum of 2's complement. i have some problem in convert the "d9" and "b7" to the correct ASCII string as the ASCII is overflow from 8 till F, i think... If anyone can help me to debug the problem, i would be very grateful... Thanks a lot...


# region using directive
using System;
using System.IO.Ports;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
# endregion

namespace WindowsApplication1
{
public partial class Form1 : Form
{

SerialPort sp = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
int Val1, Val2;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private string HexAsciiConvert(string hexValue1)
{
StringBuilder sb = new StringBuilder();

for (int i = 0; i <= hexValue1.Length - 2; i += 2)
{
sb.Append(Convert.ToString(Convert.ToChar(Int32.Pa rse(hexValue1.Substring(i, 2), System.Globalization.NumberStyles.HexNumber))));
}

for (int i = 8; i <= hexValue1.Length - 2; i++)
{
sb.Append(Convert.ToChar(Int32.Parse(hexValue1, System.Globalization.NumberStyles.HexNumber)));
}

return sb.ToString();

}

private void button3_Click(object sender, EventArgs e)
{
Val1 = Val2 = 0;

if (H0_1.Checked == true)
{
Val1 = Val1 + 1;
}

if (H1_1.Checked == true)
{
Val1 = Val1 + 2;
}

if (H2_1.Checked == true)
{
Val1 = Val1 + 4;
}

if (H3_1.Checked == true)
{
Val1 = Val1 + 8;
}

if (H4_1.Checked == true)
{
Val1 = Val1 + 16;
}

if (H5_1.Checked == true)
{
Val1 = Val1 + 32;
}

if (H6_1.Checked == true)
{
Val1 = Val1 + 64;
}

if (H7_1.Checked == true)
{
Val1 = Val1 + 128;
}

if (D0.Checked == true)
{
Val2 = Val2 + 1;
}

if (D1.Checked == true)
{
Val2 = Val2 + 2;
}

if (D2.Checked == true)
{
Val2 = Val2 + 4;
}

if (D3.Checked == true)
{
Val2 = Val2 + 8;
}

if (D4.Checked == true)
{
Val2 = Val2 + 16;
}

if (D5.Checked == true)
{
Val2 = Val2 + 32;
}

if (D6.Checked == true)
{
Val2 = Val2 + 64;
}

if (D7.Checked == true)
{
Val2 = Val2 + 128;
}


{
textread.Text = "";


int checksum = (byte)~(Val1 + Val2) + 1;
string a = checksum.ToString("x");

string hexValue1;
string hexValue2;

if ( Val1< 16)
{
hexValue1 = "0" + Val1.ToString("x");
}

else

hexValue1 = Val1.ToString("x");


if (Val2< 16)
{
hexValue2 = "0" + Val2.ToString("x");
}

else

hexValue2 = Val2.ToString("x");

textread.Text = hexValue1 + hexValue2 + a;
}


}

private void button1_Click(object sender, EventArgs e)
{
string hexValue1;
string hexValue2;

if (Val1 < 16)
{
hexValue1 = "0" + Val1.ToString("x");
}

else

hexValue1 = Val1.ToString("x");

if (Val2 < 16)
{
hexValue2 = "0" + Val2.ToString("x");
}

else

hexValue2 = Val2.ToString("x");


int checksum = (byte)~(Val1 + Val2) + 1;
string a = checksum.ToString("x");

hexValue1 = HexAsciiConvert(hexValue1);
hexValue2 = HexAsciiConvert(hexValue2);
a = HexAsciiConvert(a);

textsend.Text = hexValue1 + hexValue2 + a;

sp.Open();

sp.Write(textsend.Text);

sp.Close();

}



emily
May 30 '07 #1
Share this Question
Share on Google+
2 Replies


P: 79
Converting decimal to hex shouldn't be to much trouble.
The second part is converting the hex to ASCII and sending it.
Each hex digit can be represented on 4 bits, so an ASCII character which takes up 8 bits can store 2 hex digits.

So, if you have let's say 2AC1 in hex, that means 0010101011000001 in binary which results in two ASCII characters: *┴(codes 42 and 193 in decimal).
May 30 '07 #2

Plater
Expert 5K+
P: 7,872
integers have their .ToString() overloaded to take in "X" which will output a string representation of the number in hex

[code]
int a=255;
string myhex=a.ToString("X");
//myhex is now "FF"

Likewise, the int.Parse() is overloaded to take in a string representation of a number in hex digits.
Aug 1 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.