Newbie: can't manage some types...

Alfred Newman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Oct 31 07:09:17 PDT 2016


On Monday, 31 October 2016 at 11:44:25 UTC, Cleverson Casarin 
Uliana wrote:
> Hello all, I'm trying to do two tasks which involves some type 
> conversion, and I'm having dificulties, probably because I 
> haven't yet understood well how such types works.
>
> First, I wanted to convert UTF-8 strings to ansi, so it displays
> correctly at the Windows command prompt. One function to do 
> that is
> "toMBSz" from std.windows.charset, which is declared as follows:
>     const(char)* toMBSz(in char[] s, uint codePage = 0);
>
> So, after some struggling, I managed to write the following 
> code:
>
> import std.stdio;
> import std.windows.charset;
> void main() {
> string s = "Testando acentuação";
> auto r = toMBSz (s, 1);
> writeln (r[0..19]);
> }
>
> Although the above code works, I have an impression that it 
> could be more elegant and concise, but don't know how to 
> improve it...
>
> I'd also like to play a sound file, so tried to use the 
> function "PlaySound" from core.sys.windows.mmsystem, declared 
> as follows: BOOL PlaySoundW(LPCWSTR, HMODULE, DWORD);
>
> According to some error messages I receive, the first parameter 
> is of
> type const(char)*, which corresponds to the sound file name, 
> but I
> just can't figure out how to convert the string (or a char 
> array) to
> that type... Could you please give some snippet example? The 
> closest I
> have come, which doesn't compile at all, is as follows:
> import core.sys.windows.mmsystem;
>
> void main() {
> char[] f = "C:/base/portavox/som/_fon102.wav".dup;
> const(wchar)* arq = cast(const(wchar)*)&f;
> void* nulo;
> uint SND_FILENAME;
> PlaySound (arq, nulo, SND_FILENAME);
> }
>
> Thank you,
> Cleverson

Cleverson,

About your question related to "Testando acentuação", and 
assuming you're using Windows, you can also do the following:

import std.stdio, std.string;

//A Windows function to set the code page of the console output
extern (Windows): private int SetConsoleOutputCP(uint codepage);

void main()
{
     SetConsoleOutputCP(65001);
     string s = "Testando acentuação";
     writeln("Output: ", s.toUpper());
}

Cheers



More information about the Digitalmars-d-learn mailing list