www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - CharToOemW

reply "Simon Buchan" <currently no.where> writes:
How is this function supposed to be used?
I can only get something like:
┼┼__>_______┼___>___┼┼__>_______________┼___>___┼┼______________┼___________4___
___4_____________┼┼┼┼____________________________┼┼┼┼┼_________________________┼
┼┼┼┼___5____________5___________┼┼┼┼_______________________5____┼┼┼┼┼___________

with

wchar[] temp = cast(wchar[])<Function returning char[]>;
temp ~= 0;
string temp2;
temp2.length = temp.length;
CharToOemW(temp.ptr, temp2.ptr);
printf(temp2.ptr);

I had to define
extern(Windows) BOOL CharToOemW(wchar*, LPSTR);

is this correct? (OK, it SHOULD be LPCTSTR, not wchar*, but they're
all void* to the function)

I would really like to be able to see semi-UTF'ish strings on my cruddy
Windows console.

-- 
"Unhappy Microsoft customers have a funny way of becoming Linux,
Salesforce.com and Oracle customers." - www.microsoft-watch.com:
"The Year in Review: Microsoft Opens Up"
Dec 06 2004
next sibling parent reply =?UTF-8?B?QW5kZXJzIEYgQmrDtnJrbHVuZA==?= <afb algonet.se> writes:
Simon Buchan wrote:

 wchar[] temp = cast(wchar[])<Function returning char[]>;

You can't cast char[] to wchar[], only with string literals! With dynamic strings, you *need* to use std.utf.toUTF16()... This program:
 import std.stdio;
 import std.utf;
 
 void main()
 {
   char[] s = "hello!";

   wchar[] a = cast(wchar[]) "hello!";
   wchar[] b = cast(wchar[]) s;
   wchar[] c = toUTF16(s);
 
   writefln(a);
   writefln(b);
   writefln(c);
 }

Prints something like: (i.e. with a modern UTF-8 console)
 hello!
 桥汬漡
 hello!

And if your char[] has an odd number of bytes, it fails:
 Error: array cast misalignment

--anders PS. Hope that middle line isn't offensive to anyone... Maybe it means "Go Stick Your Head in a Pig!" ? :)
Dec 07 2004
parent "Simon Buchan" <currently no.where> writes:
On Tue, 07 Dec 2004 11:02:31 +0100, Anders F Björklund <afb algonet.se>  
wrote:

 Simon Buchan wrote:

 wchar[] temp = cast(wchar[])<Function returning char[]>;

You can't cast char[] to wchar[], only with string literals! With dynamic strings, you *need* to use std.utf.toUTF16()... This program:
 import std.stdio;
 import std.utf;
  void main()
 {
   char[] s = "hello!";

   wchar[] a = cast(wchar[]) "hello!";
   wchar[] b = cast(wchar[]) s;
   wchar[] c = toUTF16(s);
    writefln(a);
   writefln(b);
   writefln(c);
 }

Prints something like: (i.e. with a modern UTF-8 console)
 hello!
 桥汬漡
 hello!

And if your char[] has an odd number of bytes, it fails:
 Error: array cast misalignment

--anders PS. Hope that middle line isn't offensive to anyone... Maybe it means "Go Stick Your Head in a Pig!" ? :)

Duh! I should have seen that! (slaps forehead) Thanks for helping us morons :D Is there any way to convert UTF-8 to a codepage? (other than that, of course) I suppose you could find a UTF to ASCII converter and use CharToOemA... -- "Unhappy Microsoft customers have a funny way of becoming Linux, Salesforce.com and Oracle customers." - www.microsoft-watch.com: "The Year in Review: Microsoft Opens Up" -- "I plan on at least one critical patch every month, and I haven't been disappointed." - Adam Hansen, manager of security at Sonnenschein Nath & Rosenthal LLP (Quote from http://www.eweek.com/article2/0,1759,1736104,00.asp) -- "It's been a challenge to "reteach or retrain" Web users to pay for content, said Pizey" -Wired website: "The Incredible Shrinking Comic"
Dec 09 2004
prev sibling parent Roberto Mariottini <Roberto_member pathlink.com> writes:
In article <opsimkvtlujccy7t simon.mshome.net>, Simon Buchan says...
How is this function supposed to be used?

import std.stdio; import std.c.stdio; import std.c.windows.windows; extern (Windows) { export BOOL CharToOemW( LPCWSTR lpszSrc, // string to translate LPSTR lpszDst // translated string ); } int main() { wchar[] mess = ""; char[] OEMmess = new char[mess.length]; CharToOemW(mess, OEMmess); puts(OEMmess); return 0; } Ciao
Dec 09 2004