Join 3,512 readers in helping fund MetaFilter (Hide)


What kind of magic does QString().setNum() do?
February 16, 2012 7:32 PM   Subscribe

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.
posted by crawl to Computers & Internet (6 answers total)
 
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.
posted by Chocolate Pickle at 7:37 PM on February 16, 2012 [1 favorite]


From the documentation:

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.
posted by Blazecock Pileon at 7:50 PM on February 16, 2012


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 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.
posted by zsazsa at 7:57 PM on February 16, 2012 [3 favorites]


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.
posted by crawl at 8:09 PM on February 16, 2012


The shift hack will work, but mathematically 232 - 1413011644 = 2881955652 if you want something that's easier for people too understand if they ever look through your code.
posted by delmoi at 9:43 PM on February 16, 2012


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.
posted by hattifattener at 1:29 AM on February 17, 2012


« Older I sit at my desk all day and f...   |  I'm looking for lists of TV sh... Newer »
This thread is closed to new comments.