String to Octal

F

fuch6921

I want to read in an Octal number argument and have it stored as an
octal number. For instance the user will type: ./a.out 777 and it
will store the octal number 777. But it atoi does this as an interger,
and sscanf gives me 0.
 
W

Walter Roberson

I want to read in an Octal number argument and have it stored as an
octal number. For instance the user will type: ./a.out 777 and it
will store the octal number 777. But it atoi does this as an interger,
and sscanf gives me 0.

strtoul() specifying a base of 8.
 
B

Ben Pfaff

I want to read in an Octal number argument and have it stored as an
octal number. For instance the user will type: ./a.out 777 and it
will store the octal number 777. But it atoi does this as an interger,
and sscanf gives me 0.

Use strtol(), specifying base 8.
 
M

Martin Ambuhl

I want to read in an Octal number argument and have it stored as an
octal number. For instance the user will type: ./a.out 777 and it
will store the octal number 777. But it atoi does this as an interger,
and sscanf gives me 0.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *src[] = { "309", "511", "777", "1ff", "1411" }, *endp;
size_t i, n = sizeof src / sizeof *src;
unsigned long x;
printf(" Note: when * endp = '\\0', it is printed as '$'\n\n");
for (i = 0; i < n; i++) {
printf(" The input string is \"%s\"\n", src);
x = strtoul(src, &endp, 8);
printf("read as octal, *endp='%c',\n"
" value: %#lo (oct), %lu (dec), %#lx (hex)\n",
*endp ? *endp : '$', x, x, x);
x = strtoul(src, &endp, 10);
printf("read as decimal, *endp='%c',\n"
" value: %#lo (oct), %lu (dec), %#lx (hex)\n",
*endp ? *endp : '$', x, x, x);
x = strtoul(src, &endp, 16);
printf("read as hex, *endp='%c',\n"
" value: %#lo (oct), %lu (dec), %#lx (hex)\n\n",
*endp ? *endp : '$', x, x, x);
}
return 0;
}

Note: when * endp = '\0', it is printed as '$'

The input string is "309"
read as octal, *endp='9',
value: 030 (oct), 24 (dec), 0x18 (hex)
read as decimal, *endp='$',
value: 0465 (oct), 309 (dec), 0x135 (hex)
read as hex, *endp='$',
value: 01411 (oct), 777 (dec), 0x309 (hex)

The input string is "511"
read as octal, *endp='$',
value: 0511 (oct), 329 (dec), 0x149 (hex)
read as decimal, *endp='$',
value: 0777 (oct), 511 (dec), 0x1ff (hex)
read as hex, *endp='$',
value: 02421 (oct), 1297 (dec), 0x511 (hex)

The input string is "777"
read as octal, *endp='$',
value: 0777 (oct), 511 (dec), 0x1ff (hex)
read as decimal, *endp='$',
value: 01411 (oct), 777 (dec), 0x309 (hex)
read as hex, *endp='$',
value: 03567 (oct), 1911 (dec), 0x777 (hex)

The input string is "1ff"
read as octal, *endp='f',
value: 01 (oct), 1 (dec), 0x1 (hex)
read as decimal, *endp='f',
value: 01 (oct), 1 (dec), 0x1 (hex)
read as hex, *endp='$',
value: 0777 (oct), 511 (dec), 0x1ff (hex)

The input string is "1411"
read as octal, *endp='$',
value: 01411 (oct), 777 (dec), 0x309 (hex)
read as decimal, *endp='$',
value: 02603 (oct), 1411 (dec), 0x583 (hex)
read as hex, *endp='$',
value: 012021 (oct), 5137 (dec), 0x1411 (hex)
 
S

suresh

I want to read in an Octal number argument and have it stored as an
octal number.

What do you mean by 'storing it as an octal number' ?

The representation of a number dose not matter for C. Typically it is
stored as binary number. If you have an underlying machine which
can store 8 symbols for each "bit" only then can you store it as octal
number.

I have seen lot of beginners having this confusion with octal and
hexadecimal representations. It is better to be cleared off this
confusion in the beginning it self.
 
R

Richard G. Riley

For what? See below.



Brian

Is your threading in XanaNews broken? Try a threaded news reader : you
wont lose so much sleep over lack of context in single word
replies. It wasn't that interestnig anyway : he was just thanking
someone who actually helped him with a C problem.
 
E

Emmanuel Delahaye

(e-mail address removed) a écrit :
I want to read in an Octal number argument

OK, it's a text representation. Say 123 or 0123 (C-way)
and have it stored as an
octal number.

Nonsense. The storage of numbers is binary.
For instance the user will type: ./a.out 777 and it
will store the octal number 777. But it atoi does this as an interger,
and sscanf gives me 0.

With which formatter ? Try "%o".

Better is strtol() with base 8 ("123") or 0 ("0123")
 
M

Micah Cowan

Richard G. Riley said:
Is your threading in XanaNews broken? Try a threaded news reader : you
wont lose so much sleep over lack of context in single word
replies. It wasn't that interestnig anyway : he was just thanking
someone who actually helped him with a C problem.

I'm sure we all have threaded news readers. That's not the point. It
is rude to post responses without context for the following reasons:

1. Messages are not guaranteed to arrive remotely in a timely or
orderly manner. His response could well show up ahead of the
message to which he's responding for many people.

2. Many servers expire old messages. At some point, the original
message is likely to get expired on some server, and the response
may stick around a bit beyond it. People reading it will have no
idea what it's talking about.

3. Even if events transpire to make it possible, it's annoying to
have to look at one or more other messages just to get enough
context to understand the only one you were trying to read.

-Micah
 
J

Jordan Abel

I'm sure we all have threaded news readers. That's not the point. It
is rude to post responses without context for the following reasons:

1. Messages are not guaranteed to arrive remotely in a timely or
orderly manner. His response could well show up ahead of the
message to which he's responding for many people.

2. Many servers expire old messages. At some point, the original
message is likely to get expired on some server, and the response
may stick around a bit beyond it. People reading it will have no
idea what it's talking about.

3. Even if events transpire to make it possible, it's annoying to
have to look at one or more other messages just to get enough
context to understand the only one you were trying to read.

Before throwing stones, though, it might be useful to examine whether
the extra context was _really_ needed. A "thank you" is mostly vacuous
anyway, and the person being addressed probably knows what they're being
thanked for. I've also seen people get called out for responding to the
question in the subject line alone without actually addressing the
specifics in the message body.
 
M

Micah Cowan

suresh said:
What do you mean by 'storing it as an octal number' ?

The representation of a number dose not matter for C. Typically it is
stored as binary number.

Indeed, it is required to be stored as a binary number, or at least
that the implementation behave "as if" that were the case.
I have seen lot of beginners having this confusion with octal and
hexadecimal representations. It is better to be cleared off this
confusion in the beginning it self.

Absolutely. I recently had a similar problem where someone was trying
to strip leading zeroes from decimal numbers before printing them back
out. So their first-draft test had something like:

long var = 008;

Which of course caused compile-time errors. :)

For the original poster: it does sound as if you're confusing storage
with representation. The only way to store something as an "octal
number" is to store it in a string representation of octal, which is
hardly efficient.

What you /really/ want to do is read it into a /binary/ format (some
sort of integer type), using scanf() or strtol() or similar (I and
probably many others here would /strongly/ recommend against any of
the *scanf() funcitons--few really know how to use it properly, and
those who do know how typically don't bother, since strtol() and its
cousins are so much more flexible). Once you've done that, you have
everything you need to know about the numnber.

When it comes time to print it out again, you simply use the
/appropriate/ conversion specification in printf() or whatever:
namely, "%o" (possibly modified according to your specific needs). Be
aware that the "o" specifier wants an unsigned int. And, if you want
it to be easily recognizable as an octal number, you probably want to
use the "#" flag (as in "%#o") as well.

In summary, it doesn't matter /what/ kind of number it is: if you read
it in correctly, it is that number, regardless of what kind of
representation you plan to use to print it out. The representation
used to print it back out merely depends on what conversion
specification you're using with *printf().

Hope that helps...

-Micah
 
D

Default User

Micah Cowan wrote:

I'm sure we all have threaded news readers. That's not the point. It
is rude to post responses without context for the following reasons:

[snip 1 - 3]


4. One has the newsreader set to display only new messages. If the
preceeding messages were from a previous "session" then they aren't
immediately visible. It's a pointless annoyance to switch the view to
"view all messages" just to figure out what some Googler is trying to
say.



Brian
 
R

Richard G. Riley

I'm sure we all have threaded news readers. That's not the point. It
is rude to post responses without context for the following reasons:

It is just as rude and silly to post twice the same net nannying twice
in 2 minutes. Personally I'm here to read and comment on C : not a
high percentage of infantile posts telling people to include context
when they post "thanks".

Obviously context is better
 
R

Richard G. Riley

Richard G. Riley wrote:



I've had enough of your obstructist crap.


*plonk*


Brian

It is always amusing to see those who are doing the "obstructing" and
the "telling off" get pissed off when they get some of it back.
 
M

Mark McIntyre

Is your threading in XanaNews broken?

I doubt it. And nor is mine in Agent.
Try a threaded news reader :

Agent is threaded. However neither I nor my newsprovider keep every
single message ever posted, forever, not is it guaranteed that the
messages arrive in the order they were posted.

Thus a message posted this morning, or last week, or last month may
have already expired, not not have arrived yet, or be garbled or
whatever. All the threading in the world won't help you there. Each
post needs to stand on its own, containing enough context to be
meaningful.


As an aside, you started posting in CLC a couple of days ago, and seem
intent on picking fights with all the regulars. I'd really recommend
against that. It will simply cause you no end of pain and get you
killfiled if you're too annoying. You will then lose much of the
benefit you might have gained from posting here
Mark McIntyre
 
M

Mark McIntyre

It is always amusing to see those who are doing the "obstructing" and
the "telling off" get pissed off when they get some of it back.

You're well on the way to being killfiled by the regulars.
Mark McIntyre
 
C

CBFalconer

Mark said:
.... snip ...

As an aside, you started posting in CLC a couple of days ago, and
seem intent on picking fights with all the regulars. I'd really
recommend against that. It will simply cause you no end of pain
and get you killfiled if you're too annoying. You will then lose
much of the benefit you might have gained from posting here

As a statistical datum, plonking him resulted in a 25% reduction of
download volume in my last synchronization of this newsgroup. Tis
a consummation devoutly to be wished.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
R

Richard G. Riley

As a statistical datum, plonking him resulted in a 25% reduction of
download volume in my last synchronization of this newsgroup. Tis
a consummation devoutly to be wished.

Most of the regulars are 4 people I could name : including you. And over such
ridiculous issues as who "we" should help or offer advice to in this
NG. Fortunately there are also a plethora of posters less willing to
offer advice on posting style but more willing to help with c related
issues.

Of course I'm not so naive as to not see that I am banging my head
against a wall with such as you.

Other discussions have been fruitful even if one doesnt always see eye to eye.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top