我有一个从缓冲区获取数据的函数,并将它们转换为字符串,如您所见:
public string GetParameters(byte[] buf)
{
string result = System.Text.UnicodeEncoding.Unicode.GetString(buf);
return result;
}
另一个应用程序将一个Farsi
字符串发送到缓冲区,我的应用程序应该获取此数据,但我的应用程序的函数返回:
㨲께裙듘�����¯
我如何接收缓冲区数据: 我定义了这个:
public byte[] dataBuffer = new byte[4000];
我有这个功能:
public void WaitForData(System.Net.Sockets.Socket soc)
{
try
{
if (pfnWorkerCallBack == null)
{
// Specify the call back function which is to be
// invoked when there is any write activity by the
// connected client
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
SocketPacket theSocPkt = new SocketPacket();
theSocPkt.m_currentSocket = soc;
// Start receiving any data written by the connected client
// asynchronously
soc.BeginReceive(theSocPkt.dataBuffer, 0,
theSocPkt.dataBuffer.Length,
SocketFlags.None,
pfnWorkerCallBack,
theSocPkt);
}
catch (Exception qqq)
{
}
}
还有一个:
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;
int iRx = 0;
// Complete the BeginReceive() asynchronous call by EndReceive() method
// which will return the number of characters written to the stream
// by the client
iRx = socketData.m_currentSocket.EndReceive(asyn);
string res = GetParameters(socketData.dataBuffer);
}
答案 0 :(得分:2)
尝试使用UTF-8:
string result = System.Text.UnicodeEncoding.UTF8.GetString(buf);
对我来说,看起来像波斯语。 (但我不会说波斯语。)
这里提到了Unicode和UTF-8之间的区别:UTF-8 vs. Unicode
基本上,角色的长度会有所不同。
答案 1 :(得分:-2)
string myDecodedString = Encoding.GetEncoding("1256").GetString(buf)