Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a unicode string that I would like to convert to single byte ascii.

fe ff 00 4d 00 61 00 69 00 6e 00 20 00 42 00 6f 00 61 00 72 00 64 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 þÿ.M.a.i.n. .B.o.a.r.d...........

I tried using this

size_t converted = 0;
errno_t er = wcstombs_s(&converted, pL->szName, 64, (const wchar_t *)pLI->szLayerName, _TRUNCATE);


but it returns an error EILSEQ. I tried a couple of other versions but nothing is working. What am I missing? Is it the BOM (byte order marker) at the beginning?

TIA
Posted
Comments
Richard MacCutchan 30-Nov-11 9:30am    
What happens when you try from the first actual WCHAR value?

Same thing

C++
errno_t er = wcstombs_s(&converted, pL->szName, 64, (const wchar_t *)&pLI->szLayerName[2], _TRUNCATE);

error EILSEQ
 
Share this answer
 
v2
Comments
Richard MacCutchan 30-Nov-11 10:42am    
I just did this using the string you have above, but ignoring the "feff" and it converts fine. Let us see the exact code you are using, including the input string and its content.
That is the code, did you enter the string as a string between "..." or a byte buffer setting each byte value with a hex code?

Someone suggested that I should ignore the BOM but that would mean that the byte order is then Big Endian and that MS defaults to Little Endian so I'm going to swap the bytes and see what happens.

I know if I write the string in notepad and save as unicode the byte order is swapped on all bytes.

The string actually comes from ghostScript processing a PDF file created by CorelDraw, the string (memory dump) is from the debugger
 
Share this answer
 
Comments
Richard MacCutchan 30-Nov-11 11:13am    
The comments above are not very helpful and you still haven't shown what I asked for so I have no idea what you are doing wrong.
Ok, so ignoring the BOM and swapping the bytes did the trick. What a PITA.

Thanks much for pushing me on.

The memory after swapping looks like this

ff fe 4d 00 61 00 69 00 6e 00 20 00 42 00 6f 00 61 00 72 00 64 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ÿþM.a.i.n. .B.o.a.r.d...
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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