LinuxAnswers - the LQ Linux tutorial section.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 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.

Notices

 05-20-2004, 03: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, 04:09 PM #2 Hko Senior Member   Registered: Aug 2002 Location: Groningen, The Netherlands Distribution: ubuntu Posts: 2,530 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, 04:10 PM #3 Mara Moderator   Registered: Feb 2002 Location: Grenoble Distribution: Debian Posts: 9,539 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, 04:14 PM #4 Mara Moderator   Registered: Feb 2002 Location: Grenoble Distribution: Debian Posts: 9,539 Rep: Hko, the orginal author wants decimal->octal, not octal->decimal conversion
 05-20-2004, 04:21 PM #5 Hko Senior Member   Registered: Aug 2002 Location: Groningen, The Netherlands Distribution: ubuntu Posts: 2,530 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 04:27 PM.