Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
As I am a novice of C and C++,

I am coding of MFC in VC++ using UNICODE.

It is simple for you but not easy for me, the problem is difficult as follows:

int x = 19;

char y[2];

I want to move the value x to y like as '0013' // 00 13 = hex is decimal 19

So I tried to

CString str = _T("");

str.Format(_T("%d"), x);

WideCharToMultiByte(CP_ACP, 0, s.GetBuffer(), str.GetLength(), y, sizeof(y), NULL, NULL);

After doing it, but as I debugging y, y has '0x49' and '0x57' (0x49='1', 0x57='9')

I want to get the 2 byte char y : y[0] = 0x00, y[1] = 0x13

In the world of C++ with unicode, Conversion is very difficult for me

Please help me.

What I have tried:

More one day wasted for this problem.
Posted
Updated 14-May-16 5:37am
Comments
Sergey Alexandrovich Kryukov 14-May-16 10:52am    
There is no such thing. Integer cannot be decimal or hexadecimal. It's always "binary". So, there is no "convert".
Only a string can represent decimal, hexadecimal or any other numeric notation. What you need is string formatting, not "conversion".
—SA
PJ Arends 14-May-16 11:42am    
Really ?!?

The OP asked a simple question that we all as beginners would have asked and you give him an English lesson on the difference between the words "conversion" and "formatting". You can clearly see from his use of the Format function and the char array that he was on the correct path to solving his problem.

You are obviously a very knowledgeable guy but sometimes you can be pretty dense too.
Sergey Alexandrovich Kryukov 14-May-16 12:00pm    
Yes, really.
You have to understand that this is not about "density";
1) my comment is not about language, this is about the misconception, too;
2) if you think that language, more exactly, terminology, is not important, you are wrong.
In practice, bad terminology will only delay learning, getting help, all that stuff. Why not correct a person from the very beginning? Yes, I can see that "correct path", but this is not an excuse for not correcting.
So, I think that my help is not less useful than yours; I up-voted your solution just because it looks correct.
—SA
PJ Arends 14-May-16 12:25pm    
Ok, I can understand your point now that you have explained it. I probably would not have reacted the way I did if you would have made your point clearer to begin with.

I just hate to see newcomers to this site get down voted and their questions not answered for silly reasons like "terminology", especially as their first language may not be English.
Sergey Alexandrovich Kryukov 14-May-16 13:54pm    
I just don't think that "there is no such thing" and "what you need is" require explanations of the motivation or excuses. There is nothing rude in that; it's only helpful. You are talking of "giving a lesson" as if it was something bad, and this an actual problem. If someone gives me a lesson, I either say "thank you" or argue if I find it incorrect, or both.

Thank you for sharing your views.
—SA

1 solution

Use the %X type specifier in your CString::Format call. Do not bother with the WideCharToMultiByte stuff if you are working in UNICODE, just keep every thing in UNICODE.

C++
CString str;
str.Format(L"%X", x);


Lookup CString::Format in MSDN
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-May-16 11:55am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900