我正在尝试将二进制文件(例如可执行文件)读入字符串,然后将其写回
FileStream fs = new FileStream("C:\\tvin.exe", FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
System.Text.Encoding enc = System.Text.Encoding.ASCII;
string myString = enc.GetString(bin);
fs.Close();
br.Close();
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] rebin = encoding.GetBytes(myString);
FileStream fs2 = new FileStream("C:\\tvout.exe", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs2);
bw.Write(rebin);
fs2.Close();
bw.Close();
Run Code Online (Sandbox Code Playgroud)
这不起作用(结果具有完全相同的字节大小但无法运行)
如果我做bw.Write(bin)结果没问题,但我必须将它保存为字符串
我需要从文件中读取一个字节,然后用0x71读取它并将其写回另一个文件.但是,当我使用以下内容时,它只是将字节作为字符串读取,因此xoring会产生问题.
f = open('a.out', 'r')
f.read(1)
Run Code Online (Sandbox Code Playgroud)
所以我最后在C中做了同样的事情.
#include <stdio.h>
int main() {
char buffer[1] = {0};
FILE *fp = fopen("blah", "rb");
FILE *gp = fopen("a.out", "wb");
if(fp==NULL) printf("ERROR OPENING FILE\n");
int rc;
while((rc = fgetc(fp))!=EOF) {
printf("%x", rc ^ 0x71);
fputc(rc ^ 0x71, gp);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人能告诉我如何将我使用的字符串转换f.read()为十六进制值,以便我可以将其与0x71进行xor并随后将其写入文件?
我需要解析并编码为Java中的传统二进制消息格式.我首先使用DataOutputStream来读取/写入基本类型,但我遇到的问题是消息格式与字节偏移不完全对齐并包括位标志.
例如,我必须处理这样的消息:
+----------+---+---+----------+---------+--------------+
+uint32 +b +b + uint32 +4bit enum+32 byte string+
+----------+---+---+----------+---------+--------------+
Run Code Online (Sandbox Code Playgroud)
其中(b)是一位标志.问题是java原语类型没有与字节边界对齐,因此我无法使用DataOutputStream对此进行编码,因为我可以写入的最低级别类型是一个字节.
是否有任何库,标准或第三方处理任意位级消息格式?
编辑:感谢@Software Monkey强迫我更仔细地查看我的规范.我正在使用的规范确实在字节边界上对齐,因此DataOutputStream是合适的.鉴于我原来的问题,虽然我会选择@emboss提出的解决方案.
编辑:虽然发现此问题的消息格式是在字节边界上,但我遇到了另一种适用于原始问题的消息格式.这种格式定义了一个6位字符映射,其中每个字符实际上只占用6位,而不是整个字节,因此字符串不在字节边界上对齐.我发现了几个解决这个问题的二进制输出流.像这样:http://introcs.cs.princeton.edu/java/stdlib/BinaryOut.java.html
我有一个使用Python代码创建的二进制文件.此代码主要编写一系列任务来预处理一组数据文件.我现在想在Fortran中读取这个二进制文件.二进制文件的内容是简单格式的点坐标,例如:点数,x0,y0,z0,x1,y1,z1,....
这些二进制文件是使用numpy中的'tofile'函数创建的.到目前为止,我在Fortran中有以下代码:
integer:: intValue
double precision:: dblValue
integer:: counter
integer:: check
open(unit=10, file='file.bin', form='unformatted', status='old', access='stream')
counter = 1
do
if ( counter == 1 ) then
read(unit=10, iostat=check) intValue
if ( check < 0 ) then
print*,"End Of File"
stop
else if ( check > 0 ) then
print*, "Error Detected"
stop
else if ( check == 0 ) then
counter = counter + 1
print*, intValue
end if
else if ( counter > 1 ) then
read(unit=10, iostat=check) …Run Code Online (Sandbox Code Playgroud) 我是JQuery的新手,我想使用JQuery Ajax将一些文件上传到服务器,仅在PUT方法中.当我向我的服务器发送一些二进制文件(例如gif或jpeg)时,上传成功,但二进制数据的内容被更改(它总是大于原始文件大小).我尝试更改内容类型或文件结果类型,但仍然无法正常工作.谁知道如何解决这个问题?
PS:我无法将二进制文件的内容编码成其他形式,因为我无法触及服务器的代码.
var reader = new FileReader();
reader.onloadend = (function(Thefile) {
$.ajax({
url: url,
processData:false,
//contentType:"application/octet-stream; charset=UTF-8",
data: file.result,
type: 'PUT',
success : function(){console.log("OK!");},
error : function(){console.log("Not OK!");}
});
})(file);
reader.readAsBinaryString(file);
Run Code Online (Sandbox Code Playgroud) 当我研究流程调度时,这个问题浮现在我脑海中.
操作系统如何执行和控制二进制文件和已编译文件的执行?我想OS可能会将二进制文件的一部分复制到某个内存位置,跳转到那里,在执行该块后返回并执行下一个块.但是它对它没有任何控制(例如程序可以在任何地方跳转而不会回来).
在JVM的情况下,它很有意义,VM正在解释每条指令.但在二进制文件的情况下,指令是真正的CPU可执行指令,所以我不认为操作系统像VM一样.
我只是想从二进制文件中读/写.我一直在关注这个教程,它的工作原理......除了它似乎是在写一个txt文件.我在测试时命名文件test.bin,但记事本可以打开并正确显示它,所以我认为它实际上不是二进制文件.我已经说过它是一个带有"wb"和"rb"的二进制文件吗?
if arg[1] == "write" then
local output = assert(io.open(arg[2], "wb"))
output:write(arg[3]) --3rd argument is written to the file.
assert(output:close())
elseif arg[1] == "read" then
local input = assert(io.open(arg[2], "rb"))
print(input:read(1)) --Should read one byte, not one char/int. Right?
end
Run Code Online (Sandbox Code Playgroud) 我在一个程序中有一节写入直接访问二进制文件,如下所示:
open (53, file=filename, form='unformatted', status='unknown',
& access='direct',action='write',recl=320*385*8)
write (53,rec=1) ulat
write (53,rec=2) ulng
close(53)
Run Code Online (Sandbox Code Playgroud)
该程序使用ifort编译.但是,如果我从使用gfortran编译的其他程序中读取数据文件,则无法正确重建数据.如果读取数据的程序也在ifort中编译,那么我可以正确地重建数据.这是读取数据文件的代码:
OPEN(53, FILE=fname, form="unformatted", status="unknown", access="direct", action="read", recl=320*385*8)
READ(53,REC=2) DAT
Run Code Online (Sandbox Code Playgroud)
我不明白为什么会这样?我可以用两个编译器正确读取第一条记录,这是我混合编译器时无法正确重建的第二条记录.
我试图找出在Python中生成的包装二进制文件的最佳方法,如下所示:
import struct
f = open('tst.bin', 'wb')
fmt = 'iih' #please note this is packed binary: 4byte int, 4byte int, 2byte int
f.write(struct.pack(fmt,4, 185765, 1020))
f.write(struct.pack(fmt,4, 185765, 1022))
f.close()
Run Code Online (Sandbox Code Playgroud)
我一直在修补我在Github.com和其他一些消息来源上看到的一些例子 但我似乎无法正常工作(更新显示工作方法). 在Go中做这种事的惯用方法是什么?这是几次尝试之一
更新和工作
package main
import (
"fmt"
"os"
"encoding/binary"
"io"
)
func main() {
fp, err := os.Open("tst.bin")
if err != nil {
panic(err)
}
defer fp.Close()
lineBuf := make([]byte, 10) //4 byte int, 4 byte int, 2 byte int per line
for true {
_, err …Run Code Online (Sandbox Code Playgroud) 我有一个整数数组
Array
(
[0] => Array
(
[0] => 1531412763
[1] => 1439959339
[2] => 76
[3] => 122
[4] => 200
[5] => 4550
[6] => 444
)
...
Run Code Online (Sandbox Code Playgroud)
依此类推,我想如果我把它看作是一个数据库 - 最外层数组的元素是行,内部数组的元素是列.
我想将该信息保存到文件中,以便稍后我可以检索它,但我想将其保存为二进制数据以节省空间.基本上,如果我将示例中的第一个整数写入1531412763文件,它将占用10个字节,但如果我可以将其保存为有符号整数,则需要占用4个字节.
我已经看了一些其他的答案,这些答案都建议使用fwrite哪些我无法理解如何以这种方式使用?
binaryfiles ×10
fortran ×2
python ×2
ajax ×1
binary-data ×1
c# ×1
executable ×1
filesystems ×1
gfortran ×1
go ×1
hex ×1
java ×1
jquery ×1
lua ×1
php ×1
put ×1
string ×1
upload ×1