C# | 类型 | 大小/字节 | .NET类型 | 说明 |
---|---|---|---|---|
byte | 1 | Byte | 无符号,值0~255 | |
char | 2 | Char | Unicode字符 | |
bool | 1 | Boolean | true or false | |
sbyte | 1 | SByte | 有符号,值-128~127 | |
short | 2 | Int16 | 有符号,值-32768~3276 | |
ushort | 2 | UInt16 | 无符号,值-2147483648~2147483647 | |
int | 4 | Int32 | 有符号整数,值0~4294967295 | |
float | 4 | Single | 浮点数,7位有效数字 | |
double | 8 | Double | 双精度浮点数,15~16位有效数字 | |
decimal | 12 | Decimal | 固定精度,值位最大28位加小数点 | |
long | 8 | Int64 | 有符号整数,值-9223372036854775808~9223372036854775807 | |
ulong | 8 | UInt64 | 无符号整数,值0~oxffffffffffffffff |
java | 类型 | 储存需求 | 取值范围 |
---|---|---|---|
int | 4字节 | -2147483648~2147483647(正好超过20亿) | |
short | 2字节 | -32768~32768 | |
long | 8字节 | -9223372036854775808~9223372036854775807 | |
byte | 1字节 | -128~127 | |
float | 4字节 | 大约+_3.40282347E+38F(有效位数6~7位) | |
double | 8字节 | 大约+_1,797693134866231570E+308(有效数位15位) | |
boolean | 只有true和false | ||
char | 存储Unicode码,用单引号赋值 |
C++ | 类型 | 含义 | 最小尺寸 |
---|---|---|---|
bool | 布尔类型 | 未定义 | |
char | 字符 | 8位 | |
wchar_t | 宽字符 | 16位 | |
char16_t | Unicode字符 | 16位 | |
char32_t | Unicode字符 | 32位 | |
short | 短整型 | 16位 | |
int | 整型 | 16位 | |
long | 长整型 | 32位 | |
long long | 长整型 | 64位 | |
float | 单精度浮点数 | 6位有效数字 | |
double | 双精度浮点数 | 10为有效数字 | |
long double | 扩展精度浮点数 | 10为有效数字 | |
C# byte 数组写入 读取
-
数据 int data1=100;float data2=0.1f;
-
文件是一块连续的内存 所以需要记录好每次写入数据的位置
int index=0;//文件开头位置
byte[] b=new byte[256];//初始化一个byte数组 -
数据转换
将数据转化为byte数据 byte[] b=BitConverter.GetBytes(data1); -
将数据存入byte数组
b.CopyTo(bytes,index); -
移动位置索引:写入文件后需要叠加文件大小位置
index+=4; -
将数据保存到指定位置
Syste.IO.File.WriteAllBytes(file,bytes); -
读取文件 判断文件是否存在
System.IO.File.Exists(file); -
建立缓存文件
bytes = new byte[256]; -
读取文件
bytes = System.IO.File.ReadAllBytes(file); -
分别读取文件
int data1=BitConverter.ToInt32(bytes,0);//从头开始读取 int占4字节
float data2 =BitConverter.ToSingle(bytes,4);//加上int的4字节,才是float文件位置 -
输出
Console.WriteLine(data1);
Console.WriteLine(data2);
using System;
using System.Collections.Generic;
using System.Text;
namespace control
{
public class ProgramClass
{
private static string file = "/Users/dragonfly/Documents/files/test.dat";
public ProgramClass()
{
//createFile();
//readFile();
//readByte();
writeFile();
}
public static void createFile() {
string[] data = new string[2];
data[0] = "第一条信息";
data[1] = "第二条信息";
//将信息写入磁盘中
System.IO.File.WriteAllLines(file, data);
Console.ReadKey();
}
public static void readFile() {
//判断文件是否存在
if (System.IO.File.Exists(file)) {
string[] data = new string[2];
data = System.IO.File.ReadAllLines(file);
data = System.IO.File.ReadAllLines(file);
Console.WriteLine(data[0]);
Console.WriteLine(data[1]);
//Console.ReadKey();
}
}
public static void deleteFile() {
if (System.IO.File.Exists(file)) {
System.IO.File.Delete(file);
}
}
public static void readByte() {
if (System.IO.File.Exists(file)) {
//需要存储的数据
int data1 = 100;
float data2 = 0.1f;
//记录位置
int index = 0;
byte[] bytes = new byte[256];
//将data1转化为byte数组
byte[] b = BitConverter.GetBytes(data1);
//将data1数据存入bytes数组中
b.CopyTo(bytes, index);
//一个int类型占4个byte
index += 4;
//将data2转化为byte数组
b = BitConverter.GetBytes(data2);
//将data2数据存入bytes数组中
b.CopyTo(bytes, index);
index += 4;
//将数据保存到:指定位置
System.IO.File.WriteAllBytes(file, bytes);
//判断文件是否存在
bytes = new byte[256];
bytes = System.IO.File.ReadAllBytes(file);
//取出头4个字节作为int
int readdata1 = BitConverter.ToInt32(bytes, 0);
//再取4个字节作为float
float readdata2 = BitConverter.ToSingle(bytes, 4);
//打印数据
Console.WriteLine(readdata1);
Console.WriteLine(readdata2);
}
}
}
}