Simple hex excercises

controllers.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. function MainController($scope, $routeParams) {
  3. $scope.allowedLengths = [4, 8, 16, 32, 64, 128, 256];
  4. $scope.binaryLength = 64;
  5. $scope.binary = generateBinary($scope.binaryLength);
  6. $scope.parts = [];
  7. $scope.hex = "";
  8. $scope.displayResults = function () {
  9. return $scope.hex.length > 0;
  10. };
  11. $scope.generate = function () {
  12. $scope.binary = generateBinary($scope.binaryLength);
  13. $scope.parts = [];
  14. $scope.hex = "";
  15. return false;
  16. };
  17. $scope.solve = function () {
  18. $scope.parts = [];
  19. $scope.hex = "";
  20. var original = $scope.binary;
  21. while (original.length > 0) {
  22. // Find the 16 bytes we need.
  23. var part = original.substr(-4);
  24. var character = binaryToHex(part);
  25. // Divvy this stuff up.
  26. $scope.parts.push({ "binary": part, "hex": character });
  27. $scope.hex = character + $scope.hex;
  28. // Remove the last four.
  29. original = original.substr(0, original.length - 4);
  30. }
  31. return false;
  32. };
  33. }
  34. function DumpController($scope, $routeParams) {
  35. $scope.columns = 16;
  36. $scope.fileApi = false;
  37. $scope.files = [];
  38. $scope.showFiles = function () {
  39. return $scope.files;
  40. };
  41. // Check for the various File API support.
  42. if (window.File && window.FileReader && window.FileList && window.Blob) {
  43. // Great success! All the File APIs are supported.
  44. $(window).bind("dragover", function (e) {
  45. e.stopPropagation();
  46. e.preventDefault();
  47. e.originalEvent.dataTransfer.dropEffect = 'copy';
  48. });
  49. $(window).bind("drop", function(e) {
  50. e.stopPropagation();
  51. e.preventDefault();
  52. var files = e.originalEvent.dataTransfer.files;
  53. for (var i = 0, f; f = files[i]; i++) {
  54. var reader = new FileReader();
  55. // Closure to capture the file information.
  56. reader.onload = (function(theFile) {
  57. return function(e) {
  58. var bytes = new Uint8Array(e.target.result);
  59. $scope.files.push({
  60. "name": theFile.name,
  61. "bytes": bytes
  62. });
  63. /*var c = 0;
  64. for (var i = 0; i < bytes.length; i++) {
  65. if (c++ >= 16) {
  66. $("#tabFile1").append("<br>");
  67. c = 0;
  68. }
  69. $("#tabFile1").append(bytes[i].toString(16) + " ");
  70. }*/
  71. };
  72. })(f);
  73. // Read in the image file as a data URL.
  74. reader.readAsArrayBuffer(f);
  75. }
  76. });
  77. } else {
  78. $scope.fileApi = false;
  79. alert('The File APIs are not fully supported in this browser.');
  80. }
  81. }