| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | C and octal |
Having completely forgotten about octal numbers (didn't they die
out with the four-toed sloth??) this one caught me short. I'm
reading a fixed format string that contains 24hr times, and the
times with leading zeroes stuff up:
#include
#include
#include
int main()
{
char *s = "1245 2330 0400";
int l,m,n;
/* c version */
sscanf(s, "%i %i %i", &l,&m,&n);
printf("%i %i %i\n", l,m,n);
/* c++ version */
istrstream test(s, strlen(s));
test >> l >> m >> n;
printf("%i %i %i\n", l,m,n);
return 0;
}
The result of this proglet is
1245 2330 256
1245 2330 256
because C treats numbers with leading zeroes as base 8 numbers.
I can see several ways of getting around this, but they all seem
kludgy. Am I missing something obvious? Is there an elegant way to
get C or C++ to read 0400 as 400dec instead of 256dec?
Cheers
--- PPoint 1.88
* Origin: Silicon Heaven (3:711/934.16)SEEN-BY: 711/809 934 @PATH: 711/934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.