I am writing a little base conversion utility called base.c.
This is what base does.
$ base -127
Signed decimal: -127
Unsigned decimal: 4294967169
Hexidecimal: 0xffffff81
Octal: O37777777601
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
1111 1111 1111 1111 1111 1111 1000 0001
$ base 127
Signed decimal: 127
Unsigned decimal: 127
Hexidecimal: 0x7f
Octal: O177
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0000 0000 0000 0000 0000 0000 0111 1111
However, base has a bug when one inputs sign extended hexadecimal or octal
numbers, like this.
$ base 0xffffff81
Signed decimal: 2147483647
Unsigned decimal: 2147483647
Hexidecimal: 0x7fffffff
Octal: O17777777777
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0111 1111 1111 1111 1111 1111 1111 1111
Base works like this. I use sscanf %i to convert argv[1] to an int. Then
I cast the int to an unsigned int and do the rest of the manipulation.
However, as the above example shows, it doesn't work properly when I input
a sign extended octal or hex number.
The decimal of 0xffffff81 should be 4294967169, but it isn't. Why ?
Thanks 14 14259
me2 wrote:
I am writing a little base conversion utility called base.c.
This is what base does.
$ base -127
Signed decimal: -127
Unsigned decimal: 4294967169
Hexidecimal: 0xffffff81
Octal: O37777777601
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
1111 1111 1111 1111 1111 1111 1000 0001
$ base 127
Signed decimal: 127
Unsigned decimal: 127
Hexidecimal: 0x7f
Octal: O177
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0000 0000 0000 0000 0000 0000 0111 1111
However, base has a bug when one inputs sign extended hexadecimal or octal
numbers, like this.
$ base 0xffffff81
Signed decimal: 2147483647
Unsigned decimal: 2147483647
Hexidecimal: 0x7fffffff
Octal: O17777777777
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0111 1111 1111 1111 1111 1111 1111 1111
Base works like this. I use sscanf %i to convert argv[1] to an int. Then
I cast the int to an unsigned int and do the rest of the manipulation.
However, as the above example shows, it doesn't work properly when I input
a sign extended octal or hex number.
The decimal of 0xffffff81 should be 4294967169, but it isn't. Why ?
Because %i is only for integers. You will need to check the string in
argv[1] before scanning it using sscanf. If it is preceded with "0x"
then use %x (hexadecimal), if its preceded with just "0" then use %o
(octal) for scanning with sprintf, and later you can convert it to
other formats.
Scorpio wrote:
me2 wrote:
I am writing a little base conversion utility called base.c.
This is what base does.
$ base -127
Signed decimal: -127
Unsigned decimal: 4294967169
Hexidecimal: 0xffffff81
Octal: O37777777601
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
1111 1111 1111 1111 1111 1111 1000 0001
$ base 127
Signed decimal: 127
Unsigned decimal: 127
Hexidecimal: 0x7f
Octal: O177
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0000 0000 0000 0000 0000 0000 0111 1111
However, base has a bug when one inputs sign extended hexadecimal or octal
numbers, like this.
$ base 0xffffff81
Signed decimal: 2147483647
Unsigned decimal: 2147483647
Hexidecimal: 0x7fffffff
Octal: O17777777777
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0111 1111 1111 1111 1111 1111 1111 1111
Base works like this. I use sscanf %i to convert argv[1] to an int. Then
I cast the int to an unsigned int and do the rest of the manipulation.
However, as the above example shows, it doesn't work properly when I input
a sign extended octal or hex number.
The decimal of 0xffffff81 should be 4294967169, but it isn't. Why ?
Because %i is only for integers. You will need to check the string in
argv[1] before scanning it using sscanf. If it is preceded with "0x"
then use %x (hexadecimal), if its preceded with just "0" then use %o
(octal) for scanning with sprintf, and later you can convert it to
other formats.
Correction...I meant scanning with sscanf, not sprintf. Sorry.
Scorpio wrote:
Scorpio wrote:
me2 wrote:
I am writing a little base conversion utility called base.c.
>
This is what base does.
>
$ base -127
Signed decimal: -127
Unsigned decimal: 4294967169
Hexidecimal: 0xffffff81
Octal: O37777777601
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
1111 1111 1111 1111 1111 1111 1000 0001
$ base 127
Signed decimal: 127
Unsigned decimal: 127
Hexidecimal: 0x7f
Octal: O177
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0000 0000 0000 0000 0000 0000 0111 1111
>
However, base has a bug when one inputs sign extended hexadecimal or octal
numbers, like this.
>
$ base 0xffffff81
Signed decimal: 2147483647
Unsigned decimal: 2147483647
Hexidecimal: 0x7fffffff
Octal: O17777777777
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0111 1111 1111 1111 1111 1111 1111 1111
>
Base works like this. I use sscanf %i to convert argv[1] to an int. Then
I cast the int to an unsigned int and do the rest of the manipulation.
However, as the above example shows, it doesn't work properly when I input
a sign extended octal or hex number.
>
The decimal of 0xffffff81 should be 4294967169, but it isn't. Why ?
Because %i is only for integers. You will need to check the string in
argv[1] before scanning it using sscanf. If it is preceded with "0x"
then use %x (hexadecimal), if its preceded with just "0" then use %o
(octal) for scanning with sprintf, and later you can convert it to
other formats.
Correction...I meant scanning with sscanf, not sprintf. Sorry.
More correction. I just realized that %x and %o are only for printing,
and its cannot be used for scanning. So you'll have to find some other
method to read hexadecimal and octal values.
On Thu, 28 Dec 2006 07:04:32 -0800, Scorpio wrote:
Because %i is only for integers. You will need to check the string in
argv[1] before scanning it using sscanf. If it is preceded with "0x"
then use %x (hexadecimal), if its preceded with just "0" then use %o
(octal) for scanning with sprintf, and later you can convert it to
other formats.
But man sscanf says this:
The following conversion specifiers are available:
....
i Matches an optionally signed integer; the next pointer must be a
pointer to int. The integer is read in base 16 if it
begins with 0x or 0X, in base 8 if it begins with 0, and
in base 10 otherwise. Only characters that correspond to
the base are used.
So why isn't it properly reading the hex and octal numbers ?
Thanks.
me2 wrote:
>
.... snip ...
>
However, base has a bug when one inputs sign extended hexadecimal
or octal numbers, like this.
$ base 0xffffff81
Signed decimal: 2147483647
Unsigned decimal: 2147483647
Hexidecimal: 0x7fffffff
Octal: O17777777777
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0111 1111 1111 1111 1111 1111 1111 1111
Base works like this. I use sscanf %i to convert argv[1] to an
int. Then I cast the int to an unsigned int and do the rest of
the manipulation. However, as the above example shows, it doesn't
work properly when I input a sign extended octal or hex number.
The decimal of 0xffffff81 should be 4294967169, but it isn't.
Why ?
You have experienced integer overflow, after which behaviour is
undefined. Therefore the answers you are getting (or anything
else) are perfectly valid.
--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
me2 <no****@nowhere .comwrites:
I am writing a little base conversion utility called base.c.
This is what base does.
[cut]
However, base has a bug when one inputs sign extended hexadecimal or octal
numbers, like this.
[cut]
I use sscanf %i to convert argv[1] to an int.
You'd better use strtol() for that which can automatically read
decimal, octal or hexadecimal numbers and provides better error
checking I guess. You could even use strtoll() for bigger numbers if
it's available on your system (I believe it's defined in C99).
#v+
#include <stdio.h>
#include <stdlib.o>
#include <errno.h>
int main(int argc, char **argv) {
char *end;
long long num;
if (argc!=2) {
fputs("usage: base <number>\n", stderr);
return EXIT_FAILURE;
}
num = strtoll(argv[1], &end, 0);
if (*end || errno) {
fprintf(stder, "%s: invalid number\n", argv[1]);
return EXIT_FAILURE;
}
printf("signed : %lld\n", num);
printf("unsigne d: %llu\n", num);
printf("hex : %llo\n", num);
printf("octal : %llx\n", num);
return EXIT_SUCESS;
}
#v-
(code not tested)
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Michal Nazarewicz writes:
You'd better use strtol() for that which can automatically read
decimal, octal or hexadecimal numbers and provides better error
checking I guess. You could even use strtoll() for bigger numbers if
it's available on your system (I believe it's defined in C99).
Or lacking long long, handle the sign specially and read the rest into
an unsigned int with strtoul or %u. Then maybe convert to int - taking
care to avoid overflow, including for INT_MIN.
--
Hallvard
On Thu, 28 Dec 2006 11:14:06 -0500, CBFalconer wrote:
You have experienced integer overflow, after which behaviour is
undefined. Therefore the answers you are getting (or anything
else) are perfectly valid.
0xffffff81 is a 32 bit number. How can it be integer overflow ? Are you
saying it will interpret it as an unsigned integer and then try to stuff
it in a signed int variable and overflow that way ?
On Fri, 29 Dec 2006 11:16:00 +0100, Michal Nazarewicz wrote:
Changing it to use strtol and a long prevented the overflow. It works
properly now. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: MiniDisc_2k2 |
last post by:
Okay, here's a question about the standard. What does it say about
unsigned/signed mismatches in a comparison statement:
char a = 3;
unsigned char b = 255;
if (a<b)
Now what's the real answer here? If a is converted to unsigned, then b>a.
But, if b is converted to signed,then a>b. What's the correct coversion
(what is the compiler supposed to do?)
|
by: Ken Tough |
last post by:
Seems like a simple thing to find out, but I'm struggling. I have
googled, but everything I find is about implicit conversion,
not explicit.
Is this implementation-specific, or does ANSI/ISO lay out what
should happen for:
--------------------------
signed char sc;
unsigned char uc;
|
by: HH |
last post by:
int main()
{
uint x = -1;
int y = -1;
if (x == y)
printf("EQUAL");
else
printf("NOT EQUAL");
}
This code prints "EQUAL". Does this mean that bit comparison is done for the
|
by: Sam |
last post by:
Hi,
I am using some functions return a base64 encoded string.
The functions write it into an unsigned char buffer. I am a little
confused as to why a base64 encoded string would be using
an unsigned char buffer instead of a signed char buffer?
Also if I want to write this to file. Casting the buffer to
"char *" & then using fprintf seems to work, but this doesn't
seem right to me.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |