|
|
Can somebody tell me what I am doing wrong what the correct syntax is for formatting your output in binary representation.
I have been using printf("%b\n",variable) like the the help files says in .NET but it just keeps outputing a b on the console.
And does anyone know the correct syntax in C for packing your structures?
Help.
Thanks.
|
|
|
|
|
I think you have been looking at the wrong printf format specification.
There is no format that outputs binary in C (neither std or ms extension).
There is no specific packing or alignment keywords in C.
Use the pack pragma and the align declspec.
|
|
|
|
|
Okay, I found something about that pragma but Im not sure how it works with packing the data. Hmmm. Got any ideas.
I found: pragma pack(push, 1) or something like that. Not sure what it does though.
Does C++ have binary formatting?
|
|
|
|
|
About formating, nope. Actually it is printf that lacks it and it's the same for both languages.
Concerning packing, what are you trying to accomplish? Kind of hard to try to help without more info...
|
|
|
|
|
What I'm trying to do is write a struct in C with a couple data types in it, i.e. int, char.
<pre>typedef struct
{
int x;
char c;
}_attribute_((packed))myStruct</pre>
This syntax is for djgpp compiler, I was wondering what you use for .NET or Visual C 6.
|
|
|
|
|
#pragma pack(push, 1)
What this does is set the struct packing to 1-byte boundaries (i.e., no padding), while saving the previous packing. Specify this before your struct, then use #pragma pack(pop) after the struct to restore the old packing. Example:
<br />
#pragma pack(push,1)<br />
struct MyStruct {<br />
int foo;<br />
char a;<br />
};<br />
#pragma pack(pop)<br />
- Mike
|
|
|
|
|
Thanks for the help, hopefully with I can get this thing working.
|
|
|
|
|
Sirrius wrote:
Can somebody tell me what I am doing wrong what the correct syntax is for formatting your output in binary representation.
I have been using printf("%b\n",variable) like the the help files says in .NET but it just keeps outputing a b on the console.
You'll need to write a xxxToBinary() conversion routine. Then you can use printf("%s\n", ...) to output the result.
Sirrius wrote:
And does anyone know the correct syntax in C for packing your structures?
By default, structures are aligned on 8-byte boundaries. To align on some other boundary, such as 4 bytes, use:
#pragma pack(4)
before the structure/union is declared.
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
Thanks for the advice on the packing. Im not to clear on the xxxToBinary() conversion routine you talked about. How do I go about doing that?
Thanks for your help.
|
|
|
|
|
Here's a sample:
void DWORD_To_BinaryString(DWORD value,char string[])
{
for (int bit = 0; bit < 32; bit++) {
DWORD mask = 0x80000000 >> bit;
if ((value & mask) == mask) string[bit] = '1';
else string[bit] = '0';
}
string[32] = '\0';
} Note that this function converts any 32-bit value to a 32 character string or 1's and 0's. The destination array must be at least 33 characters long.
Software Zen: delete this;
|
|
|
|
|
I'm using embedded structures, and would like to access members of the embedded structure, but am having difficulties with the compiler accepting code regarding the instantiation of the embedded structure. Here's the code:
struct MajorStruct
{
struct MinorStruct
{
int cnt;
};
};
=====================
later on I do this:
====
MajorStruct majStruct;
majStruct.MinorStruct minStruct;
minStruct.cnt = 0;
The error from the compiler is that I cannot have a C-style cast right of the "." operator.
How do I instantiate the embedded structure so that access to its members can be obtained?
Thanks for any suggestion!
William
Fortes in fide et opere!
|
|
|
|
|
The nested struct is instantiated as part of its parent, so you don't want to instantiate it separately. To access its members try: majStruct::MinorStruct.cnt = 0;. Mind you I could be wrong here.;)
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
Err, no, I think you're wrong.
What you have there is a nested class. The struct MinorStruct is scoped within the MajorStruct structure. This applies to the visibility of names.
However, it's just another struct. Just because it's declared inside MajorStruct doesn't mean that MajorStruct gets a MinorStruct member; you have to explicitly say so.
To create a variable of type MinorStruct , you need to declare it as
MajorStruct::MinorStruct minStruct; Note the use of the scope resolution operator :: and the use of the class name, not the object name, for the outer scope.
If you want MajorStruct to have a member of type MinorStruct , say:
struct MajorStruct
{
struct MinorStruct
{
int cnt;
};
MinorStruct m_minStruct;
}; You can refer to MinorStruct using simply its name within the scope of MajorStruct .
|
|
|
|
|
Thanks for replying. Your suggestion provided the solution!!
William
Fortes in fide et opere!
|
|
|
|
|
Yes your right of course. I shouldn't try answering questions before my first coffee or two of the day.
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
Why do you actually declare class inside another class?
I've always wondered why.
Rickard Andersson
Here is my card, contact me later!
UIN: 50302279
Sonork: 37318
Interests: C++, ADO, SQL, Winsock, 0s and 1s
|
|
|
|
|
Usually for some information-hiding reason. For example, you might need to store data in a POD (Plain Old Data) structure in order to store it to and from some binary file format. However, you don't want users of your class to access this directly.
Normally you'd put the declaration in an implementation file, but you might want to have a member variable of that type. To do this, the structure must be declared in the header, so you put it in a private area of the header.
At other times, you might want a parameter class. You might have a method on one class that requires a large number of parameters, many of which are optional (which probably means you've overloaded the method too heavily, but sometimes it's necessary). The Refactoring people suggest that you should replace the large number of parameters with a single parameter of a class type.
The class is only used in this one method, so to avoid polluting the namespace, you declare the parameter class inside the class in which it's used (obviously here you would declare it public or protected , depending on the scope of the method that uses it as a parameter.
|
|
|
|
|
In my case, I'm doing it for a little of the second reason you gave and a third for encapsulation (meaning, the inner structure provides a kind of specialized service for the outter structure, which the outter structure assigns to it as a sort of "black box" requirement).
That "black box" requirement, has an interface which the outter structure uses, but which ANY other class or structure can use, which makes the usefullness of the inner structure "portable".
William
Fortes in fide et opere!
|
|
|
|
|
hy, to instatiate the embedded struct you must do this:
MajorStruct::MinorStruct minStruct;
greets
andreas
|
|
|
|
|
Thank you for your answer. It was a good answer, and I appreciate it.
William
Fortes in fide et opere!
|
|
|
|
|
I'm using WSAEventSelect() to register events, FD_READ, FD_WRITE, etc to handle in my program. I have a section of code like:
WSAEnumNetworkEvents(pConnect->m_oSkt, *pwsaEvents, &wsaNetEvent)
if((wsaNetEvent.lNetworkEvents & FD_READ) &&
wsaNetEvent.iErrorCode[FD_READ_BIT] == 0)
{
// do stuff
}
:
:
I get an FD_ACCEPT event on the server as I expect and an FD_WRITE on the client as I expect, but when I call send() on the client, I get wsaNetEvent.lNetworkEvents == 0 on the server. I don't understand why it would equal 0. Under what conditions would it equal 0? Any help would be appreciated.
Thanks,
melinda
|
|
|
|
|
I implemented a control (derived from CWnd) that implements a context menu. When the user selects something from the menu, the picture covering the whole control changes.
The picture is stored in an offscreen buffer and WM_PAINT only repaints the portion uncovered. I capture which item was chosen from the context menu right there with TrackPopupMenu and the TPM_RETURNCMD flag.
The problem is that when the pop-up context menu is disappearing it leaves some of itself on the control. The portion left behind is not of the same shape everytime.
While debugging I found this:
If, after a full control repaint (invalidating the whole control), I pop and hide the menu without any change to the control, the menu invalidates the area it was covering (let's call this Area A) and produces the artifact. From then on (as long as I don't issue a full control repaint), when I pop and hide the menu, it invalidates ONLY the area of itself that intersects with the area it 1st covered (Area A), making this new area Area A, and produces the artifact in this area only. So, if I pop and hide the menu making the intersection empty, it in fact doesn't invalidate anything and the artifact is not produced; Area A becomes empty and from then on there is no artifact until I invalidate the whole control, beginning this process again.
Note that the artifact does not cover all of Area A. As an example the artifact would be in the middle of Area A, which means WM_PAINT actually painted some of it (above and below).
Another fact is that if I delay the full control repaint for, let's say, 1 second, the artifact is not produced. But I don't want to delay anything.
If I move another window over the artifact, producing a WM_PAINT, it clears the artifact away.
I think the pop-up menu is somehow painting MY control's DC at the same time (as in concurrent threads) as my WM_PAINT. If this is the case, is there a way to prevent this?
Please help me! Any light on the issue will be greatly appreciated.
|
|
|
|
|
|
Registry hives are not kept in one central location. It's going to depend on whether it's for a user (HKCU), or the machine (HKLM), and which OS it is running on (Windows 9x uses different locations than Windows NT/2000).
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|