FatalBulletHit

Binary Converter

Feb 26th, 2018 (edited)
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #################################################################################################################
  2. # https://stackoverflow.com/a/48676531/9248774
  3. #
  4. # Converts 4 digit binary strings to decimal while sorting out all strings which equal 0 or are greater than 9.
  5. #
  6. # Adjust the source directory and input and output file names (files don't have to be .txt files).
  7. #
  8. $source = "C:\adjust\path"
  9. $input_file = "file_name.extension"
  10. $dec_output = "dec_output.txt"
  11. $bin_output = "bin_output.txt"
  12. #
  13. #
  14. # Using Get-Content on an input file with a size of 1GB or more will cause System.OutOfMemoryExceptions,
  15. # therefore a large file gets temporarily split up.
  16. #
  17. $split_size = 100MB
  18. $echo_ss = $split_size/1MB
  19. #
  20. #
  21. # This adds carriage returns to the temporary file after each 16'384th character. Although the sweet spot is
  22. # somewhere around 18'000 characters, the line length needs to be dividable by 4 and at best fit exactly n times
  23. # into the temporary file; using 16'384 or characters (that is exactly 16 KB) ensures that.
  24. #
  25. $line_length = 16384
  26. #
  27. #
  28. # Thanks @BenN (https://superuser.com/a/1292916/868077)
  29. # Thanks @Bob (https://superuser.com/a/1295082/868077)
  30. #################################################################################################################
  31.  
  32. Set-Location $source
  33.  
  34. if (Test-Path $bin_output) {
  35.  
  36.     $name = (Get-Item $bin_output).Basename
  37.     $ext = (Get-Item $bin_output).Extension
  38.     $num = 1
  39.  
  40.     while ($num -le 9999) {
  41.  
  42.         $test = $name+"_"+$num+$ext
  43.  
  44.         if (Test-Path $test) {
  45.  
  46.             $num += 1
  47.  
  48.         } else {
  49.  
  50.             break
  51.  
  52.         }
  53.  
  54.     }
  55.  
  56.     Rename-Item $bin_output $test
  57.     $a = "`n`n Renamed 'bin_output'!"
  58.  
  59. }
  60.  
  61. if (Test-Path $dec_output) {
  62.  
  63.     $name = (Get-Item $dec_output).Basename
  64.     $ext = (Get-Item $dec_output).Extension
  65.     $num = 1
  66.  
  67.     while ($num -le 9999) {
  68.  
  69.         $test = $name+"_"+$num+$ext
  70.  
  71.         if (Test-Path $test) {
  72.  
  73.             $num += 1
  74.            
  75.         } else {
  76.        
  77.             break
  78.        
  79.         }
  80.  
  81.     }
  82.  
  83.     Rename-Item $dec_output $test
  84.     $b = "`n`n Renamed 'dec_output'!"
  85.  
  86. }
  87.  
  88. if (Test-Path ".\_temp") {
  89.  
  90.     "`n"
  91.    
  92.     while ($overwrite -ne "true" -and $overwrite -ne "false") {
  93.    
  94.         $overwrite = Read-Host ' Splitted files already/still exists! Delete and recreate?'
  95.        
  96.         if ($overwrite -match "y") {
  97.        
  98.             $overwrite = "true"
  99.             Remove-Item .\_temp -force -recurse
  100.             $c = " Deleted existing splitted files and creating new ones!"
  101.            
  102.         } elseif ($overwrite -match "n") {
  103.        
  104.             $overwrite = "false"
  105.            
  106.         } elseif ($overwrite -match "c") {
  107.        
  108.             exit
  109.            
  110.         } else {
  111.        
  112.             "`n"
  113.             Write-Host " Error: Invalid input!" "`n" " Type 'y' for 'yes'." " Type 'n' for 'no'." " Type 'c' for 'cancel'."
  114.             "`n"
  115.             "`n"
  116.            
  117.         }
  118.    
  119.     }
  120.    
  121. }
  122.  
  123. Clear-Host
  124.  
  125. "`n"
  126.  
  127. while ($delete -ne "true" -and $delete -ne "false") {
  128.    
  129.     $delete = Read-Host ' Delete splitted files afterwards?'
  130.    
  131.     if ($delete -match "y") {
  132.    
  133.         $delete = "true"
  134.         $d = "`n`n Splitted files will be deleted afterwards!"
  135.    
  136.     } elseif ($delete -match "n") {
  137.    
  138.         $delete = "false"
  139.         $d = "`n`n Splitted files will not be deleted afterwards!"
  140.        
  141.     } elseif ($delete -match "c") {
  142.        
  143.         exit
  144.        
  145.     } else {
  146.        
  147.             "`n"
  148.             Write-Host " Error: Invalid input!" "`n" " Type 'y' for 'yes'." " Type 'n' for 'no'." " Type 'c' for 'cancel'."
  149.             "`n"
  150.             "`n"
  151.        
  152.     }
  153.        
  154. }
  155.  
  156. Clear-Host
  157.  
  158. "`n"; "`n"; "`n"; "`n"; "`n"; "`n"
  159.  
  160. $a
  161. $b
  162. $d
  163.  
  164. $start_o = (Get-Date)
  165.  
  166. if ($overwrite -ne "false") {
  167.  
  168.     $c
  169.     "`n"
  170.     $start = Get-Date
  171.     New-Item -ItemType directory -Path ".\_temp" >$null 2>&1
  172.     [Environment]::CurrentDirectory = Get-Location
  173.     $bytes = New-Object byte[] 4096
  174.     $in_file = [System.IO.File]::OpenRead($input_file)
  175.     $file_count = 0
  176.     $finished = $false
  177.  
  178.     if ((Get-Item $input_file).length -gt $split_size) {
  179.  
  180.         Write-Host " Input file larger than $echo_ss MB!"
  181.         Write-Host "     Splitting input file and inserting carriage returns..."
  182.         $v=([MATH]::Floor([decimal]((Get-Item $input_file).Length/100MB)))
  183.         $sec_rem = -1
  184.        
  185.         while (!$finished) {
  186.            
  187.             $perc = [MATH]::Round($file_count/$v*100)
  188.             $file_count++
  189.             $bytes_to_read = $split_size
  190.             $out_file = New-Object System.IO.FileStream ".\_temp\_temp_$file_count.tmp",CreateNew,Write,None
  191.            
  192.             while ($bytes_to_read) {
  193.                
  194.                 $bytes_read = $in_file.Read($bytes, 0, [Math]::Min($bytes.Length, $bytes_to_read))
  195.                
  196.                 if (!$bytes_read) {
  197.                    
  198.                     $finished = $true
  199.                     break
  200.                
  201.                 }
  202.                
  203.                 $bytes_to_read -= $bytes_read
  204.                 $out_file.Write($bytes, 0, $bytes_read)
  205.            
  206.             }
  207.            
  208.             if (($i = $file_count-1) -gt 0) {
  209.                
  210.                 (Get-Content ".\_temp\_temp_$i.tmp") -Replace ".{$line_length}", "$&`r`n" | Set-Content ".\_temp\_temp_$i.tmp"
  211.            
  212.             }
  213.            
  214.             $out_file.Dispose()
  215.             $sec_elaps = (Get-Date) - $start
  216.             $sec_rem = ($sec_elaps.TotalSeconds/$file_count) * ($v-$file_count+1)
  217.             Write-Progress -Id 1 -Activity "Splitting input file and inserting carriage returns..." -Status "Progress ($perc%):" -PercentComplete ($perc) -SecondsRemaining $sec_rem
  218.            
  219.         }
  220.        
  221.         $in_file.Dispose()
  222.         (Get-Content ".\_temp\_temp_$file_count.tmp") -Replace ".{$line_length}", "$&`r`n" | Set-Content ".\_temp\_temp_$file_count.tmp"
  223.         Write-Progress -Id 1 -Activity null -Completed
  224.    
  225.     } else {
  226.        
  227.         if ((Get-Item $input_file).length -lt $split_size) {
  228.        
  229.             " Input file smaller than $echo_ss MB!"
  230.            
  231.         } else {
  232.        
  233.             " Input file exactly $echo_ss MB!"
  234.            
  235.         }
  236.        
  237.         Write-Host "  Inserting carriage returns..."
  238.         (Get-Content $input_file) -Replace ".{$line_length}", "$&`r`n" | Set-Content ".\_temp\_temp_1.tmp"; $file_count = 1
  239.    
  240.     }
  241.    
  242.     $dur = (Get-Date) - $start
  243.     Write-Host "`n     Done! Duration:"$dur.ToString("hh\:mm\:ss\.fff")
  244.     "`n"
  245.  
  246. } else {
  247.  
  248.     "`n"
  249.     Write-Host " Continuing with existing files..."
  250.     "`n"
  251.     Get-ChildItem ".\_temp\*" -File -Include *.tmp | ForEach-Object -Process {$file_count++}
  252.    
  253. }
  254.  
  255. Write-Host " Converting binary into decimal..."
  256. $sec_rem = -1
  257. $start = Get-Date
  258.  
  259. Get-ChildItem ".\_temp\*" -File -Include *.tmp | ForEach-object -Process {
  260.  
  261.     $cur_file++
  262.     $line_count = (Get-Content ".\_temp\_temp_$cur_file.tmp").count
  263.  
  264.     ForEach ($line in Get-Content ".\_temp\_temp_$cur_file.tmp") {
  265.  
  266.         $cur_line++
  267.         $perc = [MATH]::Round(($cur_file-1+($cur_line/$line_count))/$file_count*100)
  268.         $n = 0
  269.        
  270.         if ($line.length -ge 4) {
  271.            
  272.             while ($n -lt $line.length) {
  273.                
  274.                 $dec = 0
  275.                 $bin = $line.substring($n,4)
  276.                 $dec = ([Convert]::ToInt32($bin,2))
  277.                
  278.                 if ($dec -gt 0 -and $dec -le 9) {
  279.                    
  280.                     $temp_dec = "$temp_dec$dec"
  281.                     $temp_bin = "$temp_bin$bin"
  282.                
  283.                 }
  284.                
  285.                 $n += 4
  286.            
  287.             }
  288.        
  289.         $temp_dec | Add-Content $dec_output -Encoding ascii -NoNewline
  290.         $temp_bin | Add-Content $bin_output -Encoding ascii -NoNewline
  291.         Clear-Variable -Name "temp_dec", "temp_bin"
  292.        
  293.         }
  294.        
  295.         $sec_elaps = (Get-Date) - $start
  296.         $sec_rem = ($sec_elaps.TotalSeconds/($cur_file-1+($cur_line/$line_count))) * ($file_count-($cur_file-1+($cur_line/$line_count)))
  297.         Write-Progress -ID 2 -Activity "Converting binary into decimal..." -Status "Progress ($perc%):" -PercentComplete ($perc) -SecondsRemaining $sec_rem -CurrentOperation "Current file: '_temp_$cur_file.tmp'"
  298.    
  299.     }
  300.    
  301.     Clear-Variable -Name "cur_line"
  302.  
  303. }
  304.  
  305. Write-Progress -Activity null -Completed
  306. $dur = (Get-Date) - $start
  307. Write-Host "`n     Done! Duration:"$dur.ToString("hh\:mm\:ss\.fff")
  308. "`n"
  309.  
  310. if ($delete -eq "true") {
  311.  
  312.     Remove-Item ".\_temp" -Force -Recurse
  313.  
  314. }
  315.  
  316. "`n"
  317. "`n"
  318. Write-Host " Script finished!"
  319. Write-Host "     Start time:   "$start_o.ToString("dd\.MM\.yyyy\ hh\:mm\:ss\.fff")
  320. Write-Host "     End time:     "(Get-Date).ToString("dd\.MM\.yyyy\ hh\:mm\:ss\.fff")
  321. $dur = (Get-Date) - $start_o
  322. Write-Host "`n     Duration:     "$dur.ToString("hh\:mm\:ss\.fff")
  323. "`n`n`n"
  324.  
  325. Pause
  326. Exit
Add Comment
Please, Sign In to add comment