从文本框转换并添加差异值 [英] convert from textbox and add difference values

查看:104
本文介绍了从文本框转换并添加差异值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
i有一个文本框,想要进行串行数据传输。

i在文本框上写一个像610这样的数字,并希望通过串口发送这个数字为2个字节。 br />
如何将此textbox.Text转换为2个字节?

和下一个问题,我该如何添加不同的值?例如,我有0xAA,0xBB,...和从转换610到2个字节的2个字节。

解决方案

 < span class =code-keyword> 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 / < span class =code-digit> 2 ] = Convert.ToByte(hex.Substring(i, 2 ), 16 );
return 个字节;
}


  var  myBuffer =  new   byte  [ 20 ]; 

myBuffer = ASCIIEncoding.ASCII.GetBytes(textBox.Text);


我认为Naresh的答案给了你所需要的,但是我将添加一些评论:



1.数字与其在.NET中的表示无关。如果你写:

 int Y = 0xAA + 0xBB; // Y的值是#357十进制,& x0165十六进制

2。您可以轻松地将数字的十六进制表示形式作为字符串:

 //请参阅ToString()文档
//X表示十六进制值> 9以大写字母显示
//4表示表示长度为4个字符
string hexOfY = 357.ToString(X4);

3。从TextBox字符串转换为十六进制值很容易:

  string  HexFromTextBox = Convert.ToInt32(textBox1.Text).ToString(  X4); 

// 更好的练习使用TryParse
// 以确保您有一个有效的条目:

Int16 tbValue;

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

// 错误!
if (!okay16BitValue) throw new ArgumentException( TextBox字符串不是16位整数);

// 现在tbValue有一个有效的16位整数,你可以转换为十六进制字符串,如果你希望

4。获得16位数字的高字节和低字节> 255和< 65536:

  //  使用位移 
< span class =code-keyword> byte highByte =( byte )( 357 > ;> 8 );

// 按位使用和
byte lowByte =( byte )( 357 & 0xff的);


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.

解决方案

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;
 }


var myBuffer = new byte[20];

myBuffer = ASCIIEncoding.ASCII.GetBytes(textBox.Text);


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:

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:

// use of bit-shift
byte highByte = (byte)(357 >> 8);

// use of bit-wise and
byte lowByte = (byte)(357 & 0xff);


这篇关于从文本框转换并添加差异值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆