0

I am trying to store some data in hexadecimal format to a variable in JavaScript and read the data in same format. When I tried assigning a variable to hexadecimal value and display it, I got as decimal value equivalent to hexadecimal.

var a = 0x0F;
WScript.Echo("a = "+a);

I got as a = 15 instead of a = 0x0F. Why is this happening? Is it possible to get the value in hexadecimal format itself?

YakovL
  • 7,557
  • 12
  • 62
  • 102
  • Possible duplicate of [How to convert decimal to hex in JavaScript?](https://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript) – InQβ Aug 16 '17 at 09:50
  • `0x0F`, `0o17` and `0b1111` are merely different ways of writing 15. You could do `(15).toString(16)`. – Salman A Aug 16 '17 at 09:55
  • Hexadecimal is a notation, not a format. If you want to output a number in a base other than `10`, use `.toString` with the desired radix/base. If you want to store bytes, _Number_ probably isn't the best solution for you, look at [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) – Paul S. Aug 16 '17 at 10:28

1 Answers1

0

hexadecimal type does not exist in javascript.

There's only a few types (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures):

  • number
  • string
  • Object
  • ...

What you are doing is a number assignment with an hexadecimal value.

But, a is a number so, it's normal representation is decimal form.

To have the hexadecimal value (as string) of your decimal number, you can take a look to this answer: https://stackoverflow.com/a/57805/5119765

ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35