473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

C# (Overflow in Hex? or ASCII?)

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
2 1997
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
7,872 Expert 4TB
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

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

Similar topics

11
by: | last post by:
Hi all...this is a great forum, In one of my posts, someone tell me that is more secure use input function with 'A field-width specifier'... So my question, which function i should use ?. ...
27
by: REH | last post by:
I asked this on c.l.c++, but they suggested you folks may be better able to answer. Basically, I am trying to write code to detect overflows in signed integer math. I am trying to make it as...
7
by: wij | last post by:
Hi: Is there better way of detecting multiplication overflow for type long than by using double precision of lldiv to verify the result? Thanks in advance. I.J.Wang
3
by: Chan | last post by:
A TCP connection to retrieve a customer's order data. While the sender's sending the same list of order data, say 10 orders, data picked up from receiving buffer can be different and keep changing....
25
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do...
0
by: Sam Durai | last post by:
Hello, A particular select query took unusually long time to execute hence I took an app.snap to find out what happens internally and I found out that tablequeue buffers are overflowing on a...
8
by: starffly | last post by:
In my program, the caculated value is supposed to be no more than the constant named MAXINT,otherwise, overflow error will be informed.however, I cannot test if the value exceeds MAXINT within the...
2
by: lexor | last post by:
Hi! I'm struggling with a strange IE behavior regarding tables. I have a table like the following one <table cellpadding="0" cellspacing="0" border="1" width="500"> <tr> <td><div...
42
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.