Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have a textbox and want to have a serial data transmission.
i write a number like 610 on textbox, and want to send this number as 2 bytes by serial port.
how can i convert this textbox.Text to 2 bytes?
and next question, how can i add different values? for example, i have 0xAA, 0xBB, ... and 2 bytes that reached from conversion 610 to 2 bytes.
Posted
Comments
BillWoodruff 29-Oct-13 3:07am    
If you start doing some basic study of C# .NET fundamentals, and such essential areas as numeric representation, and number conversion: then, in the future, you will become an independently productive programmer.

Learning how to look-up, and access, basic information, quickly, is an essential skill for any programmer who wishes to become ... and stay ... productive, because: the tools we use are constantly changing.

If you continue to ask a question here for every single line of code in every program you write, you will never become productive.

There is no substitute for making your own intellectual effort.

You may think I am personally "attacking" you by saying this, but that is not my intent. I, and many other people here, who have had (or have) successful careers as programmers, want to help other programmers become successful; we want to educate.

Just providing code-fixes, and writing other people's programs, is not education.

Think about it. Bill
Naresh1277 29-Oct-13 3:30am    
Hey Bill, I understand, and I will sincerely try to do more programing in the basics of C#.Net

Thanks for your Concern!!
I guess this should be the first motto for every individual associate in the CodeProject
BillWoodruff 29-Oct-13 3:46am    
Naresh, Helping other people on CP develop as programmers is a great way to continually improve, and refine, your own knowledge ! Your answers are thoughtful, and helpful.

"rahi gulzar to phool khilenge" Kabir
mohsenvasefi 29-Oct-13 3:26am    
i know what you say, but i haven't any time and must finish this project!
BillWoodruff 29-Oct-13 3:47am    
That's understandable ! I'll try to shut-up, and be helpful :)

C#
public static byte[] StringToByteArray(String hex)
{
  int NumberChars = hex.Length;
  byte[] bytes = new byte[NumberChars / 2];
  for (int i = 0; i < NumberChars; i += 2)
  bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  return bytes;
 }
 
Share this answer
 
Comments
mohsenvasefi 29-Oct-13 2:27am    
thanks, could you explain how does it work?
C#
var myBuffer = new byte[20];

myBuffer = ASCIIEncoding.ASCII.GetBytes(textBox.Text);
 
Share this answer
 
Comments
mohsenvasefi 29-Oct-13 2:27am    
thanks, could you explain how does it work? and how can i add this mybuffer by other bytes as hex(0xAA+0xBB+...+bytes of mybuffer)?
Naresh1277 29-Oct-13 3:24am    
you have do R&D on it ..
mohsenvasefi 29-Oct-13 3:34am    
what's R&D?
Naresh1277 29-Oct-13 3:25am    
where are you using this serial transmission , I mean is it used in NetworkPrograming
mohsenvasefi 29-Oct-13 3:37am    
not network programming, i'm using it in transfer data between micro controller and PC. i want to make CRC by add bytes as said
I think Naresh's answers gave you what you need, but I'll add a few comments:

1. a number is independent of its representation in .NET. if you write:
int Y = 0xAA + 0xBB; // the value of Y is #357 decimal, &x0165 in hex
2. you can easily get the hex representation of a number as a string:
// see ToString() documentation
// "X" means hex values > 9 shown in upper-case
// "4" means the representation will be 4 characters long
string hexOfY = 357.ToString("X4");
3. going from a TextBox string to a hex value is easy:
C#
string HexFromTextBox = Convert.ToInt32(textBox1.Text).ToString("X4");

// much better practice to use TryParse 
// to make sure you've got a valid entry:

Int16 tbValue;

bool okay16BitValue = Int16.TryParse(textBox1.Text, out tbValue);

// error !
if (! okay16BitValue) throw new ArgumentException("TextBox string not a 16-bit integer");

// now tbValue has a valid 16bit integer you can convert to a hex string if you wish
4. to get the high and low bytes of a 16-bit number > 255 and < 65536:
C#
// use of bit-shift
byte highByte = (byte)(357 >> 8);

// use of bit-wise and
byte lowByte = (byte)(357 & 0xff);
 
Share this answer
 
v3
Comments
mohsenvasefi 29-Oct-13 4:22am    
excellent. i think i can do very well it. can i have your YAHOO! ID for more relationship?
mohsenvasefi 29-Oct-13 6:04am    
thank you so much. you gave me a good method for conversion between byte and int and string :D ;)

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