---
title: "c#的二进制 读写"
author: "qjchen"
date: "2012-08-10T17:38:16Z"
tags: []
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2012-08-15T17:22:21Z"
id: 1628513
url: "https://qjchen.iteye.com/blog/1628513"
---

http://stackoverflow.com/questions/5463476/how-to-read-a-binary-file-using-c

这里有一点点说明

http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file
有序列化的写法

今天在对比二进制写入的时候，发现一个问题
string abc = "aabbcc";
bw.Write(abc);
会无缘无故地在二进制的文件里面出现
06 61 61 62 62 63 63
这个06真是不知道怎么出现的
所以，后来我只好写了一个
       
```csharp

 static private char[] str2chars(string str, int length)
        {
            char[] res = new char[length];
            //必须容错，否则会提示错误
            if (str.Length > length) { str.CopyTo(0, res, 0, length); }
            else { str.CopyTo(0, res, 0, str.Length); }
            return res;

        }

```

这样的代码来自己制作一个规定长度的char；

微软关于datagridview的写法
http://msdn.microsoft.com/zh-cn/library/5s3ce6k8