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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-20-2004, 02:36 PM
|
#1
|
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 <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();
}
|
|
|
05-20-2004, 03:09 PM
|
#2
|
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 <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;
}
|
|
|
05-20-2004, 03:10 PM
|
#3
|
Moderator
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696
|
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
|
Moderator
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696
|
Hko, the orginal author wants decimal->octal, not octal->decimal conversion
|
|
|
05-20-2004, 03:21 PM
|
#5
|
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.
|
|
|
All times are GMT -5. The time now is 02:05 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|