| | |
Arrays and functions help -- base converter
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
Hi there. I'm trying to write a program that will convert any number from base x to base y (bases 2-16). The user will also be able to convert hexadecimal numbers like 4BFA from base 16 to base 2.. which is 100101111111010.
So far I've began to prompt the user for the number, ask for the old base, the new base. I am having trouble trying to convert the numbers that are input into a char array into integers so I can perform calculations on them. Essentially, I'd like to scan through each of the letters if it's a hex string and replace those with the equivalent numerical value.
Here is the code I have so far:
I know I still need my calculations, but I'd like to do this the right way before I start into that other part.
Any help would be greatly appreciated!
So far I've began to prompt the user for the number, ask for the old base, the new base. I am having trouble trying to convert the numbers that are input into a char array into integers so I can perform calculations on them. Essentially, I'd like to scan through each of the letters if it's a hex string and replace those with the equivalent numerical value.
Here is the code I have so far:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <cctype> using namespace std; int main() { char numIn[15]; int num[15]; char values[17] = "0123456789ABCDEF"; // array used for table lookup int ob, nb, i; cout << "Enter the the number you want to convert: "; cin >> numIn; // convert string to upper case *note* i'm using _strupr_s because vc '08 likes to give me a nasty deprication warning _strupr_s(numIn); cout << "\n Enter the original base: "; cin >> ob; // range checking if(ob < 2 || ob > 16) { cout << "Bases are 2-16. Please re-enter:" << endl; cin >> ob; } cout << "\n Enter the new base: "; cin >> nb; // range checking if(nb < 2 || nb > 16) { cout << "Bases are 2-16. Please re-enter:" << endl; cin >> nb; } // convert the char array to string string s; s = numIn; cout << "\nAfter converting the char array, the string is: " << s << endl; // find the string length for the loop int length = strlen(numIn); cout << "\nThe length of the string is: " << length << endl; // loop through each subscript of the input for(i=0; i < length; i++) { cout << num[i] << " "; // check to see if it's a letter or number if(isdigit(numIn[i]) == true) { num[i] = numIn[i] - 48; } else { num[i] = numIn[i] - 55; } } // reverse the string for later use cout << "\nAfter reversing the string, the string is: "; for(i = length - 1; i >= 0; i--) { cout << numIn[i]; } cout << endl; return 0; }
I know I still need my calculations, but I'd like to do this the right way before I start into that other part.
Any help would be greatly appreciated!
You can use this in your function :
* Test if string[i] is a character and then decimal value of that character (assumed to be a hex value A-F or a-f) can be done by :
With the above procedure you'll get the decimal value of your hex digit now you can go on with your computations without any problem right ???
* Test if string[i] is a character and then decimal value of that character (assumed to be a hex value A-F or a-f) can be done by :
dec_value = 10+(int)((int)string[i]-65) (for upper case) ordec_value = 10+(int)((int)string[i]-97) (for lower case) .With the above procedure you'll get the decimal value of your hex digit now you can go on with your computations without any problem right ???
Last edited by csurfer; Jul 3rd, 2009 at 8:10 am.
I Surf in "C"....
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
Ok thanks for the help. I got that part figured out now. It turns out I was using
and I shouldn't have been using
Now I'm trying to figure out how to reject any input that is from "G"-"Z" as those aren't valid hexadecimal letters. Any help?
C++ Syntax (Toggle Plain Text)
if(isdigit(numIn[i]) == true)
and I shouldn't have been using
C++ Syntax (Toggle Plain Text)
== true
Now I'm trying to figure out how to reject any input that is from "G"-"Z" as those aren't valid hexadecimal letters. Any help?
•
•
Join Date: Jan 2008
Posts: 3,840
Reputation:
Solved Threads: 503
•
•
•
•
Ok thanks for the help. I got that part figured out now. It turns out I was using
C++ Syntax (Toggle Plain Text)
if(isdigit(numIn[i]) == true)
and I shouldn't have been usingC++ Syntax (Toggle Plain Text)
== true
Now I'm trying to figure out how to reject any input that is from "G"-"Z" as those aren't valid hexadecimal letters. Any help?
Ascii value of 'G' is 71. Ascii value of 'Z' is 90. Any character between 71 and 90, inclusive, is to be rejected.
C++ Syntax (Toggle Plain Text)
char digit = 'J'; if (digit >= 71 && digit <= 90) cout << "Illegal digit";
Have you considered using this function?
http://www.cplusplus.com/reference/c...type/isxdigit/
![]() |
Similar Threads
- Base converter (C++)
- can you help me in this program (C)
- Using functions and arrays to create a program for grades (C++)
- Can arrays be sent to functions in C? (C)
- arrays and functions (C++)
- returning arrays from functions (C++)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- Base converter (C++)
- Accessing functions from base class (C)
Other Threads in the C++ Forum
- Previous Thread: Filename problem in Windows?
- Next Thread: [c++] ISO C++ forbids declaration of _ with no type
Views: 475 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






