简体   繁体   English

Java无符号字符数组

[英]Java Unsigned Char Array

I'm working on a project fuzzing a media player. 我正在做一个模糊媒体播放器的项目。 I wrote the file generator in Java and converted the CRC generator from the original compression code written in C. I can write data fine with DataOutputStream, but I can't figure out how to send the data as an unsigned character array in java. 我用Java编写了文件生成器,然后从用C编写的原始压缩代码转换了CRC生成器。我可以使用DataOutputStream很好地编写数据,但是我不知道如何在Java中将数据作为无符号字符数组发送。 In C this is a very straightforward process. 在C语言中,这是一个非常简单的过程。 I have searched for a solution pretty thoroughly, and the best solution I've found is to just send the data to C and let C return a CRC. 我已经彻底搜索了一个解决方案,发现的最佳解决方案是将数据发送到C,然后让C返回CRC。 I may just not be searching correctly as I'm pretty unfamiliar with this stuff. 我可能不正确地搜索,因为我对这些东西不熟悉。 Thank you very much for any help. 非常感谢您的帮助。

You definitely want a byte[]. 您肯定要一个字节[]。 A 'byte' is equivalent to a signed char in C. Java's "char" is a 16-bit unicode value and not really equivalent at all. “字节”等效于C中的带符号char。Java的“ char”是16位unicode值,根本不是等效的。

If it's for fuzzing, unless there's something special about the CRC function you're using, I imagine you could simply use: 如果是用于模糊测试,除非您使用的CRC功能有特殊之处,否则我想您可以简单地使用:

import java.util.Random;
Random randgen = new Random();

byte[] fuzzbytes = new byte[numbytes];
randgen.nextBytes(fuzzbytes);
outstream.write(fuzzbytes, 0, numbytes);

I doubt that you want to do anything with characters. 我怀疑您想对角色做任何事情。 I can't see anything in your description which suggests text manipulation, which is what you'd do with characters. 我在您的描述中看不到任何暗示文本操作的内容,这就是您要处理的字符。

You want to use a byte array. 您要使用字节数组。 It's a bit of a pain that bytes are signed in Java, but a byte array is what you've got - just work with the bit patterns rather than thinking of them as actual numbers, and check each operation carefully. 用Java对字节进行签名有点痛苦,但是字节数组就是您所拥有的-只使用位模式而不是将它们视为实际数字,并仔细检查每个操作。

Most CRC operators use mostly bitwise shifts and XORs. 大多数CRC运算符大多使用按位移位和XOR。 These should work fine on Java, which does not support unsigned integer primitives. 在不支持无符号整数原语的Java上,它们应该可以正常工作。 If you need other arithmetic to work properly, you could try casting to a short . 如果您需要其他算术才能正常工作,则可以尝试强制转换为short

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM