Introduction

Recently I’ve been thinking a lot about data on the lower-level of the stack, this is the area where data stops being simple strings and starts looking more like random numbers, *e.g. (59:6f:75:20:63:72:61:63:6b:65:64:20:74:68:65:20:63:6f:64:65:21)

I’ve been wondering a lot about this data, because I do a lot of server communication with my applications, knowing encoding should come in handy!

So a couple of ideas brewing up inside, such as my own compression algorithms, encryption algorithms and also plans to learn low level languages.

I thought I would share the code I’ve been playing with.

The following should allow for a javascript developer to do the majority of  encoding required.

The sprinkle

1
2
3
4
5
6
var letter = "A" // Always decimal 65 in ASCII
var byteCode = letter.charCodeAt(0); // Get the byteCode (decimal) = 65
var character = String.fromCharCode(byteCode); // Return ASCII character/symbol
var binaryCode = byteCode.toString(2); // Convert to base 2 (Binary)
var hexCode = character.charCodeAt(0).toString(16); // Convert to base 16 (Hexadecimal)
console.log(character+" "+byteCode+" 0x"+hexCode+" "+binaryCode);

Thanks to Rezoner for comments! On using toString which I understand now!

Demo of this code in use can be viewed here:

http://ajamdonut.com/sprinkles/hexfun.html?forcing=true#adam


All source can be duplicated, sold, expanded upon, published or used at your own risk and you accept that there may be inaccuracies, you agree I am not responsible for any damages implementing this code could cause.

*If you crack the code let me know in comment!

Share →

4 Responses to Playing with Encoding. Decimal, Hex and Binary in Javascript Updated!

  1. Rezoner says:

    Hiho mate.

    You can use of Number.toString also make for converting decimal to binary.

    Examples:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
      var temp;
     
      // decToBin
        temp = 123;
        temp.toString(2);
     
      // hexToDec
        temp = 0xff;
        // or
        temp = parseInt("0xfa32");

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>