Link to home
Start Free TrialLog in
Avatar of sandip132
sandip132Flag for Japan

asked on

Add/Convert Char[] to wchar[]

I 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.
char outStr[6];
wchar_t SASI_DATAWCHR[1000]; 
int intCnter=0;
 
for (wstring::iterator i = SASI_DATAW.begin(); i != SASI_DATAW.end(); ++i) { 
	sprintf(outStr,"0x%x",(int) *i);
	SASI_DATAWCHR[intCnter]=(wchar_t)outStr;   // << Not adding actual values.
	intCnter++;
       //mbstowcs((wchar_t*)SASI_DATAWCHR[intCnter],outStr[0],10)  // NOT WORKING
}

Open in new window

SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:


char outStr[6] = {'a', 'b', 'e', 'l', '2', '0'};
wchar_t SASI_DATAWCHR[1000]; 
 
for(int i = 0; i < 6; i++)
{   
    SASI_DATAWCHR[i]=(wchar_t) outStr[i];
}  

Open in new window

SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> char c="A" that gives compilation error
It will do "A" is not of type char but of type char const *.

>> It seems that use of mbstowcs
that is for converting strings and not chars... the asker wants to convert chars.
>> It will do "A" is not of type char but of type char const *.
Or, more correctly it is of type char const[2]  :)
Avatar of sandip132

ASKER

@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",(int) *i);" everything works fine, I am getting the string in expected format, now I want to add these '0x30a2' chars in wchar_t array.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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[intCnter]=(wchar_t)outStr
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 don't see a substantial difference in above sentences, different way to say the same thing
They are completely different functions, with completely different prototypes that perform completely different tasks.
there is no substantial diffenence
no between a function and another
but between what you and me are saying
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.
#include <string>
#include <sstream>
 
int main()
{
	char outStr[6] = {0};
	char SASI_DATAWCHR[1000] = {0}; 
	int intCnter=0;
 
	std::wstring SASI_DATAW = L"ABCD";
 
	size_t idx = 0;
	for (std::wstring::iterator i = SASI_DATAW.begin(); i != SASI_DATAW.end(); ++i)
	{
		sprintf(outStr,"0x%04x",(int) *i);
		memcpy(&SASI_DATAWCHR[idx], outStr, sizeof(char) * 6);
		idx += 6;
	}
}

Open in new window

@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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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]=(wchar_t)outStr;

is it safe to add
SASI_DATAWCHR[intCnter]=(int) *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.
Please answer my question.  :-)
Is that the way the data looks when you get it from the database?
 
Oops! Sorry for that, I misunderstood the question.

The input data that I get from database is pure Japanese characters, not in the form of 0x30a2...
I save it in wstring SASI_DATAW.

ok! Here is what working.









wchar_t SASI_DATAWCHR[1000]; 
int intCnter=0;
for (wstring::iterator i = SASI_DATAW.begin(); i != SASI_DATAW.end(); ++i) { 
       SASI_DATAWCHR[intCnter]=(int) *i);
       intCnter++;
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Incidentally, I see Dan has made an additional observation regarding the data format. I don't believe either of us can help you further without clear clarification of the data formats and the API prototyping, otherwise we're just guessing.
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.
https://www.experts-exchange.com/questions/24320223/Add-Convert-Char-to-wchar.html?anchorAnswerId=24144579#a24144579
>>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?

You was right. There is no need for any conversion into char.
>> 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?
Thanks all.

Sorry for late closing.

Points given for helpful tips also.