Thread: number conversion help

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    Question number conversion help

    I am trying to make a program to change the base of numbers from base 10 to anything they want (from 2-36). the problem arises when i am trying to convert the numbers to a hex format. I have no clue where i should go, the code i have written so far is
    Code:
    void convert(vector<int>& rev_rems,int base,int num_div,vector<int>& rems)
    {
       int num;
       int rem;
       for(num=num_div;num!=0;num/=base)
       {
          rem=num%base;
          rev_rems.push_back(rem);
       }
       for(rev_rems;rev_rems.size()!=0;rev_rems.pop_back())
       {
          rems.push_back(rev_rems[rev_rems.size()-1]);
       }
    }
    
    void hex_convert(vector<int>& rems,vector<int>& rev_hex)
    {
       for(int hex=rems[rems.size()-1];rems.size()!=0;rems.pop_back())
       {
          hex=hex-10;
          if(hex<0)
          {rev_hex.push_back(rems[rems.size()-1]);}
          else
          {rev_hex.push_back('0'+hex);}
          }
    }
    the converting part works fine, its the hex_convert where i get confused on what to do

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why can't you just call convert from hex_convert?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    i could, however, i want to know how to convert it to the hex format before i condense them down, easier to follow what im doing, and then this way i can check to make sure that the answers i'm getting when i do the first conversion are still correct

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then look at your hex_convert code and ask yourself "What was I on when I wrote this?"

    What does subtracting 10 have to do with converting to hex? Why are you always pushing the last digit of your original into your answer? Why is '0' involved? Where is the number 16?

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    the 10 comes in from the fact that there are 10 single digit numbers (0-9), then the double digit numbers (10-99), at 10 is when the program would start using letters, A=10, B=11, etc., however to advance the count i need to be able to add the the values of A,B,C etc, starting at 65 which is A, if you were to add 10 to 65 it come up as 75!=A but ==J, so the -10 is there to keep the location of the number associated with the letters correct, otherwise i would skip the first 9 letters of the alphabet and jump straight to J.

    The '0' was me trying to play around and figure out how to get the letters to show instead of numbers.

    as to the question u posed about the number 16, i dont understanding what you are asking about it

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you have to decide whether the elements in your vector are the characters '0' through '9', 'A' through 'F', or the numbers 0 through 15. (Hint: whoever wrote the first convert function assumed the latter.) For instance: if you subtract 10 from any of the characters '0' through '9', 'A' through 'F' you will not get anywhere near the number 0. If you subtract 10 from the real numbers themselves, it would make sense to check against 0; but in that case it didn't make sense to subtract 10 in the first place, since you already have the real value of the number.

    I am still very interested in how you intend to convert to base 16 without ever including the number 16 in your code.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    I must apologize for wasting your time just now, i just realized that i am trying to use the character value references for calling the letters. A=65 to Z=90, i am NOT trying to convert to hex, this was a huge blind sight on my part, basically what i am trying to do is similar to hex, but only use the rest of the alphabet if it is needed.

    What i am trying to do is converting from dec code into char code
    Last edited by Creatlv3; 05-07-2010 at 08:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  4. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM

Tags for this Thread