C# 写入二进制文件
写入整型25 文件在MiniHex中显示 写入字符串I am happy
0A 6D -
6D - 这一行数据是C#把字符串转换为16进制形式
不知道为啥用MiniHex打开多了个0A
写入空""
在ASCII码中16进制00代表空字符
写入空格 " "
在ASCII码中16进制20代表空格. 01代表标题开始
string s = " AAA BBB CCC";
写入后
0C - 0C表示换页键;20表示空格
string s = "AAA BBB CCC";
写入后
0B 0B表示垂直制表符 20表示空格
string s = "A AA BBB CCC";
写入后
0C 0C表示换页键;20表示空格
string s = "AA A BBB CCC";
写入后
0C 0C表示换页键;20表示空格(还0C开头)
表示收到通知.
private void button1_Click(object sender, EventArgs e)
{
BinaryWriter bw;
BinaryReader br;
//字符串到16进制
string s = "I have";
string sHex = "";
byte[] sbytes = Encoding.Default.GetBytes(s);
for (int i = ; i < sbytes.Length; i++)
{
sHex += sbytes[i].ToString("X2") + " ";
}
//整型到16进制
int i25 = ;
string i25Hex = "";
i25Hex = i25.ToString("X2");
//浮点数到16进制
double d = 3.14157;
string dHex = "";
//dHex = d.ToString("X2");//报错
byte[] dbytes = Encoding.Default.GetBytes(d.ToString()); for (int i = ; i < dbytes.Length; i++)
{
dHex += dbytes[i].ToString("X2") + " ";
} bool b = true; string bHex = ""; //create the file bw = new BinaryWriter(new FileStream("mydata", FileMode.Create)); //创建文件 bin目录下 //writing into the file
//bw.Write(i25);//写入1个25
// bw.Write(d);
// bw.Write(b);
bw.Write(s);//写入一个字符串
bw.Close();
MessageBox.Show("ccc"); //写入'二进制'完成
//reading from the file
br = new BinaryReader(new FileStream("mydata", FileMode.Open));//在这里打个断点, i25 = br.ReadInt32(); d = br.ReadDouble(); b = br.ReadBoolean(); s = br.ReadString(); br.Close(); }
一脸懵逼....... 0A 0B 0C 06 什么鬼
1个字节8位 最大10进制数 最小值是-
= (在0000 1111中是255)在( 1111中是-)
=
=
= -
= -
= -
2个字节(1个字)16位 max
=
2个字32位 (1个字=2个字节)
= 10位数
4个字64位
= 19位数
int -> System.Int32 (整型,占 字节,表示 位整数,范围 -,,, 到 ,,,)
1个字节8位 最大10进制数 127 最小值是-128
0000 0000
1111 1111 = 255 (在0000 0000 1111 1111中是255)在(1111 1111中是-1)
1111 1110 = 254
0111 1111 = 127
1000 0000 = -128
1000 0001 = -127
1000 0010 = -126
2个字节(1个字)16位 max
0111 1111 1111 1111 = 32767
2个字32位 (1个字=2个字节)
0111 1111 1111 1111 1111 1111 1111 1111 = 2147483647 10位数
4个字64位
0111 1111 1111 1111 1111 1111 1111 1111
1111 1111 1111 1111 1111 1111 1111 1111 = 9223372036854775807 19位数
int -> System.Int32 (整型,占 4 字节,表示 32 位整数,范围 -2,147,483,648 到 2,147,483,647)
C# 创建二进制文件并写入
BinaryWriter bw = new BinaryWriter(new FileStream("mydata", FileMode.Create));
//bw.Write(i25);//写入1个25
// bw.Write(d);
// bw.Write(b);
bw.Write(s);//写入一个字符串
bw.Close(); C# 字节数组到字符串
public static string ByteArrayToString(byte[] ba)
{
string hex = BitConverter.ToString(ba);
return hex.Replace("-", "");
}
string str = System.Text.Encoding.Default.GetString(byteArray) C# 数组的创建
byte[] bytes = new byte[] { 0x01, 0x02, 0x03 };//直接赋值
byte[] bytes = new byte[];//每个值为0
byte[] bytes = { }; C# 读取二进制文件
BinaryReader br = new BinaryReader(new FileStream("mydata.pdf", FileMode.Open));
// var A0 =br.ReadByte(); //读取一个字节(第一个FF(25)(10进制)37)
byte[] bytes = new byte[];//每个值为0
for (int i = ; i < bytes.Length;i++ ) //循环读取多个字节
{
bytes[i] = br.ReadByte();
}
//读取1000字节
byte[] bytes = br.ReadBytes(); C# 读取二进制文件,从指定位置读取, 和读取到最后
br.BaseStream.Seek(, SeekOrigin.Begin);// 定位到第6236060个字节
var test = br.BaseStream.Length - br.BaseStream.Position;//总长度-当前位置, 可能是读取到最后
byte[] bytes = br.ReadBytes((int)test);
while (br.BaseStream.Position < br.BaseStream.Length)
{
// bytes[i] = br.ReadByte(); //读取到最后
}
using (BinaryReader br = new BinaryReader(fs))
{
while (br.PeekChar() > -)
{
// bytes[i] = br.ReadByte(); //读取到最后
}
}
C# 创建二进制文件并写入
BinaryWriter
bw = new BinaryWriter(new FileStream("mydata", FileMode.Create));
//bw.Write(i25);//写入1个25
//
bw.Write(d);
//
bw.Write(b);
bw.Write(s);//写入一个字符串
bw.Close();
C# 字节数组到字符串
public
static string ByteArrayToString(byte[] ba)
{
string
hex = BitConverter.ToString(ba);
return
hex.Replace("-", "");
}
string
str = System.Text.Encoding.Default.GetString(byteArray)
C# 数组的创建
byte[]
bytes = new byte[] { 0x01, 0x02, 0x03 };//直接赋值
byte[]
bytes = new byte[10];//每个值为0
byte[]
bytes = { };
C# 读取二进制文件
BinaryReader
br = new BinaryReader(new FileStream("mydata.pdf", FileMode.Open));
//
var A0 =br.ReadByte(); //读取一个字节(第一个FF(25)(10进制)37)
byte[]
bytes = new byte[1000];//每个值为0
for
(int i = 0; i < bytes.Length;i++ ) //循环读取多个字节
{
bytes[i]
= br.ReadByte();
}
//读取1000字节
byte[]
bytes = br.ReadBytes(1000);
C# 读取二进制文件,从指定位置读取, 和读取到最后
br.BaseStream.Seek(6236060,
SeekOrigin.Begin);// 定位到第6236060个字节
var test = br.BaseStream.Length -
br.BaseStream.Position;//总长度-当前位置, 可能是读取到最后
byte[]
bytes = br.ReadBytes((int)test);
while (br.BaseStream.Position < br.BaseStream.Length)
{
// bytes[i] = br.ReadByte(); //读取到最后
}
using
(BinaryReader br = new BinaryReader(fs))
{
while
(br.PeekChar() > -1)
{
// bytes[i] = br.ReadByte(); //读取到最后
}
}
C# 写入二进制文件的更多相关文章
- TypeError: write() argument must be str, not bytes报错原因及Python3写入二进制文件方法
Python2随机写入二进制文件: with open('/python2/random.bin','w') as f: f.write(os.urandom(10)) 但使用Python3会报错: ...
- 使用C++将OpenCV中Mat的数据写入二进制文件,用Matlab读出
在使用OpenCV开发程序时,如果想查看矩阵数据,比较费劲,而matlab查看数据很方便,有一种方法,是matlab和c++混合编程,可以用matlab访问c++的内存,可惜我不会这种方式,所以我就把 ...
- Python中str类型的字符串写入二进制文件时报TypeError错的处理方式
在用二进制模式打开文件情况下,写入一个str对象时报错:TypeError: a bytes-like object is required, not 'str' 出现该问题是因为Python严格区分 ...
- C# 将long类型写入二进制文件用bw.Write(num);将其读出用long num= br.ReadInt64();
理由: 因为long类型是 System.Int64 (长整型,占 8 字节,表示 64 位整数,范围大约 -(10 的 19) 次方 到 10 的 19 次方) 而long BinaryReader ...
- 将图片写入二进制文件,再从二进制文件还原图片(c++)
#include "string" #include "iostream" #include "fstream" using namespa ...
- c++读取list.txt中每行的字符串以及写入二进制文件
#include <fstream> #include <fstream> string path = ""; FILE* fp = fopen(path. ...
- python string写入二进制文件——直接wb形式open file,再write string即可
4 down vote accepted You misunderstood what \xhh does in Python strings. Using \x notation in Python ...
- [转载:]Fortran 二进制文件读写
一些朋友总是咨询关于二进制文件的读写和转化.这里就我自己的理解说一说. 一).一般问题 二进制文件与我们通常使用的文本文件储存方式有根本的不同.这样的不同很难用言语表达,自己亲自看一看,理解起来会容易 ...
- Python_struct模块操作二进制文件
''' 使用struct模块写入二进制文件 ''' import struct n=130000000 x=96.45 b=True s='a1@中国' sn=struct.pack('if?',n, ...
随机推荐
- 关于在JSP页面中为什么一定要用${pageContext.request.contextPath}来获取项目路径,而不能用${request.contextPath}?
这里的疑问在于pageContext和request都是JSP中的内置对象之一,为什么不直接用${request.contextPath}来获取项目路径? 出现这种疑问,其实是将JSP的内置对象和EL ...
- GMT,UTC,DST,CST时间详解
全球24个时区的划分 相较于两地时间表,可以显示世界各时区时间和地名的世界时区表(World Time),就显得精密与复杂多了,通常世界时区表的表盘上会标示着全球24个时区的城市名称,但究 ...
- [luogu1156]垃圾陷阱_动态规划_背包dp
垃圾陷阱 luogu-1156 题目大意:Holsteins在距离地面D英尺的地方,FJ间隔时间ti会往下扔第i个垃圾.Holsteins对待每一个垃圾都会选择吃掉或者垫高.Holsteins有10个 ...
- Linux统计行数命令wc(转)
Linux wc命令用于计算字数. 利用wc指令我们可以计算文件的Byte数.字数.或是列数,若不指定文件名称.或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据. 语 ...
- PHP array_diff()
定义和用法 array_diff() 函数返回两个数组的差集数组.返回的数组的元素都取自被比较的数组(既第一个数组). 在返回的数组中,键名保持不变. 语法 array_diff(array1,arr ...
- BZOJ 1605 [Usaco2008 Open]Crisis on the Farm 牧场危机 DP
题意:链接 方法: DP 解析: 第一眼搜索题,复杂度不同意dfs,并且牛的数量太多不能bfs,迭代更不可能,A*不会估价.可能记忆化? 等等记忆化我还搜个毛线- 直接改成DP就好了. 状态非常好想非 ...
- Buy or Build (poj 2784 最小生成树)
Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1348 Accepted: 533 Descr ...
- ios添加麦克风访问权限
不然程序崩溃: This app has crashed because it attempted to access privacy-sensitive data without a usage d ...
- ACM Amman Collegiate Programming Contest(7.22随机组队娱乐赛)
题目链接 https://vjudge.net/contest/240074#overview 只写一下自己做的几个题吧 /* D n^2的暴力dp怎么搞都可以的 这里先预处理 i到j的串时候合法 转 ...
- 【撸码caffe 五】数据层搭建
caffe.cpp中的train函数内声明了一个类型为Solver类的智能指针solver: // Train / Finetune a model. int train() { -- shared_ ...