|
To store the bytes into an int , I would do:
BYTE by[4] = { 0x30, 0x27, 0, 0 };
int n;
n = *(int*) &by[0]; Keep in mind that the order of the bytes is CPU-specific. On x86 it's low->high as I showed above.
Tom Wright wrote:
And if I type cast it as an int then I get 0x0012f408 as a value, which is not right.
That's because a cast isn't a magical command that makes the compiler read your mind. You need to understand exactly what a cast does, and avoid just "throwing in casts" hoping to make it work. (This isn't a flame, it's a difficult subject, I know.)
Casting by to int returns the value of &by[0] , which in your case was 0x0012f408
--Mike--
Personal stuff:: Ericahist | Homepage
Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt
CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ
----
If my rhyme was a drug, I'd sell it by the gram.
|
|
|
|
|
Michael Dunn wrote:
Keep in mind that the order of the bytes is CPU-specific. On x86 it's low->high as I showed above.
I see what you are saying. Here is the value that I see when I view it thru quick watch: 0x30270000 Which is reversed from what is in the array.
Okay so if I reverse this then I'll get the right Hex value in my int.
Questions:
1. The value I see in quick watch...is this the hex value or the int value?
2. If it's the hex value then I still need to convert this to a usable int. What I mean is that this BYTE array is part of a data stream from a socket.
<br />
struct ParseData{<br />
BYTE STAP[4]; <br />
BYTE BlockLen[4];<br />
BYTE HDRLen[2];<br />
BYTE FunctionCode[2];<br />
BYTE ValField[4];<br />
BYTE DataLen[4];<br />
BYTE MsgStatus[2];<br />
BYTE Dest[2];<br />
};<br />
ParseData myStuff;<br />
memcpy(&myStuff, dataBuffer, sizeof(myStuff));<br />
char* DataPacket = new char[myStuff.DataLen];<br />
It tells me the length of another array in the stream which is a datapacket. So this BYTE array is the packet lenght. I've set up the data packet as a char* Datapacket; Can I initialize it like this:
char* DataPacket = new char[n]; ??? and will this be the size of 10032 or 2730?
Really I'm trying to understand this and I really appreciate your help
Thanks
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
I got it..here is my code:
<br />
BYTE NewDataLen[4];<br />
NewDataLen[0] = myStuff.DataLen[3];<br />
NewDataLen[1] = myStuff.DataLen[2];<br />
NewDataLen[2] = myStuff.DataLen[1];<br />
NewDataLen[3] = myStuff.DataLen[0];<br />
<br />
int nDataLength;<br />
nDataLength = *(int*) &NewDataLen[0];<br />
CString someString;<br />
someString.Format("Here is the value: %d", nDataLength);<br />
AfxMessageBox(someString);<br />
I'm sure there is an easier way to do the reverse but my model is "simplicity is best"...ah I'll tweak this as I go.
Now that I have my DataLen I can size my char array and put in my data.....
Thanks for the help again.
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
|
You could (and should) just use the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/ntohl_2.asp">ntohl()</a> function to fix the byte order of 32-bit ints received through sockets.
|
|
|
|
|
I posted a question earlier about SafeArrayAccessData(), but I've made a few changes to my code since then. Now when I don't delete the pointer that was allocated on the heap, I have memory leaks reported. When I try to delete the pointer to free the memory my program crashes in "dbgheap.c" in the _CrtIsValidHeapPointer() function. Does anyone know what might be going on and why I can't delete the pointer? It's not null, but stepping through the code, I see it gets changed after the calls to SafeArrayUnAccessData().
[insert witty comment here]
bdiamond
|
|
|
|
|
bdiamond wrote:
I see it gets changed after the calls to SafeArrayUnAccessData()
First off, you're getting an assert, not a crash. When the assert failed message comes up, click Retry or Break (depending on your version of VC) and look at the test that failed. Most likely, the CRT caught the fact that you have a bug - the memory was written to after it was freed.
--Mike--
Personal stuff:: Ericahist | Homepage
Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt
CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ
----
You cannot stop me with paramecium alone!
|
|
|
|
|
I don't know if you looked at the earlier message I posted, but I set another pointer's address (pBytes) to that of the initial array (pBuf) and then I don't touch pBuf again (or pBytes, for that matter). All I do after that is read from the array to create the file. How could the memory be getting written to?
[insert witty comment here]
bdiamond
|
|
|
|
|
Hi!
I'm kinda new to C++ programming and I have difficulty understand and working with pointers. I have TCHAR *someVariable; and I wanna know how many characters are in the string pointed by the pointer "*someVariable".
I tried:
<br />
int len;<br />
len = wcslen(allTexPath);<br />
but I get the following error:
(173): error C2664: 'wcslen' : cannot convert parameter 1 from 'TCHAR *' to 'const wchar_t *'
Thanks for helping
Gimpy
----------------------
Be proud. Be yourself.
|
|
|
|
|
Are you compiling for UNICODE or MCBS?
Regards,
João Paulo Figueira
Embedded MVP
|
|
|
|
|
For the moment not of them :P it's "Not Set" I can chose any of the two... which is the most appropriate choice? MBCS should be fine, I won't use any weird characters or oriental language.
Gimpy
----------------------
Be proud. Be yourself.
|
|
|
|
|
Choose one of them and be careful with the string functions you choose. The safest way is to use the tchar.h macros that will correctly map to the appropriate functions. For instance, wcscpy is a UNICODE function that you cannot mix with TCHAR when MCBS is defined.
Regards,
João Paulo Figueira
Embedded MVP
|
|
|
|
|
If you are going to use TCHAR, you have to use the _t version of functions.
try:
len = _tcslen(allTexPath);
TCHAR, _T, and _t -- are part of MS's UNICODE/MBCS neutral stuff. Makes code that can compile as MBCS or UNICODE -- as long as you do it right.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<A HREF="http://www.santacruznetworks.com">Santa Cruz Networks</A>
|
|
|
|
|
Take a look at my string articles, they describe the various encodings, how to use TCHAR, and the diff between strlen and wcslen.
part 1[^] | part 2[^]
--Mike--
Personal stuff:: Ericahist | Homepage
Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt
CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ
----
Four fonts walk into a bar. The bartender says "Hey - get out! We don't want your type in here."
I just performed the Heimlich Maneuver on SQL #1.
-- Chris Maunder
I just performed the Heimlich Maneuver on SQL #1.
-- Chris Maunder
|
|
|
|
|
Any recommendations for a (preferably free, preferably open-source) C/C++ source code formatter?
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
|
Thanks! I've actually found a bunch of tools (including Artistic Style) on the web and was looking for thumbs up/down opinions. So I'm going to assume your's is a thumbs up.
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
Hi,
I found several unit testing tools for c++ here <http: www.opensourcetesting.org="" unit_c.php=""> but not sure which one is good. Is there one in particular you like?
Thanks
Hua-Ying
|
|
|
|
|
|
We use Boost.Test which has been terrific.
Open Source Testing is a good source for testing frameworks in most languages
If you can keep you head when all about you
Are losing theirs and blaming it on you;
If you can dream - and not make dreams your master;
If you can think - and not make thoughts your aim;
Yours is the Earth and everything that's in it.
Rudyard Kipling
|
|
|
|
|
Thanks, also useful for me
|
|
|
|
|
Hi,
Is it possible to output 2 different signals to 2 monitors? For example one displays the regular windows desktop and the other something completely different such as OpenGL full screen stuff (or something simpler than openGL).
For example if you have a graphics card that has 2 outputs how much control is available? Or if you installed 2 graphics cards (one onboard and one AGP or PCI or whatever) is it possible then?
Can someone point me in the right direction?
Thanks
|
|
|
|
|
I used two monitors back when I was developing for Windows 3.x. One was color and the other was monochrome. Since each was located at a different memory address, it was easy to write to both. The GUI stuff went to the color monitor and the debug stuff went to the monochrome monitor. I got really used to it and sorely missed it once Windows 9x came out.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
You're saying it can't be done right now that way. In either case I believe I have a solution. I configured my 2 monitors in Windows and I create a CFrameWnd derived object using Create(...) and I pass the location and size to this function such that the window is created past the first screen size, and Windows automatically creates this on the second monitor. Now I have this frame that I cran draw stuff in. However I'm still having trouble making the frame truly full screen. Any help? I've tried the ChangeDisplaySettings() function however that changes the first monitor to full screen and not the second one. Any help?
|
|
|
|
|
Anonymous wrote:
You're saying it can't be done right now that way.
No, I was just reflecting on the past. I've not ever used Windows with the new dual-monitor feature. When I saw it being used, the guy spent way too much time moving his head back and forth between the two. I didn't see the point.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|