Solved

Binary to Text Conversion in C#

Posted on 2008-10-18
8
3,616 Views
Last Modified: 2013-12-17
I am trying to convert a binary file to text. I have been told I need to know the ISO format. In order to convert. However, I have been having a difficult time trying to do that. I placed a sample of the file below. I have tried converting the using the code below, but it gives me an output of System.byte[]. I have searched the EE forum and can only find conversions from text to binary.



Below is a portion of the file:


  µ<  aÕ            N     N\ Fr] F·                 ¥~~ÿÿÿÿ)                      00001143721184914510 41021   004A12012 42000             43031               42000                    0839411671   2                                                                                                                         41007   41021                                                                                                                                                    :Õ            0   R   ×\ F¬] F\                  g`X`ÿÿÿÿ=                       00001143881184914647 61015   003A12010 62000             63069               62000                    0399795484   2                                                                                                                         61001   61015                                                                                                                                                    SÕ             M   M   4] F] F            K       Ùê Ùê ÿÿÿÿ                           00001144031184914736 21241             21241                                 21241                    16960                                                                                                                                                                                                                                                                                                   Õ             O   O   5] F] F            M       :K:Kÿÿÿÿ                           00001144041184914738 31338             31338                                 31338                    16961                                                                                                                                                                                                                                                                                                   ~Õ             Q   Q   6] F!] F            O       àñ àñ ÿÿÿÿ                           00001144051184914739 21248             21248                                 21248                    16962                                                                                                                                                                                                                                                                                                   xÕ                 Æ] F] F                    ôÿÿôÿÿÿÿÿÿ3                          00001144341184914824 52000   006B13014 52000                                 52000  


Thanks in advance for the help.
My code:
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace SampleReadFile
{
    class Program
    {
 
        
 
        public static void Main()
        {
            ReadFromFile("C:\\avaya file pull\\chr0003");
        }
        static void ReadFromFile(string filename)
        {
           
            StreamReader SR;
            //string S;
           // Byte byteArray;
           // Byte byteArray = System.Text.Encoding.UTF8.GetBytes(S);
            SR = File.OpenText(filename);
            //S = SR.ReadLine();
            Byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(SR.ReadLine());
            //System.Text.Encoding.UTF8.GetBytes(S);
            while (byteArray != null)
            {
                Console.WriteLine(byteArray);
               // byteArray = SR.ReadLine();
            }
            SR.Close();
            Console.ReadLine();
        }
 
        
        
 
 
    }
}

Open in new window

0
Comment
Question by:conflikq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:CuteBug
ID: 22750612
Try converting to Base64 string.
Use the Convert.ToBase64String() method.

http://www.csharp411.com/convert-binary-to-base64-string/
http://www.tanguay.info/webold/codeExample.php5?id=706
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22750638
specific answer depends on which file format is made, or which software.
0
 
LVL 1

Author Comment

by:conflikq
ID: 22750679
Thanks for your reply.

I tried to Convert.ToBase64String() with code below. The text the output below. I grabbed this file from a sample when I downloaded the program that is doing the task I need to get this program to do. The site is http://echi-converter.rubyforge.org/. The program works the problem is it's written in RUBY and my company needs me to do it in C#.

DAAAAO+/vTwAAO+/vdWRAB8AAAAAAAAAAAAAAE4AAAAFAQAATlzvv71Gcl3vv71G77+9AAAAAAAAAAAAAAAAAAAACwAAAO+/vQHvv70B77+9Ae+/vQHvv73vv73vv73vv70pAAMAAAADAAAAAAAAAQECBAABAAABAAAAAAAAAAAAMDAwMDExNDM3MjExODQ5MTQ1MTAANDEwMjEAAAAwMDRBMTIwMTIANDIwMDAAAAAAAAAAAAAAAAAANDMwMzEAAAAAAAAAAAAAAAAAAAA0MjAwMAAAAAAAAAAAAAAAAAAAAAAAAAAAMDgzOTQxMTY3MQAAADIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANDEwMDcAAAA0MTAyMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADvv73VkQAAAAAAAAAAAAAAAABRAAAAUQAAADZd77+9Ru+/vV3vv71GAAAAAAAAAAAAAAAATwAAAAAAAADvv70E77+9AO+/vQTvv70A77+977+977+977+9AAAAAAAAAAAAAAAAAAABAwQAAQAAAAAAAAAAAAAAADAwMDAxMTQ0MDUxMTg0OTE0NzM5ADIxMjQ4AAAAAAAAAAAAAAAAADIxMjQ4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjEyNDgAAAAAAAAAAAAAAAAAAAAAAAAAADE2OTYyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

There is an example of the conversion of this file.

printing data file: chr0003
p_callrec::get_data
PRINTING FILE: chr0003
record_size=493 version=12 file_number=15541
CALL RECORD: #1
              callid : 9557402
             acwtime : 31
          onholdtime : 0
         consulttime : 0
            disptime : 78
            duration : 261
            segstart : 1184914510
             segstop : 1184914802
            talktime : 183
           netintime : 0
        origholdtime : 0
           queuetime : 0
            ringtime : 11
         dispivector : 421
           dispsplit : 414
        firstivector : 400
              split1 : 414
              split2 : -1
              split3 : -1
          trunkgroup : 41
            tk_locid : 3
          orig_locid : 0
        answer_locid : 3
           obs_locid : 0
             uui_len : 0
              assist : 0
    audio_difficulty : 0
          conference : 0
           da_queued : 0
            hold_abn : 0
           malicious : 0
      observing_call : 0
         transferred : 0
        agt_released : 1
              acdnum : 1
           call_disp : 2
        disppriority : 4
               holds : 0
             segment : 1
           ansreason : 0
          origreason : 0
         dispsklevel : 1
           events[0] : 0
           events[1] : 0
           events[2] : 0
           events[3] : 0
           events[4] : 0
           events[5] : 0
           events[6] : 0
           events[7] : 0
           events[8] : 0
                ucid : 00001143721184914510

Any thoughts on what I could be missing. Also here is the ruby code where the conversion works.

require 'rubygems'
require 'active_record'
require 'faster_csv'
require 'net/ftp'
require 'net/smtp'
require 'fileutils'
require 'uuidtools'
require 'thread'
require $workingdir + '/ftp_fetcher.rb'

class Logger
  #Change the logging format to include a timestamp
  def format_message(severity, timestamp, progname, msg)
    "#{timestamp} (#{$$}) #{msg}\n"
  end
end

module EchiConverter
  def connect_database
    databaseconfig = $workingdir + '/../config/database.yml'
    dblogfile = $workingdir + '/../log/database.log'
    ActiveRecord::Base.logger = Logger.new(dblogfile, $config["log_number"], $config["log_length"])  
    case $config["log_level"]
      when 'FATAL'
        ActiveRecord::Base.logger.level = Logger::FATAL
      when 'ERROR'
        ActiveRecord::Base.logger.level = Logger::ERROR
      when 'WARN'
        ActiveRecord::Base.logger.level = Logger::WARN
      when 'INFO'
        ActiveRecord::Base.logger.level = Logger::INFO
      when 'DEBUG'
        ActiveRecord::Base.logger.level = Logger::DEBUG
    end
    begin
      ActiveRecord::Base.establish_connection(YAML::load(File.open(databaseconfig)))
      @log.info "Initialized the database"
    rescue => err
      @log.fatal "Could not connect to the database - " + err
      if $config["send_email"] == true
        send_email_alert "DATABASE"
      end
    end
  end
 
  #Method to open our application log
  def initiate_logger
    logfile = $workingdir + '/../log/application.log'
    @log = Logger.new(logfile, $config["log_number"], $config["log_length"])
    case $config["log_level"]
      when 'FATAL'
        @log.level = Logger::FATAL
      when 'ERROR'
        @log.level = Logger::ERROR
      when 'WARN'
        @log.level = Logger::WARN
      when 'INFO'
        @log.level = Logger::INFO
      when 'DEBUG'
        @log.level = Logger::DEBUG
    end
  end
 
  #Method to send alert emails
  def send_email_alert reason
    @log.debug "send_email_alert method"
    begin
      Net::SMTP.start($config["smtp_server"], $config["smtp_port"]) do |smtp|
        smtp.open_message_stream('donotreply@echi-converter.rubyforge.org', [$config["alert_email_address"]]) do |f|
          f.puts "From: donotreply@echi-converter.rubyforge.org"
          f.puts "To: " + $config['alert_email_address']
          f.puts "Subject: ECHI-Converter Failure"
          case reason
          when "DATABASE"
            f.puts "Failed to connect to the database."
          when "FTP"
            f.puts "Failed to connect to the ftp server."
          end
            f.puts " "
            f.puts "Please check the ECHI-Converter environment as soon as possible."
        end
      end
    rescue => err
      @log.warn err
    end
  end
 
  #Set the working directory to copy processed files to, if it does not exist creat it
  #Directory names based on year/month so as not to exceed 5K files in a single directory
  def set_directory working_directory
    @log.debug "set_directory method"
    time = Time.now
    directory_year = working_directory + "/../files/processed/" + time.year.to_s
    directory_month = directory_year + "/" + time.month.to_s
   
    if File.exists?(directory_month) == false
      if File.exists?(directory_year) == false
        Dir.mkdir(directory_year)
      end
      Dir.mkdir(directory_month)
    end      
   
    return directory_month
  end
 
  #Method to get FTP files
  def get_ftp_files
    @log.debug "get_ftp_files method"
    filelist_fetcher = FtpFetcher.new
    filequeue = filelist_fetcher.fetch_list @log
   
    if filequeue == nil
      return -1
    end
   
    if $config["max_ftp_sessions"] > 1 && filequeue.length > 4
      if $config["max_ftp_sessions"] > filequeue.length
        @log.info "Using " + filequeue.length.to_s + " ftp sessions to fetch files"
        my_threads = []
        cnt = 0
        while cnt < filequeue.length
          my_threads << Thread.new do
            fetcher = Fetcher.new
            result = fetcher.fetch_ftp_files filequeue, @log
          end
          cnt += 1
        end
        my_threads.each { |aThread|  aThread.join }
      else
        @log.info "Using " + $config["max_ftp_sessions"].to_s + " ftp sessions to fetch files"
        my_threads = []
        cnt = 0
        while cnt < $config["max_ftp_sessions"]
          my_threads << Thread.new do
            fetcher = FtpFetcher.new
            result = fetcher.fetch_ftp_files filequeue, @log
          end
          cnt += 1
        end
        my_threads.each { |aThread|  aThread.join }
      end
    else
      @log.info "Using a single ftp session to fetch the files"
      fetcher = FtpFetcher.new
      result = fetcher.fetch_ftp_files filequeue, @log
    end
    if result == false
      if $config["send_email"] == true
        send_email_alert "FTP"
      end
    end
  end
 
  #Method to write to the log table
  def log_processed_file type, filedata
    @log.debug "log_processed file method"
    begin
      echi_log = EchiLog.new
      echi_log.filename = filedata["name"]
      if type == 'BINARY'
        echi_log.filenumber = filedata["number"]
        echi_log.version = filedata["version"]
      end
      echi_log.records = filedata["cnt"]
      echi_log.processedat = Time.now
      echi_log.save
    rescue => err
      @log.info "Error creating ECHI_LOG entry - " + err
      return -1
    end
    return 0
  end
 
  #Method for parsing the various datatypes from the ECH file
  def dump_binary type, length
    @log.debug "dump_binary method"
    case type
    when 'int'
      #Process integers, assigning appropriate profile based on length
      #such as long int, short int and tiny int.
      case length
      when 4
        value = @binary_file.read(length).unpack("l").first.to_i
      when 2
        value = @binary_file.read(length).unpack("s").first.to_i
      when 1
        value = @binary_file.read(length).unpack("U").first.to_i
      end
    #Process appropriate intergers into datetime format in the database
    when 'datetime'
      case length
      when 4
        value = @binary_file.read(length).unpack("l").first.to_i
        value = Time.at(value)
      end
    #Process strings
    when 'str'
      value = @binary_file.read(length).unpack("M").first.to_s.rstrip
    #Process individual bits that are booleans
    when 'bool'
      value = @binary_file.read(length).unpack("b8").last.to_s
    #Process that one wierd boolean that is actually an int, instead of a bit
    when 'boolint'
      value = @binary_file.read(length).unpack("U").first.to_i
      #Change the values of the field to Y/N for the varchar(1) representation of BOOLEAN
      if value == 1
        value = 'Y'
      else
        value = 'N'
      end
    end
    return value
  end
 
  #Mehtod that performs the conversions
  def convert_binary_file filename
    @log.debug "convert_binary_file"
    #Open the file to process
    echi_file = $workingdir + "/../files/to_process/" + filename
    @binary_file = open(echi_file,"rb")
    @log.debug "File size: " + @binary_file.stat.size.to_s
   
    #Read header information first
    fileversion = dump_binary 'int', 4
    @log.debug "Version " + fileversion.to_s
    filenumber = dump_binary 'int', 4
    @log.debug "File_number " + filenumber.to_s
   
    begin
      #Perform a transaction for each file, including the log table
      #in order to commit as one atomic action upon success
      EchiRecord.transaction do
        bool_cnt = 0
        bytearray = nil
        @record_cnt = 0
        while @binary_file.eof == FALSE do
          @log.debug '<====================START RECORD ' + @record_cnt.to_s + ' ====================>'
          echi_record = EchiRecord.new
          @echi_schema["echi_records"].each do | field |
            #We handle the 'boolean' fields differently, as they are all encoded as bits in a single 8-bit byte
            if field["type"] == 'bool'
              if bool_cnt == 0
                bytearray = dump_binary field["type"], field["length"]
              end
              #Ensure we parse the bytearray and set the appropriate flags
              #We need to make sure the entire array is not nil, in order to do Y/N
              #if Nil we then set all no
              if bytearray != '00000000'
                if bytearray.slice(bool_cnt,1) == '1'
                  value = 'Y'
                else
                  value = 'N'
                end
              else
                value = 'N'
              end
              @log.debug field["name"] + " { type => #{field["type"]} & length => #{field["length"]} } value => " + value.to_s
              bool_cnt += 1
              if bool_cnt == 8
                bool_cnt = 0
              end
            else
              #Process 'standard' fields
              value = dump_binary field["type"], field["length"]
              @log.debug field["name"] + " { type => #{field["type"]} & length => #{field["length"]} } value => " + value.to_s
            end
            echi_record[field["name"]] = value
          end
          echi_record.save
     
          #Scan past the end of line record if enabled in the configuration file
          if $config["echi_read_extra_byte"] == "Y"
            @binary_file.read(1)
          end
          @log.debug '<====================STOP RECORD ' + @record_cnt.to_s + ' ====================>'
          @record_cnt += 1
        end
        @binary_file.close
      end
    rescue => err
        @log.info "Error processing ECHI file - " + err
    end
         
    #Move the file to the processed directory
    FileUtils.mv(echi_file, @processeddirectory)
   
    if $config["echi_process_log"] == "Y"
      log_processed_file "BINARY", { "name" => filename, "number" => filenumber, "version" => fileversion, "cnt" => @record_cnt }
    end
   
    return @record_cnt
  end

  def process_ascii filename
    @log.debug "process_ascii method"
    echi_file = $workingdir + "/../files/to_process/" + filename
 
    begin
      #Perform a transaction for each file, including the log table
      #in order to commit as one atomic action upon success
      EchiRecord.transaction do
        @record_cnt = 0
        FasterCSV.foreach(echi_file) do |row|
          if row != nil
            @log.debug '<====================START RECORD ' + @record_cnt.to_s + ' ====================>'
            echi_record = EchiRecord.new
            cnt = 0
            @echi_schema["echi_records"].each do | field |
              if field["type"] == "bool" || field["type"] == "boolint"
                case row[cnt]
                when "0"
                  echi_record[field["name"]] = "N"
                when "1"
                  echi_record[field["name"]] = "Y"
                when nil
                  echi_record[field["name"]] = "N"
                else
                  echi_record[field["name"]] = "Y"
                end
                @log.debug field["name"] + ' == ' + row[cnt]
              else
                echi_record[field["name"]] = row[cnt]
                if row[cnt] != nil
                  @log.debug field["name"] + ' == ' + row[cnt]
                end
              end
              cnt += 1
            end
            echi_record.save
            @log.debug '<====================STOP RECORD ' + @record_cnt.to_s + ' ====================>'
            @record_cnt += 1
          end
        end
      end
    rescue => err
      @log.info "Error processing ECHI file - " + err
    end
 
    #Move the file to the processed directory
    FileUtils.mv(echi_file, @processeddirectory)
 
    if $config["echi_process_log"] == "Y"
      log_processed_file nil, { "name" => filename, "cnt" => @record_cnt }
    end
 
    return @record_cnt
  end

  def insert_dat_data tablename, row
    @log.debug "insert_dat_data method"
    begin
      case tablename
      when "echi_acds"
        echi_dat_record = EchiAcd.new
      when "echi_agents"
        echi_dat_record = EchiAgent.new
      when "echi_reasons"
        echi_dat_record = EchiReason.new
      when "echi_cwcs"
        echi_dat_record = EchiCwc.new
      when "echi_splits"
        echi_dat_record = EchiSplit.new
      when "echi_trunks"
        echi_dat_record = EchiTrunk.new
      when "echi_vdns"
        echi_dat_record = EchiVdn.new
      when "echi_vectors"
        echi_dat_record = EchiVector.new
      end
      cnt = 0
      @echi_schema[tablename].each do | field |
        echi_dat_record[field["name"]] = row[cnt]
        cnt += 1
      end
      echi_dat_record.save
    rescue => err
      @log.info "Unable to insert " + tablename + " file record - " + err
    end
 
  end
 
  #Move the file to the archive location
  def archive_file file, record_cnt
    @log.debug "archive_file method"
    if File.exists?(file["filename"])
      case file["name"]
      when "echi_acds"
        filename_elements = $config["echi_acd_dat"].split(".")
      when "echi_agents"
        filename_elements = $config["echi_agent_dat"].split(".")
      when "echi_reasons"
        filename_elements = $config["echi_aux_rsn_dat"].split(".")
      when "echi_cwcs"
        filename_elements = $config["echi_cwc_dat"].split(".")
      when "echi_splits"
        filename_elements = $config["echi_split_dat"].split(".")
      when "echi_vdns"
        filename_elements = $config["echi_vdn_dat"].split(".")
      when "echi_trunks"
        filename_elements = $config["echi_trunk_group_dat"].split(".")
      when "echi_vectors"
        filename_elements = $config["echi_vector_dat"].split(".")
      end
      new_filename = filename_elements[0] + "_" + UUID.timestamp_create.to_s + "." + filename_elements[1]
      target_file = @processeddirectory + "/" + new_filename
      begin
        FileUtils.mv(file["filename"], target_file)
        if $config["echi_process_log"] == "Y"
          log_processed_file nil, { "name" => new_filename, "cnt" => record_cnt }
        end
      rescue => err
        @log.info "Unable to move processed file - " + err
      end
    end
  end
 
  #Process the appropriate table name
  def process_proper_table file
    @log.debug "process_proper_table method"
    @record_cnt = 0
    process_file = File.open(file["filename"])
    process_file.each do |row|
      if row != nil
        begin
          field = row.rstrip.split('|')
          #Make sure we do not process the extra rows that Avaya nicely throws in the dat files for no good reason that serve as keys
          if field.length > 1
            @log.debug '<====================START ' + file["name"] + ' RECORD ' + @record_cnt.to_s + ' ====================>'
            case file["name"]
            when "echi_acds"
              record = EchiAcd.find(:first, :conditions => [ "acd_number = ? AND acd_id = ?", field[1], field[0]])
            when "echi_agents"
              record = EchiAgent.find(:first, :conditions => [ "login_id = ? AND group_id = ?", field[1], field[0]])
            when "echi_reasons"
              record = EchiReason.find(:first, :conditions => [ "aux_reason = ? AND group_id = ?", field[1], field[0]])
            when "echi_cwcs"
              record = EchiCwc.find(:first, :conditions => [ "cwc = ? AND group_id = ?", field[1], field[0]])
            when "echi_splits"
              record = EchiSplit.find(:first, :conditions => [ "split_number = ? AND acd_number = ?", field[1], field[0]])
            when "echi_trunks"
              record = EchiTrunk.find(:first, :conditions => [ "trunk_group = ? AND acd_number = ?", field[1], field[0]])
            when "echi_vdns"
              record = EchiVdn.find(:first, :conditions => [ "vdn = ? AND group_id = ?", field[1], field[0]])
            when "echi_vectors"
              record = EchiVector.find(:first, :conditions => [ "vector_number = ? AND acd_number = ?", field[1], field[0]])
            end
            if record != nil
              if record.name != field[2]
                record.name = field[2]
                record.update
                @record_cnt += 1
                @log.debug "Updated record - " + field.inspect
              else
                @log.debug "No update required for - " + field.inspect
              end
            else
              insert_dat_data file["name"], field
              @record_cnt += 1
              @log.debug "Inserted new record - " + field.inspect
            end
            @log.debug '<====================STOP ' + file["name"] + ' RECORD ' + @record_cnt.to_s + ' ====================>'
          end
        rescue => err
          @log.info "Error processing ECHI record in process_proper_table_method - " + err
        end
      end
    end
     
    process_file.close
   
    archive_file file, @record_cnt
  end
 
  #Method to insert data into 'echi_agents' based on agname.dat
  def process_dat_files
    @log.debug "process_dat_files method"
    dat_files = Array.new
    dat_files[0] = { "name" => "echi_acds", "filename" => $workingdir + "/../files/to_process/"  + $config["echi_acd_dat"] }
    dat_files[1] = { "name" => "echi_agents", "filename" => $workingdir + "/../files/to_process/"  + $config["echi_agent_dat"] }
    dat_files[2] = { "name" => "echi_reasons", "filename" => $workingdir + "/../files/to_process/"  + $config["echi_aux_rsn_dat"] }
    dat_files[3] = { "name" => "echi_cwcs", "filename" => $workingdir + "/../files/to_process/"  + $config["echi_cwc_dat"] }
    dat_files[4] = { "name" => "echi_splits", "filename" => $workingdir + "/../files/to_process/"  + $config["echi_split_dat"] }
    dat_files[5] = { "name" => "echi_trunks", "filename" => $workingdir + "/../files/to_process/"  + $config["echi_trunk_group_dat"] }
    dat_files[6] = { "name" => "echi_vdns", "filename" => $workingdir + "/../files/to_process/"  + $config["echi_vdn_dat"] }
    dat_files[7] = { "name" => "echi_vectors", "filename" => $workingdir + "/../files/to_process/"  + $config["echi_vector_dat"] }
       
    dat_files.each do |file|
      if File.exists?(file["filename"]) && File.stat(file["filename"]).size > 0
        case file["name"]
        when "echi_acds"
          EchiAcd.transaction do
            process_proper_table file
          end
        when "echi_agents"
          EchiAgent.transaction do
            process_proper_table file
          end
        when "echi_reasons"
          EchiReason.transaction do
            process_proper_table file
          end
        when "echi_cwcs"
          EchiCwc.transaction do
            process_proper_table file
          end
        when "echi_splits"
          EchiSplit.transaction do
            process_proper_table file
          end
        when "echi_trunks"
          EchiTrunk.transaction do
            process_proper_table file
          end
        when "echi_vdns"
          EchiVdn.transaction do
            process_proper_table file
          end
        when "echi_vectors"
          EchiVector.transaction do
            process_proper_table file
          end
        end
      else
        archive_file file, 0
      end
    end
  end

  require $workingdir + '/echi-converter/version.rb'
end





        static void ReadFromFile(string filename)
        {
           
            StreamReader SR;
            //string S;
           // Byte byteArray;
           // Byte byteArray = System.Text.Encoding.UTF8.GetBytes(S);
            SR = File.OpenText(filename);
            //S = SR.ReadLine();
            Byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(SR.ReadLine());
 
           String conversion = Convert.ToBase64String(byteArray);
            
            //System.Text.Encoding.UTF8.GetBytes(S);
            while (conversion != null)
            {
                Console.WriteLine(conversion);
               // byteArray = SR.ReadLine();
            }
            SR.Close();
            Console.ReadLine();
        }

Open in new window

0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 16

Expert Comment

by:CuteBug
ID: 22750854
The above conversion works only if you know the proper structure of the text you are trying to decode from binary.

This is what the code in Ruby does.

Once you know the structure properly, you can use the various methods of BinaryReader (e.g. ReadByte, ReadString, etc) to decode it.
0
 
LVL 1

Author Comment

by:conflikq
ID: 22752580
Is there a way to figure out the file structure without being able to contact the person who designed it?
0
 
LVL 1

Author Comment

by:conflikq
ID: 22752709
I found a document that shows me the byte size of each field in that file. For instance, ACWTIME
Type: integer
Length: 4 bytes.


How would I read 4 bytes instead of reading one byte at a time?
0
 
LVL 16

Accepted Solution

by:
CuteBug earned 500 total points
ID: 22754447
0
 
LVL 1

Author Closing Comment

by:conflikq
ID: 31507512
Thanks for your help. I was able to resolve be having someone explain the file format.

Thanks a lot
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Progress
Starting up a Project

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question