Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Another teaser.

Options
  • 22-08-2000 5:35pm
    #1
    Registered Users Posts: 21,264 ✭✭✭✭


    Be intresting to see how people answer.

    Nice and easy...

    You have to create a function that will convert from Decimal to Hex and back.

    So the call would be something like...

    ConvertBase( Number, BaseFrom , BaseTo )

    eg.
    Answer = ConvertBase( 100, 10, 2)

    Answer would equal "1100100".

    Now you may need to add another base, but you haven't been told which one yet. What is the best way to code the function to help you later on?



Comments

  • Subscribers Posts: 1,911 ✭✭✭Draco


    Originally posted by Hobbes:
    Be intresting to see how people answer.

    Nice and easy...

    You have to create a function that will convert from Decimal to Hex and back.

    So the call would be something like...

    ConvertBase( Number, BaseFrom , BaseTo )

    eg.
    Answer = ConvertBase( 100, 10, 2)

    Answer would equal "1100100".

    Now you may need to add another base, but you haven't been told which one yet. What is the best way to code the function to help you later on?


    ConvertBase( Number, BaseFrom , BaseTo )
    {
    switch Baseto{
    case 2: return ConvertToBinary(Number, BaseFrom)
    default: return error;
    };
    };

    ConvertToBinary (Number, BaseFrom)
    {
    switch BaseFrom{
    case 2: return Number;
    case 8: do octal->binary conversion
    case 10: do decimal->binary conversion
    etc.
    default: return error;
    };
    };

    Add case statements for each type of conversion and a function to actually do the conversion.

    Draco



  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Ok you gave me the typical answer. biggrin.gif However there is a better way.


  • Registered Users Posts: 310 ✭✭Cerberus


    How about this..me thinks that even if it works that it only works for converting to numbers that are powers of 2. i.e. 2,4,8,16....

    Convert number to binary first of all regardless of whether baseTo[/] is binary or not and then convert to baseTo

    The code would look something like this.


    i=0;
    while number/baseFrom >=1
    {
    array = number mod 2;
    number = number/2;
    i++;
    }
    //by rights you would need a linked list instead of an array.

    //write code to invert the order of the values in the array.
    Now depending on what baseTo is divide the array into bunchs. e.g. if it is octal then divide into bunchs of 3, if hex - bunchs of 4 and so on. Now all that is left to do is convert this binary to decimal and group the resulting numbers together. I know that for hexidecimal you will have to make 10 = A and so on til 15 = F.

    This is probably as wrong as Christianity but I hadn't much time.
    Will deal with all the ridicule tomorrow.
    nite


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Ok to make it more intresting (and kind of give a hint). Lets say I want to convert from Base 2 to Base 26 and all the bases in between each way.



  • Registered Users Posts: 3,308 ✭✭✭quozl


    I'm hungover and couldnt be arsed to write the code but all you have to do is
    to convert from any base to another
    number * original base = number
    keep do
    {
    remainder=number % (modulus) new_base
    number=number / new_base (be sure to round down)
    store remainder in array or linked list.
    }
    repeat for new number

    This splits out the characters of the new base one by one and will work from base 2 to infinity.(well not infinity but right up to it wink.gif
    Greg



  • Advertisement
Advertisement