From: tim todd on
From my previous thread of "cast int to char"
I'm sure my method can be improved! What would be the best way to do
the following:

// New file: write Unicode marker
unsigned char buf[10];
buf[0] = static_cast<int>(254);
buf[1] = static_cast<int>(255);
m_file.Write(&buf[0],2);

{ 254 and 255 are of type int, even without static_cast<int>. -mod/sk }

Or are you happy with it?
I am putting in the first 2 characters so my file is recognized as
being 16 bit characters!
Thanks
Todd.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Maxim Yegorushkin on
On Jun 20, 8:41 pm, tim todd <timza1...(a)yahoo.com> wrote:
> From my previous thread of "cast int to char"
> I'm sure my method can be improved! What would be the best way to do
> the following:
>
> // New file: write Unicode marker
> unsigned char buf[10];
> buf[0] = static_cast<int>(254);
> buf[1] = static_cast<int>(255);
> m_file.Write(&buf[0],2);
>
> { 254 and 255 are of type int, even without static_cast<int>. -mod/sk }
>
> Or are you happy with it?
> I am putting in the first 2 characters so my file is recognized as
> being 16 bit characters!

Why not:

m_file.write("\xFE\xFF", 2);

?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Bo Persson on
tim todd wrote:
> From my previous thread of "cast int to char"
> I'm sure my method can be improved! What would be the best way to do
> the following:
>
> // New file: write Unicode marker
> unsigned char buf[10];
> buf[0] = static_cast<int>(254);
> buf[1] = static_cast<int>(255);
> m_file.Write(&buf[0],2);
>
> { 254 and 255 are of type int, even without static_cast<int>.
> -mod/sk }
>
> Or are you happy with it?
> I am putting in the first 2 characters so my file is recognized as
> being 16 bit characters!

Yoy might ask yourself: If the file is supposed to be recognized as
being 16 bit characters, why are you writing it as 8 bit characters?


Bo Persson



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]