Convert Dec to Hex (weird)

Results 1 to 9 of 9

Thread: Convert Dec to Hex (weird)

  1. #1
    jsappdev Guest

    Default Convert Dec to Hex (weird)

    I am trying to use jscript to convert error codes to a output string. I need to convert dec (ex. -2147192265) to a hex (80047237). I have tried using x.toString(16);, but it will not convert using the neg (-). Any suggestions?

  2. #2
    Join Date
    Dec 1969

    Default RE: Convert Dec to Hex (weird)

    just add the - later (use Math.abs() to get rid of it before converting, and concatenate it back on afterwards).<BR><BR>j

  3. #3
    jsappdev Guest

    Default RE: Convert Dec to Hex (weird)

    I need that negative sign (-) to be converted with the number. Let me give more information. When I convert -2147192265 with vbscript I get 80047237 but with jscript (using x.toString(16) which is supposed to convert dec to hec) I get -7FFB8DC9. I need the vbscript number. How do I get that?

  4. #4
    Dan Evans Guest

    Default RE: Convert Dec to Hex (weird)

    Not a solution, but I think your problem is that your number is smaller than -2 ^ 31 so it&#039;s looping back to the other end of the range (2 ^ 31 - 1).<BR><BR>I would imagine that the toString function only takes 31 bits and a sign bit but I don&#039;t know for sure.<BR><BR>Is there no way you could make the number smaller?<BR><BR>-Dan Evans

  5. #5
    jsappdev Guest

    Default RE: Convert Dec to Hex (weird)

    No, I don&#039;t think I can make it smaller. The error is generated by a COM object which was developed and now frozen code.

  6. #6
    Dan Evans Guest

    Default RE: Convert Dec to Hex (weird)

    In that case, this may be one of those occurences when you need both VBScript and JavaScript in your code.<BR><BR>-Dan Evans

  7. #7
    jsappdev Guest

    Default RE: Convert Dec to Hex (weird)

    For your information, I was reading an article in the MSDN library where a developer tried to do this exact thing but could not. There is no way to convert keeping the negative. It is a bug in jscript toString(16)function.

  8. #8
    Dan Evans Guest

    Default Have a little faith :o)

    The reason it chucks out such weird results is because the negative hex string is always 8 characters long. The 1st character is the negative value, the rest being positive.<BR><BR>The easiest way to go from -ve decimal to hex is to work out what the equivalent +ve decimal would be (If you assumed the 1st character of the hex was +ve), then just perform .toString(16) on that. This is far easier than it sounds - Just minus the number from 2^32 (The largest you can go).<BR><BR>If that made no sense at all, I&#039;ve posted the code below. It should work as far as I can see and it works on all the test cases I&#039;ve done but don&#039;t blame me if your computer explodes when you use it:<BR><BR>var n = -2147192265;<BR>if (n.toString().charAt(0) == "-") n = Math.pow(2, 32) - Math.abs(n);<BR>alert(n.toString(16).toUpperCase() ); //Displays "80047237"<BR><BR>Hope this helps,<BR><BR>-Dan Evans

  9. #9
    Dan Evans Guest

    Default Or even better

    Even more efficient:<BR><BR>alert((n &#060; 0? 4294967296 + n: n).toString(16).toUpperCase()); <BR><BR>-Dan Evans<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts