c# Bytes读写
来源:互联网 发布:淘宝卖假货会不会坐牢 编辑:程序博客网 时间:2024/05/29 15:29
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReadAndWriteBytesDemo
{
class Program
{
static void Main(string[] args)
{
string _file = "d:\\test.dat";
long data1 = 100;
float data2 = 0.1F;
byte[] _bytes;
short _length;
List<byte> _listBytes = new List<byte>();
_length = 8;
_bytes = BitConverter.GetBytes(_length);
if(BitConverter.IsLittleEndian)
{
Array.Reverse(_bytes);
}
_listBytes.AddRange(_bytes);
_bytes = BitConverter.GetBytes(data1);
_listBytes.AddRange(_bytes);
_length = 8;
_bytes = BitConverter.GetBytes(_length);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(_bytes);
}
_listBytes.AddRange(_bytes);
_bytes = BitConverter.GetBytes(data2);
_listBytes.AddRange(_bytes);
System.IO.File.WriteAllBytes(_file, _listBytes.ToArray()); //写
if (System.IO.File.Exists(_file))
{
_bytes = System.IO.File.ReadAllBytes(_file);//读
int _index = 0;
short _realdatal = BitConverter.ToInt16(_bytes, 0);
_index += 2;
long _realData1 = BitConverter.ToInt64(_bytes, _index);
_index += 8;
_realdatal = BitConverter.ToInt16(_bytes, _index);
_index += 2;
float _realData2 = BitConverter.ToSingle(_bytes, _index);
_index += 8;
Console.WriteLine(_realData1);
Console.WriteLine(_realData2);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReadAndWriteBytesDemo
{
class Program
{
static void Main(string[] args)
{
string _file = "d:\\test.dat";
long data1 = 100;
float data2 = 0.1F;
byte[] _bytes;
short _length;
List<byte> _listBytes = new List<byte>();
_length = 8;
_bytes = BitConverter.GetBytes(_length);
if(BitConverter.IsLittleEndian)
{
Array.Reverse(_bytes);
}
_listBytes.AddRange(_bytes);
_bytes = BitConverter.GetBytes(data1);
_listBytes.AddRange(_bytes);
_length = 8;
_bytes = BitConverter.GetBytes(_length);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(_bytes);
}
_listBytes.AddRange(_bytes);
_bytes = BitConverter.GetBytes(data2);
_listBytes.AddRange(_bytes);
System.IO.File.WriteAllBytes(_file, _listBytes.ToArray()); //写
if (System.IO.File.Exists(_file))
{
_bytes = System.IO.File.ReadAllBytes(_file);//读
int _index = 0;
short _realdatal = BitConverter.ToInt16(_bytes, 0);
_index += 2;
long _realData1 = BitConverter.ToInt64(_bytes, _index);
_index += 8;
_realdatal = BitConverter.ToInt16(_bytes, _index);
_index += 2;
float _realData2 = BitConverter.ToSingle(_bytes, _index);
_index += 8;
Console.WriteLine(_realData1);
Console.WriteLine(_realData2);
}
}
}
}
- c# Bytes读写
- C# Bimap转bytes解决方法
- bytes
- C#读写INI文件
- C#读写INI文件
- C#读写注册表
- C# 读写注册表
- C#读写注册表
- C#读写配置文件
- C#读写INI文件
- C#读写INI文件
- C#中读写内存
- C#读写系统日志
- c#读写xml文件
- c#读写xml文件
- c#读写xml文件
- C#读写文件
- C#读写文本文件
- 2013/08/27 C# WPF 学习笔记
- 【Android 开发】:数据存储之 SQLite 数据库操作(二)
- HOD服务集群 torque maui
- 浏览器端图表渲染技术SVG, VML HTML Canvas
- 多线程技术
- c# Bytes读写
- linux ubantu安装jdk、tomcat
- stack smashing detected 的处理
- 编译安装哈工大LTP工具
- go 0x30008000之后遇到的问题
- 一天一天学 windows phone 引言 + 大概认知 之 导航3事件(三)
- Java Tutorial: How to Create RESTful Java Client using Apache HttpClient – Example
- 【Android 开发】:数据存储之 SQLite 数据库操作(三)
- C++内存分配秘籍 -- new,malloc,GlobalAlloc详解