I'm trying to write a program that converts a string to a vector of ints to binary to hexadecimal. The reason it's started off as a string instead of an int is because an int has a limit, so the string is supposed to work for any huge number.
So far I have converted a string to a vector of ints. But now I'm not really sure how to convert a vector to a binary number.
I have:
#include%26lt;iostream%26gt;
#include%26lt;vector%26gt;
#include%26lt;string%26gt;
using namespace std;
int main(void)
{
cout%26lt;%26lt;"Type in a string: ";
string s;
cin%26gt;%26gt;s;
// converting the string to a vector
vector%26lt;int%26gt;v;
for (int i = 0; i %26lt; s.length(); i = i + 1)
{
v.push_back(s[i]-48);
}
// printing out the vector
cout%26lt;%26lt;"vector v is: ";
for (int i = 0; i %26lt; v.size(); i = i + 1)
{
cout%26lt;%26lt;v[i];
} cout%26lt;%26lt;endl;
return 0;
}
Any help would be great, thanks!
Converting a vector of ints into binary in C++?
/* outline the program */
// void main()...etc..
/* define your variables */
/* you'll need a pair of char pointers */
/* and some numbers to hold converted values */
/* input the first binary as a string */
/* input the second binary as a string */
/* convert them to decimal */
/* add them */
/* fprintf them */
/* end program */
or
You can use a look-up table for the characters 0 to 9 and A to F
0 = 0000
1 = 0001
2 = 0010
...
D = 1101
E = 1110
F = 1111
Split your hex value into each character, get the corresponding binary value as a string, concatenate the strings together in the same order.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment