Saturday, April 8, 2017

binary and hexadecimal numbers in TypeScript

let foo: number = 0x56;
let bar: number = 0b1000101;
let baz: number = 0x2a;
let qux: number = 0b1101;
alert(foo);
alert(bar);
alert(baz);
alert(qux);

 
 

The code above is going to give us four alerts which will progress through these four numbers:

  • 86
  • 69
  • 42
  • 13

 
 

These were confusing when I first saw them. Basically you lead the hexadecimal settings with 0x and the binary settings with 0b. The stuff that comes after the 0x or the 0b is the actual number. Binary numbers need to be made up of ones and zeros only while letters a through f are legit in a hexadecimal number obviously. Also, the hexadecimal stuff clearly works standalone in JavaScript because look at the JavaScript the TypeScript compiles to.

var foo = 0x56;
var bar = 69;
var baz = 0x2a;
var qux = 13;
alert(foo);
alert(bar);
alert(baz);
alert(qux);

No comments:

Post a Comment