can you show the declaration of SASI_DATAW?
It looks to me that you want to enter a pointer to the char array, or do you mean to copy the actual values one by one? There are six of them, this works for me:
Main Topics
Browse All TopicsI am getting char string like 0x30a2 0x30c6 0x30a3 0x30d3 0x30c6 in below for loop.
I want to add these string one by one in into the wchar_t array.
like this :
SASI_DATAWCHR[0] = 0x30a2
SASI_DATAWCHR[1] = 0x30c6
SASI_DATAWCHR[2] = 0x30a3
SASI_DATAWCHR[3] = 0x30d3
SASI_DATAWCHR[4] = 0x30c6
:
:
:
Tried mbstowcs, but copying some different value into array.
Is there any other way?
Thanks in advance.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I'm a little confused. You are iterating through a wstring, which generally has a value_type of wchar_t and the array you are looking to populate also has a element type of wchar_t so where exactly does the char conversion come in? Why don't you just copy the string directly to the array using std::copy()?
http://www.sgi.com/tech/st
std::copy(SSDATAW.begin(),
Hint: Pointers can be used as iterators.
Regarding the idea of casting from char to wchar_t, it's not safe to do this because there is no guarantee that the char type directly maps to a wchar_t type of the same value. It all depends upon the character encoding. If it's simple ASCII then it'll probably work ok but if it's UTF8 encoded data (which is a multi-byte encoding) then this isn't going to work as expected.
The correct and safe way to convert a narrow to wide character and a wide to narrow narrow is to use mbtowc and wctomb respectively. The links below give full details on how to use these.
http://www.cplusplus.com/r
http://www.cplusplus.com/r
If this doesn't help please explain why and we can take it from there.
hi,
you cannot copy a printable rappresentation of a binary value (also if it represent a char)
to the binary variable:
char c=65
equivalent to
char c='A'
but not equal to
char c="A" that gives compilation error
neither c=(char)"A" that gives no error (maby), but means:
put address of automatic area "A" into a char: compiler takes low or high 8bits of pointer
"A" (2,4,8 bytes) and put in c var: difficulty it will be 65 seeing with debug.
It seems that use of mbstowcs
http://msdn.microsoft.com/
is a little different.
bye
vic
@abel:
>>wchar_t wc = (wchar_t) c;
doesnot convert the value as it is. It copies some integer value and not 0x30a2 etc..
SASI_DATAW is a wstring.
Please refer my expected output in my question.
@evilrix:
The char is to hold the Hex Unicode value of the characters in wstring. I need to convert it to Hex Unicode and then copy it in wchar_t array.
The both options mbtowc and wctomb not working. No function copies the actual data in wchar_t array.
@dolomiti:
tried your link too.
@All
I am doing this to pass the Hex Unicode wchar_t array to a third party API.
Upto this statement "sprintf(outStr,"0x%x",(in
>> The char is to hold the Hex Unicode value of the characters in wstring
Ok, I think you are getting yourself confused a little. What you are doing is converting the numeric value (for example) 0xFFEE to the string "0xFFEE". This is therefore a character array of 7 bytes (including the NULL terminator). You can then just store this in a wchar_t since that is a 16 bit (on Windows) data type, where as "0xFFEE" is a string. If you want to be storing the hex numeric value then just copy as I suggested above because putting 0xFFEE into a wchar_t as a numeric value is the same as copying a wchar_t holding that as a UTF16 char into another wchar_t. The bit pattern is the same because the values are the same. Does that make sense?
If you want the wchar_t value to be a string then you will need to copy 6 bytes (assuming a char is a byte) from outStr into the SASI_DATAWCHR array.
hi,
probably competition, than collaboration, clouds posts interpretations.
me>>>It seems that use of mbstowcs ... is a little different
you>> that is for converting strings and not chars
I don't see a substantial difference in above sentences, different way to say the same thing
asker>>SASI_DATAWCHR[intCnte
me>>>char c="A" as I wrote. is a no-sense in C
About this, I looked for another known way to present an incorrect operation
I suspect you are trying to do something like below, although that might not be completely correct since I am still a bit fuzzy on what it is you're trying to achieve and why. It seems overly convoluted to me that you need to convert wide data into a string of hexadecimal so I'm not convinced this is what actually needs to be done.
So, how about we change this around a little. Rather than telling us what you are trying to do how about you explain your problem in terms of what you are trying to achieve. Tell us more about this 3rd party API. The function prototypes, describe the parameters, what format of data should be going in and what do they represent.
@dolomiti, maybe there is just a language barrior problem here. I am guessing you are not a native English speaker, so maybe I misunderstood you, if so I am sorry. The point; however, is that if I have misunderstood then so too will the Asker (probably). I am just suggesting you make sure your posts are unambiguous so as not to confuse the issue and, thus, make life harder for both the asker and the other experts.
There is, indeed considerable confusion as to the desired output.
Based in an earliier question (http:CPP/Q_24312267.html)
"0x65ed, 0x8466, 0x82a6, 0x9bf5, 0x6893"
If so, then evilrix's last post will do as desired if you just ass the comma and the space:
sprintf( outStr, "0x%04x, ", (int) *i ); // added comma space
memcpy( &SASI_DATAWCHR[idx], outStr, sizeof(char) * 8 ); // changed 6 to 8
idx = 8; // changed 6 to 8
Aslo, you will need to trim the final comma and space and apply the terminating NUL to the end when finished, like so:
SASI_DATAWCHR[ idx - 2 ]= 0;
I am Sorry. My question is confusing.
@evilrix:
Your example works fine, but its a char array. (char SASI_DATAWCHR[1000] = {0};)
@Dan, evilrix, dolomiti,
I am getting Japanese character string from DB and I need to convert it into following formatted array .
The array I need to pass to the third party API is like below:
wchar_t txt2U[] = {0x30a2, 0x30c6, 0x30a3, 0x30d3, 0x30c6, 0x30a3, 0x30fb, 0x30ed, 0x30b0, 0x306b, 0x30b9, 0x30a2, 0x3055, 0x308c, 0x305f, 0x30a4, 0x0};
I have raised the points, after understanding the complexity of the question.
I think I understand now, but just to be sure:
The data comes to you as a character string of text that looks like:
0x30a2 0x30c6 0x30a3 0x30d3
^^ --- two spaes?
That string is 32 bytes long. A set of 6 characters, two spaces, six more characters...
digit zero, letter ex, digit three, digit zero, letter aye, digit two, space, space, digit zero, letter ex, etc...
Is that correct?
Dan,
Thank you very much for your response. I am getting that data in char outStr[6] in each iteration of 'for' loop. I am afraid even if we concate it with " ," , it will be difficult to pass it as wchar_t array.
I found that it's unsafe to add the hex values in char.
Instead of adding SASI_DATAWCHR[intCnter]=(w
is it safe to add
SASI_DATAWCHR[intCnter]=(i
directly?
I guess character representation of (int) *i and "0x%x",(int) *i would be same?
Let me give a try! Your suggestions are welcome.
>> The array I need to pass to the third party API is like below:
>> wchar_t txt2U[] = {0x30a2, 0x30c6, 0x30a3, 0x30d3, 0x30c6, 0x30a3, 0x30fb, 0x30ed, 0x30b0, 0x306b, 0x30b9, 0x30a2, 0x3055, 0x308c, 0x305f, 0x30a4, 0x0};
The example you've shown above is assigning each element of the wchar_t array with a NUMERIC hex value, which is 1 wide character. What you are describing as your requirement is to encode these hex values into their string format and then concatenate the strings into an array of a wide chars. I honestly think you are just getting yourself confused into thinking you need to code up something that isn't necessary because what you are describing makes little sense to me.
If this is the format of the data that the API needs that all you need to is this...
// Assuming it takes a null terminated string
void SomeApiFunc(wchar_t data[]); // Example prototype of API
SomeApiFunc(SASI_DATAW.str
or maybe...
// Assuming it takes data and a length
void SomeApiFunc(wchar_t data[]), size_t len; // Example prototype of API
SomeApiFunc(SASI_DATAW.dat
Out of interest have you actually tried doing this? IF it works then great, if not I asked above for you to provide the prototype of the API function, can you do this please along with a description of the parameter because I think following the current train of thought is just creating confusion since we are (a) all guessing at what you're trying to do and (b) working on the premise that your understanding of the API is correct (please don't take offense to this comment as I don't mean it in a bad way).
>> Your example works fine, but its a char array. (char SASI_DATAWCHR[1000] = {0};)
It wasn't mean to be a solution, I was just trying to understand your problem better. :)
evilrix,
Thanks for your honest efforts and sorry for the confusion again.
I assumed that, the format provided in Que :
SASI_DATAWCHR[0] = 0x30a2
SASI_DATAWCHR[1] = 0x30c6
SASI_DATAWCHR[2] = 0x30a3
SASI_DATAWCHR[3] = 0x30d3
SASI_DATAWCHR[4] = 0x30c6
And the other Array format :
wchar_t txt2U[] = {0x30a2, 0x30c6, 0x30a3, 0x30d3, 0x30c6, 0x30a3, 0x30fb, 0x30ed, 0x30b0, 0x306b, 0x30b9, 0x30a2, 0x3055, 0x308c, 0x305f, 0x30a4, 0x0};
are same. Instead of adding the elements at the time of initialization, my approach was to add the element one by one and pass that array to the API.
As I mentioned above the Third party API only takes the wchar_t array as parameter, there is really nothing to describe anything else. APIFun(const wchar_t * text)
This is the final solution that I found working perfect for the problem.
http://www.experts-exchang
>> I assumed that, the format provided in Que
Yes, your assumption is correct, both assign numeric hex values to the wchar_t elements.
>> This is the final solution
I'm happy we managed to find a working solution for you.
So, just one question from me... I just want to make sure that you understand why the proposed solution works. Obviously, it's nice that it does but my job would be incomplete if you don't understand why. Do you need further clarification or does it now make sense?
Business Accounts
Answer for Membership
by: abelPosted on 2009-04-14 at 06:11:09ID: 24137727
Isn't a simple case what you need? This works for me:
Select allOpen in new window