|
itoa
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
For Question One :-
would wsprintf or CString::format is of any use!
J_E_D_I wrote: Second question :
How come the condition below results in an error? How should it rather be written?
char CHARACTER = "1";
if (CHARACTER == "1") {cout << "True";}
use ' instead of "
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You/codeProject$$>
|
|
|
|
|
I have a 32bit int that I'm trying to stuff into an array of bytes.
Is it okay to do this?
CharArray = new unsigned char [4];
CharArray = (unsigned char *)&IntegerValue
ByteArray[Length-4] = CharArray[3]
ByteArray[Length-3] = CharArray[2]
ByteArray[Length-2] = CharArray[1]
ByteArray[Length-1] = CharArray[0]
|
|
|
|
|
Yes it's ok (to cast from int * to unsigned char *).
Also is that a typo? You overwrite the allocated CharArray...
should that have been:
ByteArray = new unsigned char [Length];
CharArray = (unsigned char *)&IntegerValue
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
No, that's not a typo.
The ByteArray is actually a different array type, but is still an array of unsigned char.
If it is okay to get the IntegerValue into the unsigned char[4] then I'm okay.
so...
CharArray = new Unsigned Char [4];
CharArray = (unsigned char*)&IntegerValue ;
That should still be fine. The 4 Unsigned Char bytes will then be populated with the bytes from the integer.
|
|
|
|
|
ChemmieBro wrote: No, that's not a typo
In that case you have a problem ...
CharArray = new unsigned char [4];
CharArray = (unsigned char *)&IntegerValue //<--- overwrites the pointer allocated above!!
The storage has already been allocated - it is the 4 bytes used by the
IntegerValue variable.
You only need the second line above...
CharArray = (unsigned char *)&IntegerValue;
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I see that, now.
Thank you. I just needed a quick answer and got it.
I'm just re-using some code and they had done this same thing using an integer and a two shorts. I figured if it worked that way, it would work using an integer and four unsigned chars. Now I'll have to change the old stuff, too.
Thanks again.
|
|
|
|
|
Have you considered using a union ?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: using a union
I like your solution!
Maxwell Chen
|
|
|
|
|
No. This is existing code and I was just curious if I was allowed to do that or not. I thought I could but couldn't find anything out there to confirm that it would work correctly.
I will look up Unions, though, for future reference.
Thank you.
|
|
|
|
|
ChemmieBro wrote: No. This is existing code
You still can use union in the existing code! And you won't even need to cast.
ChemmieBro wrote: I thought I could but couldn't find anything out there to confirm that it would work correctly.
You have the debugger of VC++ for you to watch the memory addresses and the values.
Maxwell Chen
|
|
|
|
|
Maxwell Chen wrote: You still can use union in the existing code!
Except for the swapping of byte order (if I read the original code correctly).
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Another way is...
int nNum = 10;
char Data[4] = {0};
memcpy(&Data, &nNum, 4);
|
|
|
|
|
I'm using VC++ 6 service pack 6 on Windows 2000. I'm working with an automation server MDI application with the following requirements.
1) Can be started by user with visible interface.
2) Only one instance at a time.
3) If controlled by client, runs in background (invisible) initially.
4) Client(s) has option to make application visible.
5) If visible and user attempts to close with client(s) connections, warning is given with choice to close or not.
6) Has menu item that allows user to disconnect from all clients.
7) If invisible, closes when last client disconnects.
I have no problem with 1-4, the questions arise with 5-7. In order to implement these, the server needs to know if there are clients connected to it. My main automation class (CAutoApp) is instantiated every time a client contacts it. In the constructor I add to a static array of LPUNKOWNs (m_lpUnk[MAXNUMCONN]) using GetInterface(&IID_IUnknown):
LPUNKNOWN CAutoApp::m_lpUnk[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
CAutoApp::CAutoApp()
{
EnableAutomation();
for (int i=0; i < MAXNUMCONN && NULL != m_lpUnk[i]; i++);
if (i < MAXNUMCONN)
m_lpUnk[i] = GetInterface(&IID_IUnknown);
}
Whenever a client closes, CMainFrame::OnClose is called in the server. So I have the following code to implement 5 and 7 in OnClose():
int i=0;
while(i < MAXNUMCONN && NULL != CAutoApp::m_lpUnk[i])
{
try
{
CAutoApp::m_lpUnk[i]->AddRef();
CAutoApp::m_lpUnk[i++]->Release();
}
catch(...)
{
for(int j=i; j<MAXNUMCONN-1 && NULL != CAutoApp::m_lpUnk[j]; j++)
CAutoApp::m_lpUnk[j] = CAutoApp::m_lpUnk[j+1];
}
}
if (NULL != CAutoApp::m_lpUnk[0])
{
if (IsWindowVisible())
{
if (IDNO == AfxMessageBox("One or more remote connections is still active. Exit anyway?", MB_YESNO))
return;
}
else
return;
}
.....
CMDIFrameWnd::OnClose();
This works, although maybe it's a no-no to use a try-catch this way. If there's a better way to do it, I'd like to know.
To implement (6), I have the following:
void CMainFrame::OnCloseconnections()
{
if (IDNO == AfxMessageBox("This will close all connections. Are you sure?", MB_YESNO))
return;
for (int i=0; NULL != CAutoApp::m_lpUnk[i]; i++)
{
try
{
while(CAutoApp::m_lpUnk[i]->Release());
}
catch(...)
{
}
CAutoApp::m_lpUnk[i] = 0;
}
}
This works, however when a user attempts to close my application, it crashes somewhere in the OLE dlls. There must be a better way to do what I'm trying to do?
Thanks,
Gary
|
|
|
|
|
I have a program that reads contents of a text file. There is this one Swedish word that gives me trouble. The word is KALVFÄRS. When my program reads, read buffer contains KALVFÄRS, which is different than what's in the file. But the word is shown correctly in EditPlus. I need my program to read the way EditPlus reads.
I first used Win32 api to read, then tried with file io functions (fread) to read. I still have not found a way to read it correctly. Does any one know how EditPlus reads, or is there anything I should try?
BTW, language is set to Swedish in my computer.
|
|
|
|
|
Try to read in binary mode?!
Maxwell Chen
|
|
|
|
|
I already did, but no difference.
|
|
|
|
|
|
|
Holy cow, man! Assembly language AND Swedish! You rock
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Norwegian.
-David Delaune
|
|
|
|
|
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
That's just happy Swedish
|
|
|
|
|
its urgent...............pls.........
|
|
|
|
|