First of all, using CP_ACP means you don't really have a control of the target code page - it depends on the users's settings. Sometimes it is exactly what you want, sometimes it is not.
Anyway, if we assume that your system locale is Windows CP 1252 and you have some Greek characters in Text, your code will try to convert Greek characters to CP1252 and because there is no mapping between the two scripts, you are going to get replacement characters (?) instead.
To convert Greek text from const wchar* value to char value to char*, you'll need to use CP1253 (not sure if I spelled the constant correctly) instead of CP_AP.
This question is related to my previous question. As i mentioned before, my project contains many dlls and i am facing some ambiguity due to two dlls which contains same namespace names. So, i want to change the paths of these to dlls. But i dont know how to. Is their any option in visual studio c++ so that i can easily solve my problem?
I am having problem in converting ANSI to UNICODE but my code is not giving the desired result. Please have a look at the below code and advice me. When I compare the both strings, the strings are not equal after conversion. Please help me. Many thanks in advance.
char szData ;
szData = '♠';
szData = 0;
wchar_t wszData1 = L"♠";
MultiByteToWideChar(CP_ACP, 0, szData, -1, wszData2, sizeof(wszData2) / sizeofwchar_t));
if (wcscmp(wszData1, wszData2) == 0)
MessageBox("Strings are equal!");
MessageBox("Strings are not equal.");
These probably aren't doing what you're expecting. You should only have ASCII characters in source code, and use hex escapes for any characters outside the ASCII range. Depending on what that character is, and what your system's code page is, it may not even be possible to store the character in a non-Unicode string.
Many thanks for your replies and suggestions. Actually I read a data from the serial device and it is having the ASCII value 6 and the corresponding character for 6 is '♠'. I just give the previous code as an example.
//the actual data will be
szData = 0x06;//from a serial device
Now, I want to display the corresponding character (received from serial device) for 0x06 in the CEdit. Please advice me.
I have tried in many possible ways but I cannot display the character ♠. The real problem is that I need to change my entire project to UNICODE but this is not possible for right now. I also tried the below and even not working.
display = 0x2660;
display = 0;
edit.SetWindowText(display); //This only works when I change the project into UNICODE.
Is there any possible way to do simply. Many thanks in advance.
If you use HeapAlloc [^] function (instead of GlobalAlloc) then you have the option to use also HeapReAlloc [^] for the purpose.
If you're stuck with GlobalAlloc then you have to allocate a new, bigger memory block whenever is required, copy the original block content on the newer one and then free the original block.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke [My articles]
You can use memcpy for concatenation.
Only thing is that you must correctly specify the offset.
When the first packet is received - memcpy(bmpBuffer, lpData, dwCount);
Increment a count variable - dwTotalCount += dwCount;
When the next packet is received - memcpy(bmpBuffer + dwTotalCount, lpData, dwCount);
«_Superman_» I love work. It gives me something to do between weekends.
How are you knowing how many bytes are to be read? I've seen most people throw some random and huge number at it. You might want to try IOCtl with FIONREAD this if you aren't doing it already there:
DWORD dwHowMuch = 0;
IOCtl(FIONREAD, &dwHowMuch); //After this call, <code>dwHowMuch</code> will tell you how many bytes should be read with the Receive call.
Receive(m_szBuffer.GetBuffer(dwHowMuch), /*other params*/);
//Now, szBuffer has the data received from the socket.
Note that I'm letting CString do the gruesome stuff of memory allocation and deallocation for me (because CString already has a GetBuffer() method, and I can exploit that here). I'm not proposing this as an excellent solution, but it works and you will be free of any hand-crafted memory management that you might want to implement. I, for once, believe that CString will manage the memory much better than we do (OK, we can write something better, but what the heck... OOP is about reusability and what GetBuffer() has to offer has been good enough for me).
Also note that passing a number to CString::GetBuffer() will dynamically 'adjust' the string buffer to hold at least "that much" bytes that was passed to it (mind you this does not include the terminating zero character, but here, we aren't dealing with a string anyways). This will ensure you won't run out of memory and if the previously allocated memory was good enough to hold the data to be currently received, no harm is done!
I assumed that you're using MFC, but if you are not, I see your query is already answered. Also, note that you don't 'need' MFC as such if you want to just use CString.
It is a crappy thing, but it's life -^Carlo Pallini