hex data type?
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
Any data type can hold hex data. That said, you''ll probably find it easiest to work with unsigned longs (DWORDs).
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[if you have a link proposal, email me.]
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[if you have a link proposal, email me.]
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]
-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]
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.
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.
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.
// print hex to stdoutprintf("%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.
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;
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
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
check out std::bitset:
[edited by - kvh on April 30, 2002 5:08:36 AM]
#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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement