number base converter

This is a discussion on number base converter within the C Programming forums, part of the General Programming Boards category; hi i found the itoa non standard function to convert numeber from base 10 to other base here a test ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    number base converter

    hi

    i found the itoa non standard function to convert numeber from base 10 to other base

    here a test code i have written
    Code:
    int main() {
       char bin[25];
        int n;
       scanf("%d",&n);
       itoa(n, bin, 2);
       printf ("binary: %s\n",bin);
        return 0;
    }
    now.. how can i do conversion from numbers in base other than 10 to base 10?

    i hadn'd understood if the atoi function can help me

    thanks in advance && sorry for my bad english!
    Last edited by didrochi; 03-26-2010 at 10:53 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,958
    Use strtol().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    i searched info on strtol()

    so if i want to convert a binary number to 10-base
    i must have the binary number in bin
    and i must have base=2
    and i will found in n the 10-base number?
    Code:
    n = strtol(bin, NULL, base);
    all right?

    but bin could be a int or must be bin[]??

    thanks in advance

    and sorry for my ignorance in C and also english!!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,958
    bin must be an int for a long. Once you have it as an int, printing the int in whatever base representation you want is another matter.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    what means "an int for a long"?


    this will work? and is correct?

    Code:
    int bin,num;
    
    bin=0100101;
    
    num = strtol(bin, NULL, 2);
    
    
    printf("%d converted in 10-base is: %d", bin, num);
    thanks.
    Last edited by didrochi; 03-26-2010 at 11:46 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,958
    Oh, sorry, I misread your post.

    Let's clarify: given an integer value stored an int, you wish to store its representation in some base other than base 10 in a string? If so, then unless these bases are 8 or 16, I believe that you would have to perform the conversion yourself using some arithmetic.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    strol()?? it converts char string to long integer. can u convert 10011011 in binary to octal system using strol

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    there isn't need that the number that should be converted is an int

    the number is obtained from a scanf

    what is the type of the bin variable in the row:
    num = strtol(bin, NULL, 2);

    ?

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by magestrium View Post
    strol()?? it converts char string to long integer. can u convert 10011011 in binary to octal system using strol
    Numbers are stored internally as binary.

    You do not onvert numbers...

    What you can do - read string representation of the number in one format and output number as string in another format...

    strtol could read string representation of the number in any base from 2 to 36

    When you have read number you can print it as octal, dec or hex using printf, or in any other representation using your own function...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,958
    Quote Originally Posted by didrochi
    the number is obtained from a scanf
    That would not matter; once it is read into an int, the process will be the same.

    Quote Originally Posted by didrochi
    what is the type of the bin variable in the row:
    num = strtol(bin, NULL, 2);

    ?
    No, strtol() wouldn't work. I misread your original post. I thought that you were trying to use atoi() with base conversion, but you are actually looking to use something along the lines of sprintf(). sprintf() can handle conversion to octal and hexadecimal, but not other bases (besides decimal, of course).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    i understood almost anything


    so how becomes my function that read a number (base other than 10) from scanf
    then read (scanf) the number base

    and convert it to 10-base??

    Code:
    int n, base;
    printf ("Enter a number: ");
    scanf ("%d",&n);
    printf ("enter base of the number inserted: ");
    scanf ("%d",&base);
    
    ...
    so should i use a function like this?
    Code:
    int convert_base_to_10(int base, int x) {
        return (x < base) ? x : convert_base_to_10(base, x / 10) * base + (x % 10);
    }
    and there is not a "direct" function to conversion like the reverse itoa??

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    scanf ("%d",&n);

    this is only good is number is decimal...

    so enter the number as string and use strtol afterwards - when you really know the base
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    someone has a link of a source of a base converter from and to any base (2-16) for study how it work

    i had searched in google but i've not found something simple to understand..

    thanks in advance

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    5,827
    It's not actually all that hard.

    Code:
    If value is negative multiply by -1.
    Clear result string.
    Repeat
       Compute value modulo base.  Append representation of that value to result string.
       Divide value by base (integral division)
    Until value is zero
    If original value was negative, append a '-' character.
    Reverse string.
    For bases 2-36 this works with a simple mapping of digits to character: Digits 0 through 9 map to '0' through '9'. Remainders/digits 10 through 35 map to 'A' through 'Z'.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-08-2010, 06:15 PM
  2. 128-bit number to senary (base 6) efficient tranlation
    By t_maggot in forum C Programming
    Replies: 2
    Last Post: 06-05-2008, 10:30 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Number converter, MSVC++ 5.0
    By TravelByMail in forum Windows Programming
    Replies: 1
    Last Post: 03-10-2002, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21