Thread: Hi there I'm new beginner. Decimal/Octal converter help/

  1. #1
    Registered User
    Join Date
    Sep 2022
    Posts
    2

    Hi there I'm new beginner. Decimal/Octal converter help/

    I downloaded this code from some site after a search. As you can see it has a limit(295600127). Why? is there a mistake in the code. . anyways I want to make it work correctly. With someones help of course. Basically explail what is going on below the first return 0;

    Code:
    /*295600127currentlimit*/
    
    
    
    #include <stdio.h>
    #include <math.h>
    
    
    
    
    int convertDecimalToOctal(int decimalNumber);
    int main()
    {
    int decimalNumber;
    
    
    
    
    printf("Enter a decimal number: ");
    scanf("%d", &decimalNumber);
    
    
    
    
    printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber));
    
    
    
    
    return 0;
    }
    
    
    
    
    int convertDecimalToOctal(int decimalNumber)
    {
    int octalNumber = 0, i = 1;
    
    
    
    
    while (decimalNumber != 0)
    {
    
    
    octalNumber += (decimalNumber % 8) * i;
    decimalNumber /= 8;
    i *= 10;
    }
    
    
    
    
    return octalNumber;
    }
    Just to prove i'm a beginner this is my own dec/oct converter:
    It would only do up 2147483647 until I figured out to unsign my int's
    I guess I can't wordwrap my code huh?

    Code:
    /*decimal_octal_converter*/
    
    
    
    #include <stdio.h>
    /*---------------*/
    
    
    
    
    unsigned int dec, oct;
    
    
    
    
    int main(void)
    
    
    
    
    {
    printf("Enter a number between 0 and 4294967295: ");
    scanf ("%d", &dec);
    
    
    
    
    
    
    
    
    printf("Octal value: %d%d%d%d%d%d%d%d%d%d%d", dec/(8*8*8*8*8*8*8*8*8*8), dec%(8*8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8*8), dec%(8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8), dec%(8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8), dec%(8*8*8*8*8*8*8)/(8*8*8*8*8*8), dec%(8*8*8*8*8*8)/(8*8*8*8*8), dec%(8*8*8*8*8)/(8*8*8*8), dec%(8*8*8*8)/(8*8*8), dec%(8*8*8)/(8*8), dec%(8*8*8)%(8*8)/8, dec%8);
    
    
    
    
    return 0;
    }



    And this is the easiest one:
    It took me a while to learn that %o means substitute the decimal number with it's equivalent octal value. In fact if yoy replace the o with an x it wil give the hex value. I couldn't figure out how just modulus%ozero could do all that figuring. Obviously I now know that's not a zero but an "o".

    Code:
    /* C Program to Convert Decimal to Octal Number */
    
    
    
    #include <stdio.h>
    int main()
    {
    int number;
    printf("\n Please Enter the Number You want to Convert : ");
    scanf("%d", &number);
    
    
    
    
    printf("\n Octal Number of a Given Number = %o", number);
    
    
    
    
    return 0;
    }
    Last edited by ManyBeers; 09-05-2022 at 10:33 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > As you can see it has a limit(295600127). Why? is there a mistake in the code.
    Because it's faking it by storing only digits 0 through 7 in an otherwise decimal integer.

    295600127 decimal is 2147477777 octal.
    But the first code just stores it in a decimal integer (that's the %d format), and that's close to the max decimal value of 2147483647

    > I guess I can't wordwrap my code huh?
    You can add newlines almost anywhere you can use space.
    Code:
    printf("Octal value: %d%d%d%d%d%d%d%d%d%d%d", 
        dec/(8*8*8*8*8*8*8*8*8*8), 
        dec%(8*8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8*8), 
        dec%(8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8), 
        dec%(8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8), 
        dec%(8*8*8*8*8*8*8)/(8*8*8*8*8*8), 
        dec%(8*8*8*8*8*8)/(8*8*8*8*8), 
        dec%(8*8*8*8*8)/(8*8*8*8), 
        dec%(8*8*8*8)/(8*8*8), 
        dec%(8*8*8)/(8*8), 
        dec%(8*8*8)%(8*8)/8, 
        dec%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.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Salem View Post
    Because it's faking it by storing only digits 0 through 7 in an otherwise decimal integer.
    Binary Coded Decimal (BCD) is a thing, where we "fake" storing decimal digits in an otherwise binary integer. Is this thread just an example of Decimal Coded Octal?

  4. #4
    Registered User
    Join Date
    Sep 2022
    Posts
    2
    Quote Originally Posted by christop View Post
    Binary Coded Decimal (BCD) is a thing, where we "fake" storing decimal digits in an otherwise binary integer. Is this thread just an example of Decimal Coded Octal?
    I'm a beginner I can't fake anything. If youcan would you tell me exactly what every line of this snippet does. I know it is part of that long function highlighted. I also know it creates 2 variables
    octalnumber=0 and i=1 I also know what modulos does.
    Code:
    int convertDecimalToOctal(int decimalNumber){
    int octalNumber = 0, i = 1;
    
    
    
    
    
    
    
    
    while (decimalNumber != 0)this says while decimalNumber is not zero
    do the following
    {
    
    
    
    
    octalNumber += (decimalNumber % 8) * i;(what this does)
    decimalNumber /= 8;(what this does)
    i *= 10;(what this does)
    }
    
    
    
    
    
    
    
    
    return octalNumber;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [BEGINNER] Binary To Decimal Converter Help
    By Sankait Laroiya in forum C Programming
    Replies: 15
    Last Post: 09-17-2014, 07:21 AM
  2. [BEGINNER] Decimal to Binary Converter help
    By Sankait Laroiya in forum C Programming
    Replies: 7
    Last Post: 09-11-2014, 10:10 AM
  3. Converting Octal to decimal
    By lavaboy in forum C Programming
    Replies: 26
    Last Post: 03-21-2014, 07:11 PM
  4. Decimal, Octal & Hexadecimal
    By mie2cpr in forum C Programming
    Replies: 2
    Last Post: 03-01-2011, 10:56 AM
  5. Decimal to Hex Converter
    By rocketman03 in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:50 AM

Tags for this Thread