|
Hello gurus,
I'd like to know how I can convert a DWORD (32 bits) value into a byte array of four bytes (32 bits)?
<br />
DWORD dwLength=(DWORD) file.GetLength();<br />
BYTE length[4];<br />
length contains the value dwLength as a 4 bytes array.
Thanks
Best regards.
There is no spoon.
|
|
|
|
|
for (int i = 3; i >= 0; --i)
{
length[3 - i] = (dwLength & (0xff << (i * 8))) >> (i * 8);
}
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it! Honoured as one of The Most Helpful Members of 2004
|
|
|
|
|
DWORD dwLength=(DWORD) file.GetLength();
BYTE length[sizeof(DWORD)];
*(DWORD*) &length[0] = dwLength; Keep in mind that any code doing anything with the bytes will have to account for the endian type of the CPU.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ
Strange things are afoot at the U+004B U+20DD
|
|
|
|
|
Hi,
Thanks
Actually I've found the following way:
<br />
length[0]=HIBYTE(HIWORD(dwLength));<br />
length[1]=LOBYTE(HIWORD(dwLength));<br />
length[2]=HIBYTE(LOWORD(dwLength));<br />
length[3]=LOBYTE(LOWORD(dwLength));<br />
which also works
There is no spoon.
|
|
|
|
|
Or how about:
BYTE *pLength = (BYTE*)dwLength;
pLength[0] will equal the low byte (assuming an intel machine)
What do you need this for?
|
|
|
|
|
It works, but not the same way as my code in my earlier post. Yours forces big-endian order. Which isn't wrong per se, it's just something to be aware of. Don't try to write that byte array out to a file and read it back in as a DWORD , for example.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ
Strange things are afoot at the U+004B U+20DD
|
|
|
|
|
Hi
DWORD dwData=0x65A6;
BYTE arrDest[4];<br />
memcpy(arrDest,&dwData,sizeof(DWORD));<br />
|
|
|
|
|
|
Priyank Bolia wrote:
...but not good
Why? This is the method I've used in years past. I find it easier to read than a bunch of pointers ands casts.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I just write that thinking that people like it or not. I still use it in imaging programs, where image documents are large size and memory and speed are a constraint. It saves memory and helps out small programmers from the pointers nightmare, and the main point is it will save precious time as compare to for loops and memcpy operations etc.
http://www.priyank.in/
|
|
|
|
|
hello,
how can I compare two HTML files and know if there text contents has changed and highlight the changed parts.
|
|
|
|
|
google for beyond compare
|
|
|
|
|
I have many shared printers on my windows network. I want to use the API function StartDoc to initialize a print job. How can I get to know the HDC of every printer I have access to. Thank you.
modified 9-Mar-17 17:22pm.
|
|
|
|
|
Hi all,
I'm working on an MFC based application dealing with EN_CHANGE events. In my program, the user has to enter a parameter in a Edit Box, that starts an events:
void CExcitaPage::OnEnChangeAmplitude()
{
//Event...
}
I need to disable this event to configure the program first. Does someone can help me?
NikoiD.
|
|
|
|
|
Are you saying that OnEnChangeAmplitude() is being called when the dialog is first displayed, rather than just when the user makes a change? If so, try:
class CExcitaPage : public CDialog
{
private:
bool m_bInitializing;
};
CExcitaPage::CExcitaPage()
{
m_bInitializing = true;
}
BOOL CExcitaPage::OnInitDialog()
{
CDialog::OnInitDialog();
m_Amplitude.SetWindow(...);
m_bInitializing = false;
return TRUE;
}
void CExcitaPage::OnEnChangeAmplitude()
{
if (! m_bInitializing)
{
}
}
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Use a flag that is set to false when your app starts and is set to true once your program is configured. Check this flag in the EN_CHANGE handler.
void CExcitaPage::OnEnChangeAmplitude()
{
if (m_bIsPageInitialized)
{
}
}
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it! Honoured as one of The Most Helpful Members of 2004
|
|
|
|
|
Hi PJ Arends,
Thank you for your answer! I already tryed our solution, but for a reason that I can't determine, the program crashes because of the declaration of my variable. In the class related to my program's page, I declare as public variable:
<br />
public:<br />
int Configured;<br />
and then I initialize to zero this variable before the configuration. Afterthat I set the variable to one and check in in my handler like that:
<br />
<br />
void CExcitaPage::OnEnChangeAmplitude()<br />
{ <br />
if (Configured) <br />
{ <br />
}<br />
}<br />
When I try to close the program, I become a classic Debug Error, Damage Block...
Do you have an other idea?
Thank you in advance
NikoiD.
|
|
|
|
|
You obviously have other errors in your program. I use this technique frequently if no problems. The debugger's error messages will usually give you a clue as to where the error is, although some detective work may be necessary.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it! Honoured as one of The Most Helpful Members of 2004
|
|
|
|
|
Hi,
I am trying to use 'LoadLibrary()' & 'GetProcAddress()' functions to call the following function :
'D3DXCreateTextureFromFile(...)'
Now, this function is typedefed in 'd3dx8tex.h' as
#ifdef UNICODE<br />
#define D3DXCreateTextureFromFile 3DXCreateTextureFromFileW<br />
#else<br />
#define D3DXCreateTextureFromFile 3DXCreateTextureFromFileA<br />
#endif
So I wrote the following piece of code in my prog hoping to get things running at runtime...
################################################<br />
<br />
.<br />
.<br />
.<br />
<br />
typedef HRESULT (WINAPI *PFTEXT) (LPDIRECT3DDEVICE8, LPCSTR,LPDIRECT3DTEXTURE8*);<br />
<br />
HINSTANCE hLib1;<br />
PFTEXT pfText1;<br />
<br />
hLib1 = LoadLibrary("D3D8.DLL");<br />
<br />
if(hLib1 == NULL)<br />
return false;<br />
<br />
pfText1 = (PFTEXT) GetProcAddress hLib1,"D3DXCreateTextureFromFileA");<br />
<br />
if(pfText1 == NULL)<br />
{<br />
FreeLibrary(hLib1);<br />
return false;<br />
}<br />
<br />
if(FAILED(pfText1( m_pD3DDevice,szTextureFilePath,&m_pTexture)))<br />
{<br />
FreeLibrary(hLib1);<br />
return false;<br />
}<br />
<br />
FreeLibrary(hLib1);<br />
return true;<br />
<br />
.<br />
.<br />
.<br />
<br />
################################################
though it seems a working code it is not running properly giving NULL in 'pfText1' ie the function is not getting located from DLL. Also i think ( ) that there is a problem in DLL itself ie i am not able to find out where this function is defined (in which DLL). Please help me get this thing to work...
thxx.
gogizmo
|
|
|
|
|
its easier to put into the include directive in afx.h
Its better to use apparently the AfxLoadLibrary
heres a link for load library
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_link_explicitly.asp[^]
save your self trouble and type
#include <D3DX8.h>
at the top. make sure in tools->options->projects you have the paths linked up.
Building and executing a D3D sample will let you know.
my 2 cents from my experience, but I am in know an expert at c++
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
has anyone seen code that finds mersenne prime numbers???
|
|
|
|
|
Google for the GIMPS project. I was going to refer you to the Mersenne site but it is currently down.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
|
how can i find out the information about my computers processor like - Manufacturer, Clock Speed etc.
|
|
|
|
|
Try the Win32_Processor WMI class.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|