本文介绍了阿拉伯语单词不会转换为字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有告诉你我是伊拉克人,当然我在我的应用程序中写了一些阿拉伯语。

我的应用程序是聊天服务器 - 客户端。如你所知,在TCP发送之前,我们应该将字符串转换为字节数组。



Hi, I didn't tell you that I'm an Iraqi and ofcourse I write some arabic in my application.
My application is chat server-client. As you know that before TCP sending we should translate string to array of bytes.

byte[] DataBytes = new byte[Message.Length + ("<EOP>").Length + 1];
for (int x = 0; x < Message.Length; x++)
{
    DataBytes[x] = Convert.ToByte(Message[x]);
}

DataBytes[Message.Length + 1] = Convert.ToByte('<');
DataBytes[Message.Length + 2] = Convert.ToByte('E');
DataBytes[Message.Length + 3] = Convert.ToByte('O');
DataBytes[Message.Length + 4] = Convert.ToByte('P');
DataBytes[Message.Length + 5] = Convert.ToByte('>');

clientSocket.Send(DataBytes);





当我写一个英文单词(如:Hello)并发送它...发送成功。

但是当我写一个阿拉伯语单词(如:هلو)并发送它时...发生错误告诉我对于无符号字节,值太大或太小。



这是什么意思???

如何解决?



When I wrote an English word (like: Hello) and send it... sending is succeeded.
But When I wrote an Arabic word (like: هلو) and send it... an error occurred tells me that "Value was either too large or too small for an unsigned byte".

What this mean???
And how to solve it???

推荐答案

using System.Text;

byte[] bytes = Encoding.UTF32.GetBytes(iString);


string s = ... // Your string source here
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);

但我不保证。如果您使用不同的编码,它可能会有效。

But I don't guarantee it. It may work if you use a different encoding.


这篇关于阿拉伯语单词不会转换为字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 21:48