-1

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!

  • 4
    It is usually helpful to provide a working test case. Just copying your description doesn't duplicate the problem: http://jsfiddle.net/aybba/ – Quentin Jan 14 '12 at 17:55
  • Without seeing the code we don't know why it's being converted. To decimal, btw. – Dave Newton Jan 14 '12 at 17:55
  • 0x000[NUMBER] is a C-language notation for an integer (hexadecimal). When you use parseInt it will be translated to an integer. – Codebeat Jan 14 '12 at 18:01

2 Answers2

1

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:

x.toString(16);

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?.

Community
  • 1
  • 1
ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • I'm still don't get why it converts it even though I'm passing it as a string. Using x.toString(16) to convert it back to Hex, I should be able to provide the padding. – user1149389 Jan 14 '12 at 18:24
  • @user1149389 - There's not enough code posted yet to determine the exact path of this variable / value. We would need to see where the value is being declared as 0x90D to help further. – ziesemer Jan 14 '12 at 18:30
  • It's being read in from a database (varchar(50) is the field definition), in to a List of objects in C#. Those results are then filtered using Linq, and I'm dynamically creating a table using jQuery using the results from the Linq query. The first column of the table is a hyperlink that I using to call the ShowDetails function. It displays as the 0x000000000000090D everywhere EXCEPT in the called function. – user1149389 Jan 14 '12 at 18:42
  • @user1149389 - Great. Now please show us more code if you still need a better answer to the "get why". How is this value being conveyed from the database into something accessible by JavaScript? Is it being written directly into the HTML somewhere? AJAX? As JSON or as XML? – ziesemer Jan 14 '12 at 18:45
1

If the string is in fact a string, the formatting should not change. You must be converting it to a number somewhere.

see this fiddle.

http://jsfiddle.net/8SYNJ/

var x = '0x000000000000090D';
var div = document.querySelector('div');
function show(str) {
    div.innerHTML = div.innerHTML + '<br/>' + typeof str + ' '  + str;   
}

show(x); // string
show(+x); // convert str to num aka decimal
Trevor
  • 11,269
  • 2
  • 33
  • 40