Question

I need a few lines of Javascript code that will take a hexadecimal number (in the form of a 16 character string) and convert it to two variables representing the high and low 32 bits of the 64-bit original value.

I am trying to use the iTunes COM function "ItemByPersistenID" to play a song in iTunes with Windows Script. But I only have the hexadecimal value of the PersistentId and the function only takes the high and low 32-bits.

The function definition (from the iTunes COM SDK documentation)

function TrackCollection:ItemByPersistentId(long highID, long lowID)

where the "highID" parameter is "The high 32 bits of the 64-bit persistent ID" and "lowID" is "The low 32 bits of the 64-bit persistent ID".

Was it helpful?

Solution

Nicholas has a good answer. In the hypothetical case that the hex string doesn't have leading zeroes you could do:

var a=hexstr.match(/(.*?)(.{0,8})$/);
var lo=parseInt(a[2],16);
var hi=parseInt(a[1],16);

OTHER TIPS

Try

var loNibble = parseInt( hexValue.substring(8,16) , 16 ) ;
var hiNibble = parseInt( hexValue.substring(0,8)  , 16 ) ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top