来自Java的DataOutputStream的C BinaryReader ReadUTF
我一直在致力于将这些类转换为C DataInputStream和DataOutputStream,我已经完成了DataOutputStream类,现在问题都出现在InputStream类中 注意:我没有在C中使用Encoding类的原因是因为Java中的DataInput/DataOutputStream使用自定义UTF-8编码 基本上,我有以下代码:C,它使用BinaryReader类 这是我的ReadUnsignedShort方法 下面是使用的Readfully方法: 对于OutputStream,问题是我使用的是Writeint而不是Writebyte函数,但我认为这里不是这种情况,否则我一定是瞎子 如果您对UTF字符串的发送方式感兴趣,下面是它的C转换:来自Java的DataOutputStream的C BinaryReader ReadUTF,java,c#,input,io,output,Java,C#,Input,Io,Output,我一直在致力于将这些类转换为C DataInputStream和DataOutputStream,我已经完成了DataOutputStream类,现在问题都出现在InputStream类中 注意:我没有在C中使用Encoding类的原因是因为Java中的DataInput/DataOutputStream使用自定义UTF-8编码 基本上,我有以下代码:C,它使用BinaryReader类 这是我的ReadUnsignedShort方法 下面是使用的Readfully方法: 对于OutputStr
public int WriteUTF(string str)
{
int strlen = str.Length;
int utflen = 0;
int c, count = 0;
for(int i = 0; i < strlen; i++)
{
c = str.ToCharArray()[i];
if((c >= 0x0001) && (c <= 0x007F))
{
utflen++;
}
else if(c > 0x07FF)
{
utflen += 3;
}
else
{
utflen += 2;
}
}
if(utflen > 65535)
{
throw new Exception("Encoded string is too long: " + utflen + " bytes");
}
byte[] bytearr = null;
bytearr = new byte[(utflen*2) + 2];
bytearr[count++] = (byte) (((uint)utflen >> 8) & 0xFF);
bytearr[count++] = (byte) (((uint)utflen >> 0) & 0xFF);
int x = 0;
for(x = 0; x < strlen; x++)
{
c = str.ToCharArray()[x];
if (!((c >= 0x0001) && (c <= 0x007F))) break;
bytearr[count++] = (byte)c;
}
for(;x < strlen; x++)
{
c = str.ToCharArray()[x];
if ((c >= 0x0001) && (c <= 0x007F))
{
bytearr[count++] = (byte)c;
}
else if (c > 0x07FF)
{
bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
else
{
bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
}
ClientOutput.Write (bytearr, 0, utflen+2);
return utflen + 2;
}
希望我已经提供了足够的信息,可以在读取UTF值时得到一些帮助,这真的给我的项目进度设置了障碍 如果我正确地理解了这个问题——你说有一个障碍,但你没有解释到底是什么障碍,那么你正在尝试用C实现从流中读写文本的代码。如果是这样的话,那么我知道如果你是.NET新手,这并不是很明显的,你自己显式地处理文本编码是疯狂的 BinaryReader和BinaryWriter具有处理此问题的方法。创建对象时,可以传递用于解释或创建文本二进制数据的编码实例,例如System.Text.Encoding.UTF8、System.Text.Encoding.Unicode等。您可以使用BinaryReader.ReadCharsint读取文本,使用BinaryWriter.Writechar[]写入文本
如果由于某种原因无法工作,至少可以直接使用编码实例来解释或创建某些文本的二进制数据。Encoding.GetStringbyte[]将二进制转换为文本,Encoding.GetByTeString将文本转换为二进制。同样,使用特定的编码实例来处理实际的文本编码。已经编写了Java的DataInputStream和DataOutputStream的C转换,您可以在这里收集它们 要构造这些类,需要将BinaryWriter或BinaryReader传递到构造函数中 构造DataOutputStream的步骤
DataOutputStream out = new DataOutputStream(new BinaryWriter(Stream));
构造DataInputStream的步骤
DataInptuStream in = new DataInputStream(new BinaryReader(Stream));
我这样做的原因是因为我在Java服务器中使用的DataOutputStream类使用自定义UTF-8加密,所以当我从服务器发送UTF字符串时,它使用的编码不同于标准UTF-8编码。这里的问题是,它没有正确读取数据,无论出于何种原因,它都没有返回字符串的值。。。甚至在这件事上有所进展。为了详细说明我为什么要这样做,我一直在编写的服务器结构使用Java DataInputStream和DataOutputStream类,我现在用C编写一个客户机,它需要能够与服务器通信而不会出现问题,考虑到Java类对UTF-8使用自定义编码,如果我正确理解Java实现,那么它与普通UTF8编码之间的唯一区别就是两个字节前缀指示剩余数据的字节计数。因此,只需将这两个字节作为Int16读取,然后使用该字节计数确定之后要读取的字节长度,然后将这些字节传递给Encoding.UTF8.GetString。显然,在写字符串时,你会做与之相反的事情。如果这里的回答没有帮助,但你能够自己解决问题,那么你最好继续回答自己的问题,以防将来有人发现自己处于类似的情况。如果这里的任何回答有帮助,你应该评论如何以及为什么,当然,对任何有用的答案进行标记/投票,以确定它们。谢谢很好,你把信息源放到了网上,但那是其他地方,无论如何,你都不会特别指出实际的问题是什么。链接已经死了。。。你能在别的地方分享代码吗?
public int WriteUTF(string str)
{
int strlen = str.Length;
int utflen = 0;
int c, count = 0;
for(int i = 0; i < strlen; i++)
{
c = str.ToCharArray()[i];
if((c >= 0x0001) && (c <= 0x007F))
{
utflen++;
}
else if(c > 0x07FF)
{
utflen += 3;
}
else
{
utflen += 2;
}
}
if(utflen > 65535)
{
throw new Exception("Encoded string is too long: " + utflen + " bytes");
}
byte[] bytearr = null;
bytearr = new byte[(utflen*2) + 2];
bytearr[count++] = (byte) (((uint)utflen >> 8) & 0xFF);
bytearr[count++] = (byte) (((uint)utflen >> 0) & 0xFF);
int x = 0;
for(x = 0; x < strlen; x++)
{
c = str.ToCharArray()[x];
if (!((c >= 0x0001) && (c <= 0x007F))) break;
bytearr[count++] = (byte)c;
}
for(;x < strlen; x++)
{
c = str.ToCharArray()[x];
if ((c >= 0x0001) && (c <= 0x007F))
{
bytearr[count++] = (byte)c;
}
else if (c > 0x07FF)
{
bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
else
{
bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
}
ClientOutput.Write (bytearr, 0, utflen+2);
return utflen + 2;
}
DataOutputStream out = new DataOutputStream(new BinaryWriter(Stream));
DataInptuStream in = new DataInputStream(new BinaryReader(Stream));