一个发送消息的功能:
private void button2_Click(object sender, System.EventArgs e)
{
try
{
Byte[] sendByte=new Byte[64];
string send=rtb2.Text+"\r\n";
NetworkStream netStream=new NetworkStream(accSock);
sendByte=System.Text.Encoding.BigEndianUnicode.GetType(send.ToCharArray());//错误在这行:重载GetByte方法未获取"1参数"
netStream.Write(sendByte,0,sendByte.Length);
}
catch
{
MessageBox.Show("连接尚未建立,无法发送!");
}
}
private void button2_Click(object sender, System.EventArgs e)
{
try
{
Byte[] sendByte=new Byte[64];
string send=rtb2.Text+"\r\n";
NetworkStream netStream=new NetworkStream(accSock);
sendByte=System.Text.Encoding.BigEndianUnicode.GetType(send.ToCharArray());//错误在这行:重载GetByte方法未获取"1参数"
netStream.Write(sendByte,0,sendByte.Length);
}
catch
{
MessageBox.Show("连接尚未建立,无法发送!");
}
}
解决方案 »
- C#串口通信中如何接收串口的数据
- 请教各位高手一个问题,关于mvc的
- 内存分配访问无效。 (异常来自 HRESULT:0x800703E6)
- 刚看了个帖子 想了解下.NET开发WAP的问题
- C#里面有类似JAVA的StingBuilder.Reverse()方法吗
- 请问这样的班还有必要上下去吗?
- 怎么样移动到picturebox上的时候鼠标显示一只手
- 如何使设置成主窗体的Form永远都不显示ScorllBar.
- 请问下面这些东西怎么转换成中文
- 给定三个point abc c为ab的交点 求ab之间的角度
- 求助:遇到“指定的参数已超出有效值的范围。参数名: value”问题,请大家帮忙啊,急~~谢谢
- 路径问题?
----------------------
我觉得是char的数组无法转化为Byte数组造成的,不知道对不对
System.Text.Encoding不包含对GetBytes的定义
System.Text.Encoding.BigEndianUnicode.GetBytes( str1 );
Dim encoding As System.Text.Encoding = System.Text.Encoding.BigEndianUnicode
Dim reader As New StreamReader("TextFile.txt", encoding)
Dim line As String = reader.ReadLine()
While Not (line Is Nothing)
Console.WriteLine(line)
line = reader.ReadLine()
End While
你自己研究下咯。
----------------------------------
你说得对,书上写的就是问题很多
string str = "123";
Char[] chars = str.ToCharArray();
int n = chars.Length;
Byte[] bytes = new byte[n];
int i = 0;
foreach(char c in chars)
{
bytes[i] = Convert.ToByte(c);
i ++;
}
System.Text.Encoding.BigEndianUnicode.GetBytes()
Byte[] bytes = System.Text.Encoding.Default.GetBytes(str);
str = System.Text.Encoding.Default.GetString(bytes);