在要求输入邮箱的文本域,请填写真实的邮件地址。非真实邮件地址,将收不到回复信息。

C#文本文档类型转换(dos2unix、dos2mac、unix2dos、mac2dos、unix2mac、mac2unix)

C# 清风 2045℃ 0评论

由于某些业务需要与银行进行交互,我们提交相应的批次文件银行处理,但是银行要求的文本文件格式为Unix,所以需要进一步处理。如果一个文件还可以用编辑器转换一下,但是数量稍微多再使用编辑器转换就太累了。

Windows、Linux、MAC的文本文件换行回车格式不同,基于 Windows系统 的文本文件类型是DOS在每一行末尾有一个 CR(回车)和 LF(换行),而 Linux系统的文本文件类型是Unix只有一个 LF(换行),MAC系统的文本文件类型是mac一个 CR(回车)。

对于换行这个动作,unix下一般只有一个0x0A表示换行(“\n”),windows下一般都是0x0D和0x0A两个字符(“\r\n”),苹果机(MAC OS系统)则采用回车符CR表示下一行(\r)

  • Unix系统里,每行结尾只有“<换行>”,即“\n”;
  • Windows系统里面,每行结尾是“<回车><换行>”,即“\r\n”;
  • Mac系统里,每行结尾是“<回车>”,即“\r”。

Unix/Mac系统下的文件在Windows里打开的话,所有文字会变成一行;而Windows里的文件在Unix下打开的话,在每行的结尾会多一个^M字符。即 “\r” = ^M,Dos和windows采用回车+换行 CR+LF(\r\n)表示下一行,即^M$($不是换行符的表示,换行符没有表示出来,$是文本结束EOF的表示)。

C#实现文本文档格式转换:


public class TextDocumentFileTypeConvert 
{
  const byte CR = 0x0D;
  const byte LF = 0x0A;
  static byte[] DOS_LINE_ENDING = new byte[] { CR, LF };
  public static void Unix2Dos(string fileName) 
  {
    byte[] data = File.ReadAllBytes(fileName);
    using (FileStream fileStream = File.OpenWrite(fileName)) 
    {
      BinaryWriter bw = new BinaryWriter(fileStream);
      int position = 0;
      int index = 0;
      do 
      {
        index = Array.IndexOf(data, LF, position);
        if (index >= 0) 
        {
          if (index > 0 && data[index - 1] == CR) 
          {
            bw.Write(data, position, index - position + 1);
          } else 
          {
            bw.Write(data, position, index - position);
            bw.Write(DOS_LINE_ENDING);
          }
          position = index + 1;
        }
      }
      while (index >= 0);
      bw.Write(data, position, data.Length - position);
      fileStream.SetLength(fileStream.Position);
    }
  }
  public static void Mac2Dos(string fileName) 
  {
    byte[] data = File.ReadAllBytes(fileName);
    int len = data.Length - 1;
    using (FileStream fileStream = File.OpenWrite(fileName)) 
    {
      BinaryWriter bw = new BinaryWriter(fileStream);
      int position = 0;
      int index = 0;
      do 
      {
        index = Array.IndexOf(data, CR, position);
        if (index >= 0) 
        {
          if (index >= 0 && index < len && data[index + 1] == LF)
          {
             bw.Write(data, position, index - position + 1);
          } 
         else 
         { 
            bw.Write(data, position, index - position); 
            bw.Write(DOS_LINE_ENDING);
         } position = index + 1; 
       } 
      } while (index >= 0);
      bw.Write(data, position, data.Length - position);
      fileStream.SetLength(fileStream.Position);
    }
  }
  public static void Dos2Unix(string fileName) 
  {
    byte[] data = File.ReadAllBytes(fileName);
    int len = data.Length-1;
    using (FileStream fileStream = File.OpenWrite(fileName)) 
    {
      BinaryWriter bw = new BinaryWriter(fileStream);
      int position = 0;
      int index = 0;
      do 
      {
        index = Array.IndexOf(data, CR, position);
        if (index >= 0) 
        {
          if (index >= 0&&index= 0);
      bw.Write(data, position, data.Length - position);
      fileStream.SetLength(fileStream.Position);
    }
  }
  public static void Dos2Mac(string fileName) 
  {
    byte[] data = File.ReadAllBytes(fileName);
    using (FileStream fileStream = File.OpenWrite(fileName)) 
    {
      BinaryWriter bw = new BinaryWriter(fileStream);
      int position = 0;
      int index = 0;
      do 
      {
        index = Array.IndexOf(data, LF, position);
        if (index >= 0) 
        {
          if (index > 0 && data[index - 1] == CR) 
          {
            bw.Write(data, position, index - position);
          } else 
          {
            bw.Write(data, position, index - position+1);
          }
          position = index + 1;
        }
      }
      while (index >= 0);
      bw.Write(data, position, data.Length - position);
      fileStream.SetLength(fileStream.Position);
    }
  }
  public static void Mac2Unix(string fileName) 
  {
    byte[] data = File.ReadAllBytes(fileName);
    int len = data.Length-1;
    using (FileStream fileStream = File.OpenWrite(fileName)) 
    {
      BinaryWriter bw = new BinaryWriter(fileStream);
      int position = 0;
      int index = 0;
      do 
      {
        index = Array.IndexOf(data, CR, position);
        if (index >= 0) 
        {
          if (index >= 0 && index<len&&data[index + 1] == LF)
          { 
             bw.Write(data, position, index - position + 1);
          } 
         else
         { 
            bw.Write(data, position, index - position); 
            bw.Write(LF); 
         } 
         position = index + 1; 
        }
      } while (index >= 0);
      bw.Write(data, position, data.Length - position);
      fileStream.SetLength(fileStream.Position);
    }
  }
  public static void Unix2Mac(string fileName) 
  {
    byte[] data = File.ReadAllBytes(fileName);
    using (FileStream fileStream = File.OpenWrite(fileName)) 
    {
      BinaryWriter bw = new BinaryWriter(fileStream);
      int position = 0;
      int index = 0;
      do 
      {
        index = Array.IndexOf(data, LF, position);
        if (index >= 0) 
        {
          if (index > 0 && data[index -1] == CR) 
          {
            bw.Write(data, position, index - position + 1);
          } else 
          {
            bw.Write(data, position, index - position);
            bw.Write(CR);
          }
          position = index + 1;
        }
      }
      while (index >= 0);
      bw.Write(data, position, data.Length - position);
      fileStream.SetLength(fileStream.Position);
    }
  }
}
    



转载请注明:清风亦平凡 » C#文本文档类型转换(dos2unix、dos2mac、unix2dos、mac2dos、unix2mac、mac2unix)

喜欢 (8)or分享 (0)
支付宝扫码打赏 支付宝扫码打赏 微信打赏 微信打赏
头像
发表我的评论
取消评论

CAPTCHA Image
Reload Image
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址