LinuxQuestions.org Have problem converting a decimal number to octal
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

 05-20-2004, 02:36 PM #1 Linh Member   Registered: Apr 2003 Posts: 178 Rep: Have problem converting a decimal number to octal Have problem converting a decimal number to octal the decimal number is 408. When converting from 408(decimal) to octal, the value should be 630 (octal) but instead it gives a value of 40 or 32. ==================================== root:/home# ./convert_a_decimal_to_octal decimal_char = 408 decimal_number = 408 octal_number = 40 octal_number = 32 ==================================== Code: ```#include #include #include /********************************************/ start_processes() { char decimal_char[4] = "408"; int decimal_number = 0; int octal_number = 0; printf ("decimal_char = %s\n", decimal_char); /* convert a numeric character to decimal */ sscanf(decimal_char, "%d", &decimal_number); printf ("decimal_number = %d\n", decimal_number); /* convert a decimal number to octal */ sscanf(decimal_char, "%o", &octal_number); printf ("octal_number = %o\n", octal_number); printf ("octal_number = %d\n", octal_number); } /********************************************/ main() { start_processes(); }```
 05-20-2004, 03:09 PM #2 Hko Senior Member   Registered: Aug 2002 Location: Groningen, The Netherlands Distribution: Debian Posts: 2,536 Rep: "408" is not a valid octal string, because octal uses only 8 digits: '0' to '7'. So the digit '8' in "408" is not valid for octal conversion so sscanf() stop the conversion after reading "40". Also you have some other mistakes in the code, though it would work probably. To see warnings in addition to erros, compile with Code: `gcc -Wall -pedanic -o convert_a_decimal_to_octal convert_a_decimal_to_octal.c` Fixed code below, changes are in red. Code: ```#include #include #include /********************************************/ void start_processes(void) { char decimal_char[4] = "407"; int decimal_number = 0; unsigned int octal_number = 0; printf ("decimal_char = %s\n", decimal_char); /* convert a numeric character to decimal */ sscanf(decimal_char, "%d", &decimal_number); printf ("decimal_number = %d\n", decimal_number); /* convert a decimal number to octal */ sscanf(decimal_char, "%o", &octal_number); printf ("octal_number = %o\n", octal_number); printf ("octal_number = %d\n", octal_number); } /********************************************/ int main() { start_processes(); return 0; }```
 05-20-2004, 03:10 PM #3 Mara Moderator   Registered: Feb 2002 Location: Grenoble Distribution: Debian Posts: 9,696 Rep: Decimal or octal number, it's kept the sam way. That's displaying that makes the difference... When you do the sscanf, '408' is taken as an octal number, but it's incorrect (it has '8'), so only two first digits are taken into account. You get '32' as one of the results, because '32' (decimal) or '40' octal is in octal_number. You can use Code: `printf("octal number = %o\n", decimal_number);` without problems.
 05-20-2004, 03:14 PM #4 Mara Moderator   Registered: Feb 2002 Location: Grenoble Distribution: Debian Posts: 9,696 Rep: Hko, the orginal author wants decimal->octal, not octal->decimal conversion
 05-20-2004, 03:21 PM #5 Hko Senior Member   Registered: Aug 2002 Location: Groningen, The Netherlands Distribution: Debian Posts: 2,536 Rep: Oh. That wasn't clear to me then. Maybe Linh should read the other thread again at: http://www.linuxquestions.org/questi...hreadid=183725 Last edited by Hko; 05-20-2004 at 03:27 PM.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post llama_meme Programming 1 07-06-2010 02:00 PM tsilok Linux - Newbie 4 10-25-2005 05:18 PM pjordan Programming 2 11-18-2004 03:03 PM Linh Programming 3 05-20-2004 03:08 PM acid_kewpie Programming 10 08-20-2003 02:29 PM

LinuxQuestions.org

All times are GMT -5. The time now is 02:05 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -