adamkdean

software engineering

Readable File Sizes

By Adam K Dean on

In an effort to fill up this blog with code, I will over the next few weeks be looking back over my snippets, finding useful methods and posting them. Today's feature is a simple text formatter, which converts a long (representing bytes) into a string, giving you a nice readable file size.

So without further ado:

private static string GetReadableBytes(long lSize)
{
    double size = lSize;
    if (size >= 1024 && size < 1048576)
        return string.Format("{0:#,0.00} KB", (size / 1024));

    else if (size >= 1048576 && size < 1073741824)
        return string.Format("{0:#,0.00} MB", (size / 1048576));

    else if (size >= 1073741824 && size < 1099511627776)
        return string.Format("{0:#,0.00} GB", (size / 1073741824));

    else if (size >= 1099511627776)
        return string.Format("{0:#,0.00} TB", (size / 1099511627776));

    else return string.Format("{0} B", size);
}

And here you can see the output.

/* Bytes                    Readable Size
 * =====                    =============
 * 436                  ->  436 B
 * 2159                 ->  2.11 KB
 * 36236                ->  35.39 KB
 * 2362893              ->  2.25 MB
 * 276227272            ->  263.43 MB
 * 62367298764          ->  58.08 GB
 * 3623647473473        ->  3.30 TB
 * 352635208736296      ->  320.72 TB
 * 236363536873629626   ->  214,971.38 TB
 **/

Enjoy!

Syntax highlighting test

By Adam K Dean on

Just testing the syntax highlighter with this blog.

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.ReadKey();
        }
    }
}

Get length of associative array in JavaScript

By Adam K Dean on

Something I learnt just now is that JavaScript is funny about arrays. Unless you're using numerical indexes, your array elements will actually become properties of the underlying object. It sucks, but it also kind of makes sense.

What this means is that if you have a multi-dimensional array (which would be like array[x][y]):

[
    a: [a, b, c],
    b: [a, b, c],
    c: [a, b, c]
]

Which could be built like so:

var chars = ['a', 'b', 'c'],
    arr = [],
    x, y;

for(x = 0; x < 3; x++) {
    arr[chars[x]] = [];
    for(y = 0; y < 3; y++) {
        arr[chars[x]][chars[y]] = true;
    }
}

Then if you wanted to get the length of the first dimension of this array, arr.length isn't going to work. You'll get 0. If you extend the Object prototype, you can add in a simple key counting method like so:

Object.prototype.getLength = function() {
    var len = 0;
    for (var key in this) {
        if (this.hasOwnProperty(key)) {
            len++;
        }
    }  
    return len;
}

This works perfectly, as you can see:

> console.log(arr.length, arr.getLength());

0, 3

Another, probably safer, way of doing this would be to put it into a function rather than extending the Object prototype:

var getAssocArrayCount = function(array) {
    var len = 0;
    for (var key in array) {
        if (array.hasOwnProperty(key)) {
            len++;
        }
    }  
    return len;
}

And then simply call it like so:

> console.log(arr.length, getAssocArrayCount(arr));

0, 3