LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 05-20-2004, 02:36 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
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 <stdio.h>         
#include <stdlib.h>        
#include <string.h>

/********************************************/

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();
  }
 
Old 05-20-2004, 03:09 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
"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 <stdio.h>
#include <stdlib.h>        
#include <string.h>

/********************************************/

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;
}
 
Old 05-20-2004, 03:10 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,537

Rep: Reputation: 149Reputation: 149
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.
 
Old 05-20-2004, 03:14 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,537

Rep: Reputation: 149Reputation: 149
Hko, the orginal author wants decimal->octal, not octal->decimal conversion
 
Old 05-20-2004, 03:21 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
python: converting a 3 character octal string into and ASCII char llama_meme Programming 1 07-06-2010 02:00 PM
Converting number to locale's abbreviated month name tsilok Linux - Newbie 4 10-25-2005 05:18 PM
Read in an Octal number from a text file using C++ pjordan Programming 2 11-18-2004 03:03 PM
How do I add two octal number ? Linh Programming 3 05-20-2004 03:08 PM
converting fake hex to decimal in c acid_kewpie Programming 10 08-20-2003 02:29 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration