Posted By

jimfred on 03/11/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

umang_nine
danfinrud55


.net, console app, convert a bin file to CSV


 / Published in: C#
 

Simple program to convert a BIN file to CSV. Demonstrates .net usage of: * command-line parameters, * binary and text file access * looping thru a file using the catch (EndOfStreamException) construct * byte-swapping for endianness

I used this to convert binary MICR data to a CSV for use in Excel

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5.  
  6. namespace ConvertMicrBinToCsv
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. if (args.Length < 2)
  13. {
  14. Console.WriteLine("Expecting 2 parameters: a .bin file and a .csv file.");
  15. return;
  16. }
  17.  
  18. foreach (string arg in args)
  19. {
  20. Console.WriteLine(arg);
  21. }
  22.  
  23. BinaryReader br = new BinaryReader(File.Open(args[0], FileMode.Open ));
  24. StreamWriter sw = new StreamWriter(File.Open(args[1], FileMode.Create));
  25.  
  26. try
  27. {
  28. while (true)
  29. {
  30. UInt16 val = br.ReadUInt16();
  31. UInt16 i = (UInt16)System.Net.IPAddress.NetworkToHostOrder((Int16)val); // endianness
  32. sw.WriteLine("{0}", i);
  33. }
  34. }
  35. catch (EndOfStreamException e)
  36. {
  37. // caught and ignored.
  38. }
  39. finally
  40. {
  41. br.Close();
  42. sw.Close();
  43. }
  44.  
  45. return;
  46. }
  47. }
  48. }

Report this snippet  

You need to login to post a comment.