Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Converting string values to hex

Tags:

javascript

I have an array of string-encoded hex values. I need to convert those strings to actual hex values and then be able to compare them (using standard less-than/greater-than/equals). What is the best way to accomplish this?

like image 885
jimmydugs Avatar asked Oct 06 '09 17:10

jimmydugs


People also ask

What is toString (' hex in JavaScript?

The JavaScript function toString(16) will return a negative hexadecimal number which is usually not what you want.

How to convert number to hexadecimal in js?

To convert a number to hexadecimal, call the toString() method on the number, passing it 16 as the base, e.g. num. toString(16) . The toString method takes the base as a parameter and returns the string representation of the number.

How do you use hexadecimal in JavaScript?

Uses of Hexadecimal Numbers in JavaScript JavaScript supports the use of hexadecimal notation in place of any integer, but not decimals. As an example, the number 2514 in hex is 0x9D2, but there is no language-supported way of representing 25.14 as a hex number.

How do I encode a hex?

Hex encoding is performed by converting the 8 bit data to 2 hex characters. The hex characters are then stored as the two byte string representation of the characters. Often, some kind of separator is used to make the encoded data easier for human reading.


1 Answers

Use the JavaScript parseInt method to convert hex string values into their integer equivalent.

For example:

var value = parseInt("FF", 16);
if (value < 256) {
    // do something...
}
like image 155
Jon Benedicto Avatar answered Sep 27 '22 17:09

Jon Benedicto