gpt4 book ai didi

java - 为什么 Java/Linux 在文件末尾附加 0xa?

转载 作者:太空宇宙 更新时间:2023-11-04 04:36:20 25 4
gpt4 key购买 nike

我正在使用 FileOutputStream 写入文件,我注意到十六进制似乎包含终止 0x0a 。这是文件的输出:

0000000: d100 0b00 0000 4865 6c6c 6f20 776f 726c  ......Hello worl
0000010: 6429 0043 0500 0000 7072 696e 740a d).C....print.

(Note hex code comes from vim on Osx Yosimite x64)

请注意,文件以 0x0a 结尾。这是 Linux 的东西(比如某种 EOF 字符)还是 java/FileOutputStream追加字符?

编辑:代码

public class FileImageOpcodeRenderer implements OpCodeRenderer, AutoCloseable {
private final FileOutputStream stream;
private final Iterable<OpCode> opcodes;
private final List<Byte> data = new ArrayList<>();

public FileImageOpcodeRenderer(Iterable<OpCode> opcodes, String path) {
this.opcodes = opcodes;
try {
this.stream = new FileOutputStream(path);
} catch (IOException ex) {
ex.printStackTrace();

throw new CompilerException("Unable to open output file");
}
}

public void save() throws Exception {
List<Byte> renderedContent = renderOpCodes();
byte[] content = toArray(renderedContent);
stream.write(content, 0, content.length);
}

private List<Byte> renderOpCodes() {
for (OpCode opCode : opcodes) {
writeInt8(null, opCode.opNumber());
opCode.render(this);
}

return this.data;
}

private byte[] toArray(List<Byte> data) {
byte[] content = new byte[data.size()];

for (int index = 0; index < data.size(); index++) {
content[index] = data.get(index);
}

return content;
}

@Override
public void writeInt8(String label, int value) {
data.add((byte)value);
}

@Override
public void writeInt16(String label, int value) {
writeInt8(null, (byte)(value & 0xFF));
writeInt8(null, (byte)((value >> 8) & 0xFF));
}

@Override
public void writeInt32(String label, int value) {
writeInt8(null, (byte)(value & 0xFF));
writeInt8(null, (byte)((value >> 8) & 0xFF));
writeInt8(null, (byte)((value >> 16) & 0xFF));
writeInt8(null, (byte)((value >> 24) & 0xFF));
}

@Override
public void writeInt64(String label, long value) {
writeInt8(null, (byte)(value & 0xFF));
writeInt8(null, (byte)((value >> 8) & 0xFF));
writeInt8(null, (byte)((value >> 16) & 0xFF));
writeInt8(null, (byte)((value >> 24) & 0xFF));
writeInt8(null, (byte)((value >> 32) & 0xFF));
writeInt8(null, (byte)((value >> 40) & 0xFF));
writeInt8(null, (byte)((value >> 48) & 0xFF));
writeInt8(null, (byte)((value >> 56) & 0xFF));
}

@Override
public void writeString(String label, String value) {
writeInt32(null, value.length());
for (byte letter : value.getBytes()) {
System.out.println((int)letter);
writeInt8(null, letter);
}
}

@Override
public void close() throws Exception {
stream.close();
}
}

我什至正在查看 save 中的原始字节它以 0x74 结尾,不是0x0a .

更新:摩尔代码:

所以我只是编写了这段代码来用 C++ 加载文件:

int main() {
int fd = open("/Users/sircodesalot/Desktop/image.vbaj", O_RDONLY);

char buffer[256] { };
int amout = read(fd, buffer, 256);

cout << amout << endl;

for (int index = 0; index != 256; ++index) {
if (index > 0 && (index % 10 == 0)) cout << endl;
cout << hex << (int)buffer[index] << " ";
}

close(fd);
}

输出:

29
ffffffd1 0 b 0 0 0 48 65 6c 6c
6f 20 77 6f 72 6c 64 29 0 43
5 0 0 0 70 72 69 6e 74 0
0 0 0 0 0 0 0 0 0 0
... (rest of the 256 byte buffer)

看吧,不0xa ?这有什么关系?也许有一些 Linux 约定?

最佳答案

问题可能出在这里:

@Override
public void writeString(String label, String value) {
writeInt32(null, value.length());
for (byte letter : value.getBytes()) {
System.out.println((int)letter);
writeInt8(null, letter);
}
}

您正在以字符为单位写入长度,但随后继续从 getBytes() 写入字节。 getBytes() 使用字符串的平台编码,并且根据编码的不同,getBytes() 可以返回任何内容。

尝试明确指定编码,例如value.getBytes(Charset.forName("ISO-8859-1"));

编辑:还要确保您输出的字符串确实包含您认为它们包含的内容。

关于java - 为什么 Java/Linux 在文件末尾附加 0xa?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25997631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com