What is your favorite kind of magic?

What kind of magic does QString().setNum() do?

  • What kind of magic does QString().setNum() do to convert -1413011644 to 2881955652? I'm porting some code written in QT4 to javascript. The very last part of the process calls QString().setNum() on a number and outputs it. I have all the code working that generates that number, but can't output it without knowing what QString().setNum() does.

  • Answer:

    Those are the same numerical representation, 0xABC72744 -1413011644 is the value if that's treated as a signed number. 2881955652 is how it's read if it's unsigned.

crawl at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

I agree with Blazecock Pileon that this smells like a bug. But, if you really want to convert signed to unsigned, this bit of JS trickery will do it: Input: -1413011644 >> 0 >>> 0 Output: 2881955652 JS's Number representation is strange to say the least. It's internally represented as a floating-point number, except when https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators when it's represented as a signed integer. Except in the case of >>>, when it's an unsigned integer. So you can abuse the bitwise operators to convert your Number to a signed integer, then an unsigned integer.

zsazsa

From the http://doc.trolltech.com/4.2/qstring.html#setNum: The base is 10 by default and must be between 2 and 36. For bases other than 10, n is treated as an unsigned integer. Since you're working in base 10, this implies you can give a signed value. Perhaps you've discovered a bug, if your signed value is being printed as its unsigned equivalent. You might want to contact the devs for support, in that case.

Blazecock Pileon

I neglected to mention that 'id' is defined as quint32, so it is indeed unsigned. It is less than helpful that when I print it to stdout it comes out as signed, which led to the confusion. Applying zsazsa's trickery to my code solves the problem. Thank you.

crawl

The shift hack will work, but mathematically http://www.google.com/search?q=2%5E32+-1413011644 if you want something that's easier for people too understand if they ever look through your code.

delmoi

when I print it to stdout it comes out as signed How are you printing it to stdout? It's possible that that's where the reinterpretation from uint32 to sint32 is happening.

hattifattener

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.