namereq

パディング 0xFF

Jun 18th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5. namespace PaddingFF
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             FileStream fs = new FileStream(
  12.               @"test.bin", FileMode.Open, FileAccess.Read);
  13.  
  14.             int fileSize = (int)fs.Length; // ファイルのサイズ
  15.             byte[] buf = new byte[fileSize]; // データ格納用配列
  16.  
  17.             int readSize; // Readメソッドで読み込んだバイト数
  18.             int remain = fileSize; // 読み込むべき残りのバイト数
  19.             int bufPos = 0; // データ格納用配列内の追加位置
  20.  
  21.             Console.WriteLine(fileSize);
  22.             Console.WriteLine(fileSize < 64*1024*1024
  23.                 ? "パディングします。" : "パディングしません。");
  24.             while (remain > 0)
  25.             {
  26.                 // Console.WriteLine(remain);
  27.                 // 1024Bytesずつ読み込む
  28.                 readSize = fs.Read(buf, bufPos, Math.Min(1024, remain));
  29.  
  30.                 bufPos += readSize;
  31.                 remain -= readSize;
  32.             }
  33.             fs.Dispose();
  34.  
  35.             if(fileSize < 64 * 1024 * 1024)
  36.             {
  37.                 // パディング 0xFF
  38.                 byte[] byteArray = Enumerable.Repeat<byte>(0xFF, 64 * 1024 * 1024).ToArray();
  39.                 for(int i = 0; i < fileSize; i++)
  40.                 {
  41.                     byteArray[i] = buf[i];
  42.                 }
  43.      
  44.                 //ファイルを作成して書き込む
  45.                 //ファイルが存在しているときは、上書きする
  46.                 System.IO.FileStream fs2 = new System.IO.FileStream(
  47.                     @"padding.bin",
  48.                     System.IO.FileMode.Create,
  49.                     System.IO.FileAccess.Write);
  50.                 //バイト型配列の内容をすべて書き込む
  51.                 fs2.Write(byteArray, 0, byteArray.Length);
  52.                 //閉じる
  53.                 fs2.Close();
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment