我正在使用FileOutputStream
编写文件,我注意到hex似乎包含终止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
。
更新:Moar Code:
所以我只是编写了这段代码来加载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惯例?
答案 0 :(得分:-3)
问题可能就在这里:
@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()使用String的平台编码,取决于编码是什么,getBytes()可以返回任何。
尝试明确指定编码,例如value.getBytes(Charset.forName("ISO-8859-1"));
编辑:还要确保输出的字符串确实包含您认为包含的内容。