Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class ByteUtils {
- public ByteUtils()
- {
- }
- public byte[] convertHistogramToBytes(long[] histogram)
- {
- long currentValue;
- byte[] allBytes;
- byte[] currentValBytes;
- int longSize;
- int arrayLength;
- int totalBytes;
- int bytesWrittenThusFar;
- /* find the length of the histogram array; this *
- * could be hardcoded to 256 I believe, but by *
- * finding the length of the array at runtime *
- * the code will be more adaptable to changes */
- arrayLength = histogram.Length;
- /* find the size of a long type integer */
- longSize = sizeof(long);
- /* find total bytes of the histogram */
- totalBytes = arrayLength * longSize;
- /* now create an array of bytes large enough to *
- * hold all of the bytes from the histogram */
- allBytes = new byte[totalBytes];
- /* variable to track how many bytes out the *
- * total bytes have been written while converting */
- bytesWrittenThusFar = 0;
- /* Loop through the entire array */
- for (int i = 0; i < 256; i++)
- {
- /* grab a value from the array at the current *
- * position i */
- currentValue = histogram[i];
- /* Convert the current value to an array of bytes */
- currentValBytes = BitConverter.GetBytes(currentValue);
- /* Check the endianness of the bytes, and put the array *
- * into the right order for the endianness of the *
- * machine the code is being run on */
- if (BitConverter.IsLittleEndian) {
- Array.Reverse(currentValBytes);
- }
- /* copy the bytes from the currentValue to the array of *
- total bytes */
- Buffer.BlockCopy(
- currentValBytes,
- 0,
- allBytes,
- (bytesWrittenThusFar - 1),
- currentValBytes.Length
- );
- /* add the total bytes written at this point so that *
- * it can be used to offset where the next set of *
- * bytes is written to in the array of total bytes */
- bytesWrittenThusFar += currentValBytes.Length;
- }
- return allBytes;
- }
- public int getLongSize()
- {
- int sizeOfLong;
- sizeOfLong = sizeof(long);
- Console.WriteLine(sizeOfLong.ToString());
- return sizeOfLong;
- }
- }
- class TestDriver
- {
- static void Main(string[] args)
- {
- ByteUtils bu = new ByteUtils();
- bu.getLongSize();
- return;
- }
- }
Add Comment
Please, Sign In to add comment