Suppose i wanted to store a sieres of hexadecmial
numbers in a vector.
would i need to define it vector<int> ?
Suppose i wanted to store a sieres of hexadecmial
numbers in a vector.
would i need to define it vector<int> ?
Depends if you want their corrosponding value or if you want to keep the actual 0x2F format.
for example.
Will produce 18 as output, since its the corrosponding decimal Value.Code:int main() { vector<int> hexs;//or Long if your doing big hex 0x3423F3 hexs.push_back(0x12); cout<<hexs[0]<<endl; getch(); }
If you want to keep the 0x12 format you could just do them as strings.
But clearly this doesn't allow you to get the actual hex value =P so it probably isn't what you want. but maybe it is..Code:int main() { vector<char *> hexs; hexs.push_back("0x12"); cout<<hexs[0]<<endl; getch(); }
If your looking for a way to add hex values... hmm you could use strings but you would have to write the calculator yourself where you would just parse the hex values and then do the math based on the characters that you pull out. <shrug>
But afaik there are no built in types with C++ that store the hex value in its actual hex form.
Yes, use a vector<int> container. If you need to do any output and have the results come up as hex, just use the hex stream manipulator.
Taking Charmy's example and tweaking it a bit:
Should output:Code:#include <iostream> #include <iomanip> #include <vector> using namespace std; int main() { vector<int> hexs; hexs.push_back(0x12); cout << hex << showbase << hexs[0] << endl; cin.get(); return 0; }
Code:0x12
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Hey cool, i learned somthing new today =). didn't know showbase did that. Very cool... Does that mean if you add to values together it will return the correct hex value?
thank you, both answer helped
I don't think there's any such thing as a hex value. The only thing computers know how to do is move around numbers in binary format. When you output a number in hex format, you're just telling the computer to take a binary number and make it look a certain way. How you display a value has no effect on how it is calculated internally.Does that mean if you add to values together it will return the correct hex value?
Code:#include <iostream> using namespace std; int main() { int num = 2; cout<<num<<endl; cout<<hex<<showbase<<num<<endl; cout<<oct<<showbase<<num<<endl<<endl; int another_num = 24; cout<<dec<<another_num<<endl; cout<<hex<<showbase<<another_num<<endl; cout<<oct<<showbase<<another_num<<endl<<endl; int sum = num + another_num; cout<<dec<<sum<<endl; cout<<hex<<showbase<<sum<<endl; cout<<oct<<showbase<<sum<<endl; return 0; }
Last edited by 7stud; 06-22-2005 at 10:47 PM.
hex, decimal, binary, octal.. those are just visual representations of the numbers to be human readble. the value of a number isn't afected by it's representation