On 10/31/2014 4:54 PM, Melzzzzz wrote:
On Fri, 31 Oct 2014 22:43:23 +0100
Marcel Mueller <news.5.maazl@spamgourmet.org> wrote:
I recently had a problem with a GPU assembler. The program is mainly
C++11, but the parsing of expression components is done with sscanf.
This causes problems because sscanf cannot read the integer constant
"0x80000000". %i returns 0x7fffffff and %u cannot read hex numbers. I
could bet that this has been working with %i for many years.
I just tested with gcc 4.8.2 for OS/2 - works with %i.
But with gcc 4.8.2 Linux (Mint 17) the result is the bitwise not.
#include <stdio.h>
int main()
{ int i;
sscanf("0x80000000", "%i", &i);
printf("%x", i);
return 0;
}
The goal is to read an unsigned number in all common formats
(decimal, binary, octal, hex) from a string. The string does not
necessarily end after the number. I did not find format specifier
that does the job.
Marcel
#include <stdio.h>
int main()
{ long long i;
sscanf("0x80000000","%lli",&i);
printf("%llx\n", i);
return 0;
}
Problem is that 32 bit int is not long enough, just use long long
and all set...
or use C++ and drop the C style
#include <iostream>
int main()
{
long long i = 0x80000000;
std::cout << std::hex << i << std::endl;
return 0;
}
or if you will need the string later
#include <iostream>
#include <sstream>
int main()
{
long long i = 0x80000000;
std::ostringstream os;
os << std::hex << i;
std::cout << os.str() << std::endl;
return 0;
}
or at least #include <cstdlib> which has been for quite a long time now...