Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;字符数组";至int:C++;vs Java C++中有一些代码,如: struct Chunk { int flag; int size; }; Chunk chunk; chunk.flag = 'DIVA'; chunk.size = 512;_Java_C++ - Fatal编程技术网

&引用;字符数组";至int:C++;vs Java C++中有一些代码,如: struct Chunk { int flag; int size; }; Chunk chunk; chunk.flag = 'DIVA'; chunk.size = 512;

&引用;字符数组";至int:C++;vs Java C++中有一些代码,如: struct Chunk { int flag; int size; }; Chunk chunk; chunk.flag = 'DIVA'; chunk.size = 512;,java,c++,Java,C++,然后写入文件(fp是文件的指针): 在Java中,要读取块,我需要这样做 // Read file byte[] fileBytes = new byte[(int) file.length()]; FileInputStream fis = new FileInputStream(file); fis.read(fileBytes); fis.close(); byte[] flag = new byte[4]; byte[] size = new byte[4]; //LITTLE_END

然后写入文件(fp是文件的指针):

在Java中,要读取块,我需要这样做

// Read file
byte[] fileBytes = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(fileBytes);
fis.close();

byte[] flag = new byte[4];
byte[] size = new byte[4];
//LITTLE_ENDIAN : reverse it
System.arraycopy(fileBytes, 0, flag, 0, flag.length);
System.arraycopy(fileBytes, flag.length, size, 0, size.length);

ArrayUtils.reverse(flag);
ArrayUtils.reverse(size);
然后,检查结果

if(Arrays.equals(flag, "DIVA".getBytes()))
{
// do sth.
}
或者在java中这样做('Bytes.toInt'来自HBase)

然后执行此操作以检查此结果

if(flag == Bytes.toInt("DIVA".getBytes()))
{
// do sth.
}
我希望我已经表达清楚了

我的问题是以上两种方式,哪一种更好?还是有更好的办法

此外,Chunk被用作文件的头


我想我明白了。你知道很多C++人讨厌“C/C++”这个词吗?因为C和C++是两种截然不同的语言(妮基),对不起,我不知道。这不是一个多字节字符吗。只是定义了实现,这是有效的。@FDinoff我真的需要离开糟糕的Windows世界,伙计。谢谢你的链接。不要放弃那个评论。这很有帮助。当然,存储也是由实现定义的,所以不管怎样,假设其中任何一个上的小endian都是不安全的。再次感谢你的链接!
int flag;
int size;
flag = ByteBuffer.wrap(inBytes, startPos, 4).order(ByteOrder.LITTLE_ENDIAN).getInt();
size = ByteBuffer.wrap(inBytes, startPos + 4, 4).order(ByteOrder.LITTLE_ENDIAN).getInt();       
if(flag == Bytes.toInt("DIVA".getBytes()))
{
// do sth.
}