Archived

This topic is now archived and is closed to further replies.

redneckCoder

hex data type?

Recommended Posts

Is there a data type in C or C++ that will store a hex value? I was thinking a char array, but I''m not sure. What do you guys think? Thanks. -AJ C:\DOS C:\DOS\RUN RUN\DOS\RUN -Comic Book Store Guy''s t-shirt that I saw on the Simpsons, although it didn''t actually come from the Simpsons. http://vdsoft.netfirms.com/home.html

Share this post


Link to post
Share on other sites
thnx, is that also true of binary values?

-AJ

C:\DOS
C:\DOS\RUN
RUN\DOS\RUN

-Comic Book Store Guy's t-shirt that I saw on the Simpsons, although it didn't actually come from the Simpsons.

http://vdsoft.netfirms.com/home.html

[edited by - redneckCoder on April 29, 2002 11:02:31 PM]

Share this post


Link to post
Share on other sites
Yeah. You can''t assign binary values directly, though. You''ve gotta do some masking. Don''t worry about it, though, you''ll know when you actually have to use it.

Share this post


Link to post
Share on other sites
although c/c++ has hex storage capabilities built in, it is sometimes desirable (if we''re doing displaying of hex) to write yourself a class to handle that stuff.

im blabbing on and on and on...

Qatal: im a delphi coder.
Forum Guy: ah!...that explains it. now should i keep flaming you? difficult decision.

Share this post


Link to post
Share on other sites

  
// print hex to stdout

printf("%08X", someValue);
// where 08 represents the number of bytes to print from the value

// lower case x shows lowercase letters while capital X shows uppercase



simple ne?

all data is stored as binary, its up to you to interpret it. to use hex constants use the prefix 0x (example 255=0xFF (or 0xff)). there should be methods for using binary constants and octel constants as well as printing them using printf (see the docs). remeber hex and binary are just different bases, nothing special about them.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by redneckCoder
Is there a data type in C or C++ that will store a hex value? I was thinking a char array, but I''m not sure. What do you guys think? Thanks.

-AJ

C:\DOS
C:\DOS\RUN
RUN\DOS\RUN

-Comic Book Store Guy''s t-shirt that I saw on the Simpsons, although it didn''t actually come from the Simpsons.

http://vdsoft.netfirms.com/home.html


Correct me if I am wrong. If you want to store the hex in literal form ie "0xAAF" you''d have to us a char array, but if you wanted to store the numarical equiv. then you simple need to find a data type large enough to hold it. I think most C\C++ compiler are able to deal with something like:

int num = 0xf;

Share this post


Link to post
Share on other sites
Thats lame though, theres a hex data type (0x...)but not binary, so if you want to use stright binary you have to convert to hex first, although that is not difficult it still takes time.
Maybe someone should write a binary class, that would be cool, and it would convert to different types for you eg:
BINARY Var = 1000101101;
Var += 10100;
cout << Var.ConvertToLong() << " in binary is " << Var << endl;
Should not be to difficult at all, heck even I could write that!!!

CEO Plunder Studios

Share this post


Link to post
Share on other sites
check out std::bitset:


#include <iostream>
#include <string>
#include <bitset>
using namespace std;

int main()
{
bitset<16> bs1("0000111100001111");
bitset<16> bs2(0x0f0f);

// output bits
cout << bs1 << endl;
cout << bs2 << endl;

// convert to std::string
cout << bs2.to_string() << endl;

// convert to unsigned long
cout << bs2.to_ulong() << endl;

// output different bases
cout << showbase;
cout << dec << bs2.to_ulong() << endl;
cout << hex << bs2.to_ulong() << endl;
cout << oct << bs2.to_ulong() << endl;

// manipulate bits
cout << bs2[0] << endl;
bs2[0] = !bs2[0];
cout << bs2[0] << endl;

cout << bs1 << endl;
bs1 &= 0xff00;
cout << bs1 << endl;
}


[edited by - kvh on April 30, 2002 5:08:36 AM]

Share this post


Link to post
Share on other sites