I have a string, ‘0x000000000000090D’ that I am passing to a function. However, when I try to display it in an alert in the function, it has been converted it to 2317. Displaying it in an alert in the calling function displays it correctly.
var tagid = msg[post].TagID.toString();
alert(tagid);
ShowDetails(tagid);
function ShowDetails(tagid){
alert(tagid);
}
Why is it being converted, and what is it being converted in to so I can try to stop it?
Thanks!
Please post your exact code. Somewhere, it is being treated as a number instead of a string, and you’re seeing the decimal representation (2317) of a hexadecimal number (0x90D).
If you’re actually expecting this to be treated as a string, and if it’s in your source code, ensure that the value is surrounded by quotes.
Otherwise, if this is a variable (assume
x), you can somewhat get back to the hexadecimal format you showed:However, this won’t provide all the additional 0-padding, resulting in (only)
90d. Some additional solutions for this, including with padding, are available at How to convert decimal to hex in JavaScript?.