Java C++ C#基础数据记录

C#类型大小/字节.NET类型说明
byte1Byte无符号,值0~255
char2CharUnicode字符
bool1Booleantrue or false
sbyte1SByte有符号,值-128~127
short2Int16有符号,值-32768~3276
ushort2UInt16无符号,值-2147483648~2147483647
int4Int32有符号整数,值0~4294967295
float4Single浮点数,7位有效数字
double8Double双精度浮点数,15~16位有效数字
decimal12Decimal固定精度,值位最大28位加小数点
long8Int64有符号整数,值-9223372036854775808~9223372036854775807
ulong8UInt64无符号整数,值0~oxffffffffffffffff
java类型储存需求取值范围
int4字节-2147483648~2147483647(正好超过20亿)
short2字节-32768~32768
long8字节-9223372036854775808~9223372036854775807
byte1字节-128~127
float4字节大约+_3.40282347E+38F(有效位数6~7位)
double8字节大约+_1,797693134866231570E+308(有效数位15位)
boolean只有true和false
char存储Unicode码,用单引号赋值
C++类型含义最小尺寸
bool布尔类型未定义
char字符8位
wchar_t宽字符16位
char16_tUnicode字符16位
char32_tUnicode字符32位
short短整型16位
int整型16位
long长整型32位
long long长整型64位
float单精度浮点数6位有效数字
double双精度浮点数10为有效数字
long double扩展精度浮点数10为有效数字

C# byte 数组写入 读取

  1. 数据 int data1=100;float data2=0.1f;

  2. 文件是一块连续的内存 所以需要记录好每次写入数据的位置
    int index=0;//文件开头位置
    byte[] b=new byte[256];//初始化一个byte数组

  3. 数据转换
    将数据转化为byte数据 byte[] b=BitConverter.GetBytes(data1);

  4. 将数据存入byte数组
    b.CopyTo(bytes,index);

  5. 移动位置索引:写入文件后需要叠加文件大小位置
    index+=4;

  6. 将数据保存到指定位置
    Syste.IO.File.WriteAllBytes(file,bytes);

  7. 读取文件 判断文件是否存在
    System.IO.File.Exists(file);

  8. 建立缓存文件
    bytes = new byte[256];

  9. 读取文件
    bytes = System.IO.File.ReadAllBytes(file);

  10. 分别读取文件
    int data1=BitConverter.ToInt32(bytes,0);//从头开始读取 int占4字节
    float data2 =BitConverter.ToSingle(bytes,4);//加上int的4字节,才是float文件位置

  11. 输出
    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);
            }
            }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值