Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Unicode
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 30-Nov-11 4:24am
Comments
Richard MacCutchan at 30-Nov-11 9:30am
   
What happens when you try from the first actual WCHAR value?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Same thing
 
errno_t er = wcstombs_s(&converted, pL->szName, 64, (const wchar_t *)&pLI->szLayerName[2], _TRUNCATE);
error EILSEQ
  Permalink  
v2
Comments
Richard MacCutchan at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Richard MacCutchan at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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...
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 329
2 CPallini 270
3 Afzaal Ahmad Zeeshan 204
4 DamithSL 194
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 30 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100