Thread: Hex

  1. #1
    Banned
    Join Date
    Jun 2005
    Posts
    594

    Hex

    Suppose i wanted to store a sieres of hexadecmial
    numbers in a vector.

    would i need to define it vector<int> ?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    32
    Depends if you want their corrosponding value or if you want to keep the actual 0x2F format.

    for example.

    Code:
    int main()
    {
    	vector<int> hexs;//or Long if your doing big hex 0x3423F3
    	hexs.push_back(0x12);
    	cout<<hexs[0]<<endl;
    	getch();
    }
    Will produce 18 as output, since its the corrosponding decimal Value.

    If you want to keep the 0x12 format you could just do them as strings.

    Code:
    int main()
    {
    	vector<char *> hexs;
    	hexs.push_back("0x12");
    	cout<<hexs[0]<<endl;
    	getch();
    }
    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..

    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.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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:
    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;
    }
    Should output:
    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

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    32
    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?

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    thank you, both answer helped

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Does that mean if you add to values together it will return the correct hex value?
    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.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 06:37 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Replies: 3
    Last Post: 01-23-2006, 07:25 PM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 09:02 AM