String to Octal

Discussion in 'C Programming' started by fuch6921@kettering.edu, Feb 21, 2006.

  1. Guest

    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.
     
    , Feb 21, 2006
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    >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.
    --
    If you lie to the compiler, it will get its revenge. -- Henry Spencer
     
    Walter Roberson, Feb 21, 2006
    #2
    1. Advertising

  3. Ben Pfaff Guest

    writes:

    > 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.
    --
    "I've been on the wagon now for more than a decade. Not a single goto
    in all that time. I just don't need them any more. I don't even use
    break or continue now, except on social occasions of course. And I
    don't get carried away." --Richard Heathfield
     
    Ben Pfaff, Feb 21, 2006
    #3
  4. Guest

    Thanks!
     
    , Feb 21, 2006
    #4
  5. wrote:
    > 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)
     
    Martin Ambuhl, Feb 21, 2006
    #5
  6. suresh Guest

    wrote:

    > 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.
     
    suresh, Feb 21, 2006
    #6
  7. Default User Guest

    wrote:

    > Thanks!


    For what? See below.



    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Feb 21, 2006
    #7
  8. On 2006-02-21, Default User <> wrote:
    > wrote:
    >
    >> Thanks!

    >
    > 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.

    --
    Remove evomer to reply
     
    Richard G. Riley, Feb 21, 2006
    #8
  9. 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")

    --
    C is a sharp tool
     
    Emmanuel Delahaye, Feb 21, 2006
    #9
  10. Micah Cowan Guest

    "Richard G. Riley" <> writes:

    > On 2006-02-21, Default User <> wrote:
    > > wrote:
    > >
    > >> Thanks!

    > >
    > > 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.


    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
     
    Micah Cowan, Feb 21, 2006
    #10
  11. Jordan Abel Guest

    On 2006-02-21, Micah Cowan <> wrote:
    > "Richard G. Riley" <> writes:
    >
    >> On 2006-02-21, Default User <> wrote:
    >> > wrote:
    >> >
    >> >> Thanks!
    >> >
    >> > 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.

    >
    > 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.
     
    Jordan Abel, Feb 21, 2006
    #11
  12. Micah Cowan Guest

    "suresh" <> writes:

    > wrote:
    >
    > > 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.


    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
     
    Micah Cowan, Feb 21, 2006
    #12
  13. Default User Guest

    Richard G. Riley wrote:


    > Is your threading in XanaNews broken?


    I've had enough of your obstructist crap.


    *plonk*


    Brian
     
    Default User, Feb 21, 2006
    #13
  14. Default User Guest

    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
     
    Default User, Feb 21, 2006
    #14
  15. On 2006-02-21, Micah Cowan <> wrote:
    > "Richard G. Riley" <> writes:
    >
    >> On 2006-02-21, Default User <> wrote:
    >> > wrote:
    >> >
    >> >> Thanks!
    >> >
    >> > 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.

    >
    > 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
     
    Richard G. Riley, Feb 22, 2006
    #15
  16. On 2006-02-21, Default User <> wrote:
    > Richard G. Riley wrote:
    >
    >
    >> Is your threading in XanaNews broken?

    >
    > 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.


    --
    Remove evomer to reply
     
    Richard G. Riley, Feb 22, 2006
    #16
  17. On 21 Feb 2006 18:43:00 GMT, in comp.lang.c , "Richard G. Riley"
    <> wrote:

    >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
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Feb 22, 2006
    #17
  18. On 22 Feb 2006 08:39:44 GMT, in comp.lang.c , "Richard G. Riley"
    <> wrote:

    >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
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Feb 22, 2006
    #18
  19. CBFalconer Guest

    Mark McIntyre wrote:
    > "Richard G. Riley" <> wrote:
    >
    >> Is your threading in XanaNews broken?

    >

    .... 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/>
     
    CBFalconer, Feb 22, 2006
    #19
  20. On 2006-02-22, CBFalconer <> wrote:
    > Mark McIntyre wrote:
    >> "Richard G. Riley" <> wrote:
    >>
    >>> Is your threading in XanaNews broken?

    >>

    > ... 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.
    >


    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.


    --
    Remove evomer to reply
     
    Richard G. Riley, Feb 22, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kermit Lowry
    Replies:
    0
    Views:
    403
    Kermit Lowry
    Sep 25, 2003
  2. Hostos
    Replies:
    7
    Views:
    5,337
    La'ie Techie
    Oct 15, 2003
  3. Chris Online

    Reading out data from octal buffer

    Chris Online, Mar 2, 2004, in forum: C++
    Replies:
    1
    Views:
    399
    Peter Jansson
    Mar 2, 2004
  4. KB
    Replies:
    5
    Views:
    791
    Steven D'Aprano
    Jul 31, 2005
  5. Bill H

    Convert potion of string to Octal

    Bill H, Jan 3, 2006, in forum: Perl Misc
    Replies:
    10
    Views:
    255
    Bill H
    Jan 4, 2006
Loading...

Share This Page