BinaryReader,BinaryFormatter的 - 他们是便携式? [英] BinaryReader, BinaryFormatter - are they portable?

查看:115
本文介绍了BinaryReader,BinaryFormatter的 - 他们是便携式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,C ++,如果你写的二进制方式的一个整数(如 FWRITE(安培; someInt,sizeof的(INT),1,FP))说文件,你有大量的问题需要考虑,如果你想读的不同机器整数 - 例如,排列顺序问题,整数大小可以在其他计算机上的不同,上浮重新presentation可能不同的计算机上有所不同,负整数有不同的重新presentation,等于是直接读取,并以二进制方式写可能无法在不同的计算机上运行。

In C,C++ if you write an integer in binary way(e.g. fwrite(&someInt,sizeof(int),1,fp)) say to file, you have tons of issues to consider if you want to read that integer from different machine - e.g., endianness issues, integer size may vary on other computer, float representation may vary on different computer, negative integers have different representation, etc. So directly reading and writing in binary mode may not work across different computers.

在下面用二进制读卡器(从MSDN)这个例子中,上面做的问题也适用?

In this example below using Binary reader (from MSDN), do above issues also apply?

using System;
using System.IO;

class ConsoleApplication
{
    const string fileName = "AppSettings.dat";

    static void Main()
    {
        WriteDefaultValues();
        DisplayValues();
    }

    public static void WriteDefaultValues()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            writer.Write(1.250F);
            writer.Write(@"c:\Temp");
            writer.Write(10);
            writer.Write(true);
        }
    }

    public static void DisplayValues()
    {
        float aspectRatio;
        string tempDirectory;
        int autoSaveTime;
        bool showStatusBar;

        if (File.Exists(fileName))
        {
            using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                aspectRatio = reader.ReadSingle();
                tempDirectory = reader.ReadString();
                autoSaveTime = reader.ReadInt32();
                showStatusBar = reader.ReadBoolean();
            }

            Console.WriteLine("Aspect ratio set to: " + aspectRatio);
            Console.WriteLine("Temp directory is: " + tempDirectory);
            Console.WriteLine("Auto save time set to: " + autoSaveTime);
            Console.WriteLine("Show status bar: " + showStatusBar);
        }
    }
}

关于的BinaryFormatter什么类?

推荐答案


  • 中的BinaryWriter的字节顺序是固定的。它始终是小端。

  • 整数大小始终是每个数据类型是相同的:字节/为sbyte 1B 短/ USHORT 2B INT / UINT 4B 长/ ULONG 8B

  • 和双精度浮点数遵循IEEE斯坦达特

  • 阅读,并与 BinaryReader /的BinaryWriter 将带来同样的结果写。从C#与Java,你需要翻转所有传入的字节,因为Java是使用大尾端

  • 在同一台PC上的的BinaryFormatter 的目的是序列化或反序列化对象。传送从一台PC数据时另一个不应该被使用。

  • The endianess of the BinaryWriter is fixed. It is always little endian.
  • The integer size is always the same for each data type: byte/sbyte 1B short/ushort 2B int/uint 4B long/ulong 8B
  • Floats and Doubles follow the IEEE Standart
  • Reading and writing with the BinaryReader/BinaryWriter will bring the same result. From C# to Java you would need to flip all incoming bytes because Java is using BigEndian
  • The BinaryFormatter is meant to serializes or deserializes objects on the same PC. It should not be used when transferring data from one PC to another.
  • 如果您正在使用两个C#应用程序的工作简单地使用 BinaryReader /的BinaryWriter ,它们是可移植的。如果另一端不与C#编程,你很可能需要使用(或写你自己的)类来处理这些问题。

    If you are working with two C# Applications simply use the BinaryReader/BinaryWriter, they are portable. If the other end is not programmed with C# you will most likely need to use (or write your own) class to handle such issues.

    这篇关于BinaryReader,BinaryFormatter的 - 他们是便携式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆