Chinese string to hex values

Hi,

I want to display the hex values for a string of chinese characters in the console.

I'm using visual studio 2010 c++ in windows 7.

So far I have managed to display the hex values for single chinese characters e.g.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main ()
{
	wchar_t chi = L'您';
	printf("%X",chi);
}


which displays the correct hex value of the chinese character (60A8).

However, I need to be able to display multiple hex values corresponding to their chinese characters.
For example, the chinese string "你好世界" should display hex "60A8 597D 4E16 754C".

I've tried looking into using wstring, but I come out with one hex value that has no relevance. I don't know if it's something I'm doing wrong or if I'm just going the wrong way about it.

Any insight/help would be greatly appreciated!

Thanks in advance,

AD
You can apply the same output method to every character in your string:

1
2
3
4
5
6
7
8
9
#include <cstdio>
#include <string>
using namespace std;
int main ()
{
    wstring s = L"你好世界";
    for(wstring::size_type n = 0; n < s.size(); ++n)
        printf("%X ", s[n]);
}


demo: https://ideone.com/1CKgc

It's hard to say if your way was "the wrong way" given no source code that failed.
Thanks a lot Cubbi! That's exactly what I wanted.

Thanks for your help :)
Topic archived. No new replies allowed.