Guest User

Untitled

a guest
Apr 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. using System;
  2.  
  3. class ByteUtils {
  4. public ByteUtils()
  5. {
  6.  
  7. }
  8.  
  9. public byte[] convertHistogramToBytes(long[] histogram)
  10. {
  11. long currentValue;
  12. byte[] allBytes;
  13. byte[] currentValBytes;
  14. int longSize;
  15. int arrayLength;
  16. int totalBytes;
  17. int bytesWrittenThusFar;
  18.  
  19. /* find the length of the histogram array; this *
  20. * could be hardcoded to 256 I believe, but by *
  21. * finding the length of the array at runtime *
  22. * the code will be more adaptable to changes */
  23. arrayLength = histogram.Length;
  24.  
  25. /* find the size of a long type integer */
  26. longSize = sizeof(long);
  27.  
  28. /* find total bytes of the histogram */
  29. totalBytes = arrayLength * longSize;
  30.  
  31. /* now create an array of bytes large enough to *
  32. * hold all of the bytes from the histogram */
  33. allBytes = new byte[totalBytes];
  34.  
  35. /* variable to track how many bytes out the *
  36. * total bytes have been written while converting */
  37. bytesWrittenThusFar = 0;
  38.  
  39. /* Loop through the entire array */
  40. for (int i = 0; i < 256; i++)
  41. {
  42. /* grab a value from the array at the current *
  43. * position i */
  44. currentValue = histogram[i];
  45.  
  46. /* Convert the current value to an array of bytes */
  47. currentValBytes = BitConverter.GetBytes(currentValue);
  48.  
  49. /* Check the endianness of the bytes, and put the array *
  50. * into the right order for the endianness of the *
  51. * machine the code is being run on */
  52. if (BitConverter.IsLittleEndian) {
  53. Array.Reverse(currentValBytes);
  54. }
  55.  
  56. /* copy the bytes from the currentValue to the array of *
  57. total bytes */
  58. Buffer.BlockCopy(
  59. currentValBytes,
  60. 0,
  61. allBytes,
  62. (bytesWrittenThusFar - 1),
  63. currentValBytes.Length
  64. );
  65.  
  66. /* add the total bytes written at this point so that *
  67. * it can be used to offset where the next set of *
  68. * bytes is written to in the array of total bytes */
  69. bytesWrittenThusFar += currentValBytes.Length;
  70. }
  71.  
  72. return allBytes;
  73. }
  74.  
  75. public int getLongSize()
  76. {
  77. int sizeOfLong;
  78.  
  79. sizeOfLong = sizeof(long);
  80. Console.WriteLine(sizeOfLong.ToString());
  81.  
  82. return sizeOfLong;
  83. }
  84. }
  85.  
  86. class TestDriver
  87. {
  88. static void Main(string[] args)
  89. {
  90. ByteUtils bu = new ByteUtils();
  91.  
  92. bu.getLongSize();
  93.  
  94. return;
  95. }
  96. }
Add Comment
Please, Sign In to add comment