convert const wchar* to const byte*
-
i want to check whether its possible to do like that.. am learning vc++ now...that is why asked .. if it is possible, pls enlighten me how to do that one.. thanks, rakesh
Carlo asked you what would you be doing with the
const BYTE
buffer after such a "conversion".It is a crappy thing, but it's life -^ Carlo Pallini
-
:wtf: Are you aware of the fact that a BYTE is 8 bits and A WCHAR is 16 bits?
It is a crappy thing, but it's life -^ Carlo Pallini
-
i want to check whether its possible to do like that.. am learning vc++ now...that is why asked .. if it is possible, pls enlighten me how to do that one.. thanks, rakesh
Your code performs the conversion (i.e. the cast) of the pointer, not of the string. In other words the string buffer remains unchanged, it is just interpreted diffently. Since that maybe fine or maybe a plain catastrophe, I need to know what is the purpose of your 'conversion'. :)
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] -
Hi Rajesh, I am not aware of it...so does it mean that its not at all possible to convert??
Sure, you can reinterpret a pointer to a dialog as a pointer to a socket. It is possible to achieve that. But WHY?! After such a conversion (assuming you did it), what will you want to be doing with the BYTE buffer?! Answer that.
It is a crappy thing, but it's life -^ Carlo Pallini
-
Your code performs the conversion (i.e. the cast) of the pointer, not of the string. In other words the string buffer remains unchanged, it is just interpreted diffently. Since that maybe fine or maybe a plain catastrophe, I need to know what is the purpose of your 'conversion'. :)
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]CPallini wrote:
I need to know what is the purpose of your 'conversion'.
Just noticed t hat we both have put that word in quotes. Fits the context perfectly. Subtly expressing something, ya know. :-D
It is a crappy thing, but it's life -^ Carlo Pallini
-
Sure, you can reinterpret a pointer to a dialog as a pointer to a socket. It is possible to achieve that. But WHY?! After such a conversion (assuming you did it), what will you want to be doing with the BYTE buffer?! Answer that.
It is a crappy thing, but it's life -^ Carlo Pallini
Hi All, I am actually learning vc++.. i just want to know whether it can be converted or not.. whether it is feasible or not.. my seniors have used a functions in which they have typecast like wat i have mentioned in the first message..they are sending type-cast parameter as an arugment to other functions..but it is not casting properly....its taking the first character alone.. if i say, const wchar* test = L"hello"; const BYTE* test1 = (const BYTE*)test; o/p will be 'h' alone.. Hence raised in this forum whether you people can enlighten me about this issue.. thanks, rakesh.
-
Hi All, I am actually learning vc++.. i just want to know whether it can be converted or not.. whether it is feasible or not.. my seniors have used a functions in which they have typecast like wat i have mentioned in the first message..they are sending type-cast parameter as an arugment to other functions..but it is not casting properly....its taking the first character alone.. if i say, const wchar* test = L"hello"; const BYTE* test1 = (const BYTE*)test; o/p will be 'h' alone.. Hence raised in this forum whether you people can enlighten me about this issue.. thanks, rakesh.
Rakesh5 wrote:
if i say, const wchar* test = L"hello"; const BYTE* test1 = (const BYTE*)test; o/p will be 'h' alone..
If you do need to convert the wide char string into a char one, then use a conversion macro instead, for instance (assuming a ANSI build, like yours):
const wchar_t * wstrTest = L"Hello";
CW2A strTest( wstrTest );
AfxMessageBox( strTest);Rakesh5 wrote:
my seniors have used a functions in which they have typecast like wat i have mentioned in the first message..
I hope your seniors know what they are doing. :)
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] -
Hi All, I am actually learning vc++.. i just want to know whether it can be converted or not.. whether it is feasible or not.. my seniors have used a functions in which they have typecast like wat i have mentioned in the first message..they are sending type-cast parameter as an arugment to other functions..but it is not casting properly....its taking the first character alone.. if i say, const wchar* test = L"hello"; const BYTE* test1 = (const BYTE*)test; o/p will be 'h' alone.. Hence raised in this forum whether you people can enlighten me about this issue.. thanks, rakesh.
A BYTE is an 8 bit data type. A WCHAR is a 16 bit data type. Now, you can cast a BYTE pointer to a WCHAR pointer or vice versa. However, since these data types are of different sizes, and if you cast an array of BYTES to an array of WCHARs, the conversion will succeed, but the resultant buffer will be unusable (the code "won't work"). In other words, you only casted the pointer. You can call a horse a dog, but however, it won't bark. OK, I know. That's not a good example, but you get the drift. :)
Rakesh5 wrote:
my seniors have used a functions in which they have typecast like wat i have mentioned in the first message..they are sending type-cast parameter as an arugment to other functions..but it is not casting properly....its taking the first character alone..
I have a feeling that your seniors are not the brightest sparks for you to learn from. Or ask them to give you an explanation of what they're trying to achieve and ask them how such a "conversion" could make sense.
It is a crappy thing, but it's life -^ Carlo Pallini
-
Hi, i am trying to convert const wchar* to const byte*. but its not converting fully...can anyone help me out to solve this issue... ------------------------------------- code snippet: const WCHAR* test = L"hello"; const BYTE* test1 = (const BYTE*) test; AfxMessageBox((CString)test1); ---------------------------------------------- thanks, rakesh
const BYTE* test1 = (const BYTE*) test; is a type cast, it means your are changing the pointer type. Search for "String Conversion". It is a wide area. There are some fine ATL-macros: http://msdn.microsoft.com/en-us/library/87zae4a3%28VS.80%29.aspx[^] :suss:
Press F1 for help or google it. Greetings from Germany
-
Rakesh5 wrote:
if i say, const wchar* test = L"hello"; const BYTE* test1 = (const BYTE*)test; o/p will be 'h' alone..
If you do need to convert the wide char string into a char one, then use a conversion macro instead, for instance (assuming a ANSI build, like yours):
const wchar_t * wstrTest = L"Hello";
CW2A strTest( wstrTest );
AfxMessageBox( strTest);Rakesh5 wrote:
my seniors have used a functions in which they have typecast like wat i have mentioned in the first message..
I hope your seniors know what they are doing. :)
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] -
Hi, Actually i have made my character set as unicode and not as multibyte in the project settings.. so its not converting fully.. is there any way to do by keeping the current settings? thanks, rakesh.
Then use
const wchar_t * wstrTest = L"Hello";
CW2A strTest( wstrTest );
AfxMessageBox( (CString) strTest);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] -
Hi, i am trying to convert const wchar* to const byte*. but its not converting fully...can anyone help me out to solve this issue... ------------------------------------- code snippet: const WCHAR* test = L"hello"; const BYTE* test1 = (const BYTE*) test; AfxMessageBox((CString)test1); ---------------------------------------------- thanks, rakesh
Simply casting an 8-bit type to a 16-bit type is not going to produce the expected result. Consider:
8 8 8 8 8
╓─╥─╥─╥─╥─╖
║H║e║l║l║o║
╙─╨─╨─╨─╨─╜16 16 16 16 16
╓─┬─╥─┬─╥─┬─╥─┬─╥─┬─╖
║H│0║e│0║l│0║l│0║o│0║
╙─┴─╨─┴─╨─┴─╨─┴─╨─┴─╜In the top figure, each character uses 1 byte or 8 bits. In the bottom figure, each character uses 2 byte or 16 bits. The second byte holds a
\0
character. Casting will not magically add that character."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Simply casting an 8-bit type to a 16-bit type is not going to produce the expected result. Consider:
8 8 8 8 8
╓─╥─╥─╥─╥─╖
║H║e║l║l║o║
╙─╨─╨─╨─╨─╜16 16 16 16 16
╓─┬─╥─┬─╥─┬─╥─┬─╥─┬─╖
║H│0║e│0║l│0║l│0║o│0║
╙─┴─╨─┴─╨─┴─╨─┴─╨─┴─╜In the top figure, each character uses 1 byte or 8 bits. In the bottom figure, each character uses 2 byte or 16 bits. The second byte holds a
\0
character. Casting will not magically add that character."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
Nice illustration, David. But the Unicode string in the figure seem to have been terminated by a single '0' character, whereas it should be 2 '0's. :)
It is a crappy thing, but it's life -^ Carlo Pallini
-
Nice illustration, David. But the Unicode string in the figure seem to have been terminated by a single '0' character, whereas it should be 2 '0's. :)
It is a crappy thing, but it's life -^ Carlo Pallini
Rajesh R Subramanian wrote:
But the Unicode string in the figure seem to have been terminated...
Actually, neither is terminated. I didn't deem that important.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Rajesh R Subramanian wrote:
But the Unicode string in the figure seem to have been terminated...
Actually, neither is terminated. I didn't deem that important.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
Oh yeah. I only looked at the Unicode text. :-O And yes, the illustration serves the purpose well.
It is a crappy thing, but it's life -^ Carlo Pallini