Is it possible to convert a GUID value into a DWORD? - mfc

I have seen how to convert a GUID into a CString but what about a DWORD.
Possible?

A UUID/GUID is 128 bits. A DWORD is only 32 bits. If by "convert" you mean "convert without losing data", then the answer is no.

Related

Safeway to cast Windows CreateFile handle to 'long' and vice versa in C++

Is it safe or correct way to cast windows CreateFile return type ("HANDLE") to long datatype and vice versa ie long value to HANDLE type?
If the approach is correct then how it can be done in Windows 7 Environment?
32 bit handling will be fine but any portable code to handle 64 bit version it can be a best shot.
HANDLE is probably implemented as an opaque pointer, so casting it to/from uintptr_t shouldn't give you problems. But it is not really safe. No conversion from implementation-defined datatype to integer is safe, and no conversion from pointer to integer is safe.
Pointers should remain pointers.
Implementation-defined types should not be converted.
Integer should stay integers.
There is no way to correctly cast a HANDLE to a long and back.
Consider that on 64-bit Windows, long is 32 bits long and HANDLE is 64 bits long.

conversion from 'double' to 'DWORD', possible loss of data

I have added a resource in my project. Its size is 4.096 byte.
i need to assign it to DWORD because 3rd argument of WriteFile() needs it.
WriteFile(hFile, pExeResource,size, &bytesWritten, NULL);
So i cast it like this:
UPDATE2: DWORD bootsize = 4096; //and still warns me
But i get this warning. can you plesae help me?
I think this is a combination of regional/language differences along with C-syntax.
I assume you mean 4.096 to be 4,096 with a comma separator. (Due to regional/language differences, you probably use . instead of ,.)
In C/C++, you don't use the separators. Just do this instead:
DWORD bootsize = (DWORD)4096;
4.096 is a floating point value (of type double), just less than 4.1, while DWORD is an integral type (usually something like an unsigned int). Naturally you lose the fractional part of the floating-point value in the conversion. (Perhaps you meant the integer value 4096 (no punctuation)?)
4.096 is four and 96/1000ths of a byte. I think you just want 4096. Then you don't have to cast. Typically you don't use a comma or period in numbers in programming unless you really mean it as a decimal point.

C++: I have a string representing 8 bits. How Do I convert it to a char?

ie. "11111111" should convert to 0b11111111 / 255 (in dec)
Try strtol with a base of 2.
Another possibility would be value = std::bitset<8>("11111111").to_ulong(). This is more specialized for binary than strtol, so it could provide advantages if you might want to manipulate some bits. E.g., if you wanted to read a number, flip bit 5, and then convert.
You say specifically 8 bits, so:
static_cast<char>(std::bitset<8>(str).to_ulong());

convert uint64 to uint32

I have a uint64 variable ...but the function in which I want to pass it only accepts uint32.
I tried using static unsigned int ToUInt32(uint64 variable); reference MSDN, but it gives an error that precision lost.
Is there any way to convert uint64 to uint32 without losing any data?
Well think about what you are trying to do. You are taking a number which will potentially take up to 64bits and then cutting in half.
Of course you are going to lose data in this instance.
If you are sure that the number being given is not going to be larger than uint32 then you can try this
uint64 largeNumber = 9876543210;
uint32 smallNumber = largeNumber & 0xFFFFFFFF;
But this WILL lose you half of your number if it's bigger than 32bits.
No. You can't fit two gallons of water into a one-gallon container, and you can't fit 64 bits of data into a 32-bit value.
No. A 32 bit integer has 32 bits less than a 64 bit integer. A 64 bit integer can contain anything a 32 bit integer can, but not vice versa.
Is there any way to convert uint64 to uint32 without losing any data?
Yes, but only if you know certain characteristics of your possible uint64 values and those characteristics allow a lossless conversion.
What is most likely relevant here is knowing your values will never exceed the maximum value of a uint32. In this case, what you actually have is a 32-bit value stored in 64 bits and the conversion simply changes how it is stored rather than changing the value. To do the conversion in this case, simply cast it, such as with static_cast.
If you want to handle any arbitrary uint64 value, then this is simply impossible. If it were possible, you could compress any file, including a 40 terabyte database, down to a single 32-bit value by repeated application of the conversion.

Base 256 GUID String Representation w/Character Offset 0x01F

I need to make a string representation of a 128bit GUID that represents 8bit chunks instead of 4bit; so like base 256 :/ Why? Because I need to shorten the length of the string hex representation of the GUID, it is too long. I have a max array size of 31 plus NULL terminator. Also the character value cannot be from 0x000 to 0x01F.... It's giving me a headache but I know it can be done. Any suggestions on the best and safest way how? Right now I'm just mucking around with memcpy and adding 0x01F but the numbers keep bouncing around in my head.... can't nail 'em down no! And I have to interconvert :\
Try base64 encoding : it packs 6-bits per character and is still portable. You will need 22 chars to store 128 bits GUID in a portable way.
Having that OLESTR first convert it into a GUID with IIDFromString(), then use base64 as user Peter Tillemans suggests.