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.Threadin g;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
# endregion
namespace WindowsApplicat ion1
{
public partial class Form1 : Form
{
SerialPort sp = new SerialPort("COM 2", 9600, Parity.None, 8, StopBits.One);
int Val1, Val2;
public Form1()
{
InitializeCompo nent();
}
private void Form1_Load(obje ct sender, EventArgs e)
{
}
private string HexAsciiConvert (string hexValue1)
{
StringBuilder sb = new StringBuilder() ;
for (int i = 0; i <= hexValue1.Lengt h - 2; i += 2)
{
sb.Append(Conve rt.ToString(Con vert.ToChar(Int 32.Parse(hexVal ue1.Substring(i , 2), System.Globaliz ation.NumberSty les.HexNumber)) ));
}
for (int i = 8; i <= hexValue1.Lengt h - 2; i++)
{
sb.Append(Conve rt.ToChar(Int32 .Parse(hexValue 1, System.Globaliz ation.NumberSty les.HexNumber)) );
}
return sb.ToString();
}
private void button3_Click(o bject 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.ToStri ng("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(o bject 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.ToStri ng("x");
hexValue1 = HexAsciiConvert (hexValue1);
hexValue2 = HexAsciiConvert (hexValue2);
a = HexAsciiConvert (a);
textsend.Text = hexValue1 + hexValue2 + a;
sp.Open();
sp.Write(textse nd.Text);
sp.Close();
}
emily
2 2029
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 001010101100000 1 in binary which results in two ASCII characters: *┴(codes 42 and 193 in decimal).
Plater 7,872
Recognized Expert Expert
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.ToStrin g("X");
//myhex is now "FF"
Likewise, the int.Parse() is overloaded to take in a string representation of a number in hex digits.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 ?.
"scanf("%s",start->acNome);"
>>guarantee for hackers to kontaminate your mashine with >>viruses of any
kind. Don't use scanf or buffer overflow >>ruins your mashine.
|
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 efficient as possible without resorting to
assembly language, and without causing undefined behavior. That, of course,
means catching the overflow before it happens.
What I asked was (stripping any relevance to C++):
|
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
|
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.
Further study reveals that the extra orders are duplicated.
I'm using synchronized way, here is my code,
....
client.SetSocketOption(SocketOptionLevel.Socket,
|
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 something;
| |
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 particular node
of my logically partitioned db having 8 partitions.
Current number of tablequeue buffers overflowed = 147737
Total number of tablequeue buffers overflowed = 190224
Maximum number of tablequeue buffers overflowed = 189683
...
|
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
integer scope smaller than MAXINT,so I want to seek a measure to test
the excess without the value's comparing to MAXINT. Please let me know
if you have a good idea.
THX.
|
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 style="overflow:hidden;width: 355px;">Header1</div></td>
<td><div style="overflow:hidden;width: 100px;">Header2</div></td>
<td><div style="width: 100%;overflow:hidden;">Header3</div></td>
</tr>
|
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 type 'long' is used.
That way native C speed can be reached.
Now I want to experiment with raising a Seed7 exception
(which is emulated with setjmp(), longjmp() in C) for integer
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |