我正在尝试编写一个生成随机数的脚本,然后将这些数字转换为 4 字节组,然后将这些 4 字节组放入 Uint8Array 最后,我尝试将数组保存到二进制文件。这是我的代码:
<html>
<head>
<title>Raandom Number Generator</title>
</head>
<body>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="filesaver.min.js"></script>
<script type="text/javascript">
$(function() {
if (!Date.now) {
Date.now = function() {
return new Date().getTime();
};
}
alert("started");
var currentMousePos = {
x : -1,
y : -1
};
var randomData = [];
var bytes = new Int8Array(4);
$(document).mousemove(function(event) {
if(randomData.length>=1231){
$(document).unbind('mousemove');
console.log("Finished generating numbers ... trying to save file");
randomData = new Uint8Array(randomData);
var blob = new Blob(randomData, {type: "application/octet-stream"});
saveAs(blob, "rand.bin"); …Run Code Online (Sandbox Code Playgroud) 我正在从客户端的节点/express 端点接收数据缓冲区,并且我想将缓冲区转换为文件。
该文件可以是 pdf、文本文档或图像。端点告诉我的唯一信息是它正在发送一个八位字节流。
我该怎么做呢?
这是我迄今为止在客户端的代码
const xhr = new XMLHttpRequest();
xhr.open("POST", "MY_URL", true);
xhr.responseType = "arraybuffer";
// I need to send some data for the request
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
// send request
xhr.send(`identifier=${myIdentifier}`);
xhr.onload = function (xEvent)
{
const arrayBuffer = xhr.response;
if (arrayBuffer)
{
const byteArray = new Uint8Array(arrayBuffer);
const url = window.URL.createObjectURL(new Blob([byteArray]));
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", "file.pdf"); //or any other extension
document.body.appendChild(link);
link.click();
}
};
Run Code Online (Sandbox Code Playgroud) 我有一个二进制文件.我不知道如何使用C#读取这个二进制文件.
C++中描述的二进制文件中记录的定义是:
#define SIZEOF_FILE(10*1024)
//Size of 1234.dat file is: 10480 + 32 byte (32 = size of file header)
typedef struct FileRecord
{
WCHAR ID[56];
WCHAR Name[56];
int Gender;
float Height;
WCHAR Telephne[56];
and........
}
Run Code Online (Sandbox Code Playgroud)
如何在C#中读取包含这些记录的二进制文件,并在编辑后将其写回?
如何在scala中以块的形式读取二进制文件.
这就是我想要做的
val fileInput = new FileInputStream("tokens")
val dis = new DataInputStream(fileInput)
var value = dis.readInt()
var i=0;
println(value)
Run Code Online (Sandbox Code Playgroud)
打印的值是一个巨大的数字.而它应该返回1作为第一个输出
我有二进制文件,每次包含每个PNG文件(二进制文件不是DLL,不是EXE,没什么常见的,只是包含不同文本信息的文件,PNG文件和其他一些东西.格式文件对我来说是未知的.PNG文件可以显示一个执行此类文件的程序).我没有这个程序的来源做这些文件.我的任务是从二进制文件中提取此PNG文件以显示它或将其保存为PNG.我写了一个代码,它可以处理这些文件中的一部分(比如大约50%的文件),但不是.在不工作的文件上,创建此文件的程序仍然可以显示包含的图像,因此图像在每个文件内部确实有效 - 但无论如何我的代码对某些文件不起作用.
有些图像似乎可能有另一种格式,可能是编码类型(我已尝试过所有不同的编码类型,没有成功).这是我的代码(我希望有人可以告诉我改变什么,图像变得可读).
我的代码是什么:它找到PNG图像"‰PNG"的已知起始字符串和已知的结束字符串"IEND®B`".这个字符串在我的任何二进制文件中都包含PNG.然后我的代码获取开始和结束之间的字符串+开始和结束序列,并使用Encoding.Default将其保存到文件中.大多数通过这种方式提取的PNG文件可以使用图像查看器显示,但大约50%是无效的.如果我用编辑器打开它并将字符与工作图像进行比较,则图像看起来没问题.Sofar我不知道哪个符号是错误图像格式的原因.
如果需要我会提供更多信息,这里是我的代码:
private void button2_Click(object sender, EventArgs e)
{
string ReadFile1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "file.dat");
string WriteFile1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "test.png");
string TMP = File.ReadAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), ReadFile1), Encoding.Default); //System.Text.Encoding.GetEncoding(1251)
int start1 = TMP.IndexOf("PNG", 0 ,StringComparison.Ordinal);
if (start1 == 0) { return; }
int end1 = TMP.IndexOf("IEND", StringComparison.Ordinal);
string PNG = TMP.Substring(start1 - 1, (end1 + 9) - start1);
File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "test.png"), PNG, Encoding.Default);
}
Run Code Online (Sandbox Code Playgroud)
我还首先考虑使用二进制方法获取PNG并使用此代码,但我只是读取字符串时得到了完全相同的结果.这是我早期的代码.我使用字符串来搜索字节数组中的位置进行比较.我对二进制代码没有运气...
byte[] by;
// 1.
// Open file with a BinaryReader.
using (BinaryReader b = new …Run Code Online (Sandbox Code Playgroud) 我需要将一些图像转换为二进制文件以进行OCR.
以下是我正在使用的功能:
Mat binarize(Mat & Img, Mat& res, float blocksize, bool inverse)
{
Img.convertTo(Img,CV_32FC1,1.0/255.0);
CalcBlockMeanVariance(Img,res, blocksize, inverse);
res=1.0-res;
res=Img+res;
if (inverse) {
cv::threshold(res,res,0.85,1,cv::THRESH_BINARY_INV);
} else {
cv::threshold(res,res,0.85,1,cv::THRESH_BINARY);
}
cv::resize(res,res,cv::Size(res.cols/2,res.rows/2));
return res;
}
Run Code Online (Sandbox Code Playgroud)
地点CalcBlockMeanVariance:
void CalcBlockMeanVariance(Mat& Img,Mat& Res,float blockSide, bool inverse) //21 blockSide - the parameter (set greater for larger font on image)
{
Mat I;
Img.convertTo(I,CV_32FC1);
Res=Mat::zeros(Img.rows/blockSide,Img.cols/blockSide,CV_32FC1);
Mat inpaintmask;
Mat patch;
Mat smallImg;
Scalar m,s;
for(int i=0;i<Img.rows-blockSide;i+=blockSide)
{
for (int j=0;j<Img.cols-blockSide;j+=blockSide)
{
patch=I(Range(i,i+blockSide+1),Range(j,j+blockSide+1));
cv::meanStdDev(patch,m,s);
if(s[0]>0.01) // Thresholding parameter …Run Code Online (Sandbox Code Playgroud) 我无法找到以下问题的答案:从字符串开始,将其转换为二进制表示形式.你如何在Python中找回原始字符串?
例:
a = 'hi us'
b = ''.join(format(ord(c), '08b') for c in a)
Run Code Online (Sandbox Code Playgroud)
然后 b = 0110100001101001001000000111010101110011
现在我想在Python 2.x中获得'hi us'.例如,该网站完成了任务:http: //string-functions.com/binary-string.aspx
我已经看到了几个Java的答案,但没有运气实现到Python.我也试过b.decode(),但不知道在这种情况下我应该使用哪种编码.
我需要解析几个包含2字节整数值的二进制文件.由于我最近认为我不知道C++中的流是如何工作的,所以我决定尝试使用它们而不是使用它们fopen(); fread().所以我有这个功能:
void work( string filename )
{
ifstream f( filename );
if ( !f.is_open() )
throw exception( ("File not found: "+filename).c_str() );
//first 128 bytes are header that shouldn't be parsed
f.seekg( 0, f.end );
uint64_t length = f.tellg();
f.seekg( 128, f.beg );
if ( !f.good() )
throw exception( "Couldn't skip the 128byte header" );
length -= 128;
uint8_t buf[4];
vector<int> first, second;
size_t v = 0;
int a = -1;
int b = -1;
while ( …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个读取二进制文件并将其转换为数据类型的C程序.我正在使用head命令生成二进制文件head -c 40000 /dev/urandom > data40.bin.该程序适用于数据类型int和char但不适用于double.这是该程序的代码.
void double_funct(int readFrom, int writeTo){
double buffer[150];
int a = read(readFrom,buffer,sizeof(double));
while(a!=0){
int size = 1;
int c=0;
for(c=0;c<size;c++){
char temp[100];
int x = snprintf(temp,100,"%f ", buffer[c]);
write(writeTo, temp, x);
}
a = read(readFrom,buffer,sizeof(double));
}
}
Run Code Online (Sandbox Code Playgroud)
这是有效的char函数
void char_funct(int readFrom, int writeTo){
char buffer[150];
int a = read(readFrom,buffer,sizeof(char));
while(a!=0){
int size = 1;
int c=0;
for(c=0;c<size;c++){
char temp[100]=" ";
snprintf(temp,100,"%d ", buffer[c]);
write(writeTo, temp, strlen(temp));
}
a = read(readFrom,buffer,sizeof(char));
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,使用char我需要获得40000个单词 …
我有一个矩阵,初始化如下:
stateAndAction = zeros(11, 4);
Run Code Online (Sandbox Code Playgroud)
随着时间的推移,矩阵将被更新,以便在给定的索引处将有一个.所以在任何时候我们都可以看到这样的东西
1 1 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 1 0 0
0 0 0 1
1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Run Code Online (Sandbox Code Playgroud)
如何找到一个随机的行和列?
这是我想到的功能签名:
[random_row_index, random_column_index] = findRandom(stateAndAction)
Run Code Online (Sandbox Code Playgroud)