Thread: Converting an octal code to Char

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    Converting an octal code to Char

    Is there anyway that i can read an octal code then covert it to character ??

    like if i key in '\012' it will read as '\n' ???
    if i key in '141' it will read as 'a' ???

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Lots of ways

    If you use the %i mode in say scanf(), then any integers you type in with a leading 0 are octal

    012
    0141
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    41
    I couldnt use scanf as the parameter is being pass in as a string.
    coz when i read the argument it is in string format. what the function does is it read the \012 and store it in argv[1] and then pass it to convertOctal function so that it is able to convert the numbers to a character and then only store it in a variable.



    Code:
    main(int argc,  char *argv[])
    {
    
          convertOctal(argv[1]);
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So use sscanf() then
    Or strtol() with the base set to 8
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ text file
    By statquos in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2008, 01:42 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM