By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

What is PeekChar() ???

P: n/a
I was trying to use BinaryReader.PeekChar to "peek" the next byte, that works
fine unless the byte value's above 127 .... is there anything I can do abouit
it?

thnx,
mikolas

Nov 16 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
Don't you have PeekByte? PeekChar will read a unicode char, this can be
more than 1 byte.

Kind regards

Alexander
"Mikolas" <Mi*****@discussions.microsoft.com> wrote in message
news:85**********************************@microsof t.com...
I was trying to use BinaryReader.PeekChar to "peek" the next byte, that
works
fine unless the byte value's above 127 .... is there anything I can do
abouit
it?

thnx,
mikolas

Nov 16 '05 #2

P: n/a
Hi Mikolas,

The default constructor for BinaryReader is set to use UTF8 encoding which
will be ascii for the first 127 byte characters but will mean unicode for
characters above 127 (ie reading two bytes etc).

Use the encoding used in the text. This will create a reader with the
default windows page on your machine.
BinaryReader br = new BinaryReader(stream, Encoding.Default);

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3

P: n/a


"Morten Wennevik" wrote:
Hi Mikolas,

The default constructor for BinaryReader is set to use UTF8 encoding which
will be ascii for the first 127 byte characters but will mean unicode for
characters above 127 (ie reading two bytes etc).

Use the encoding used in the text. This will create a reader with the
default windows page on your machine.
BinaryReader br = new BinaryReader(stream, Encoding.Default);

I admit I don't know much about all those different encodings, but .Default
sounds a bit dangerous since I'm writing code that should be running on
different devices. So should I use UnicodeEncoding and then mask the desired
byte?

thanks,
mikolas
--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #4

P: n/a
On Mon, 6 Dec 2004 04:59:04 -0800, Mikolas
<Mi*****@discussions.microsoft.com> wrote:


"Morten Wennevik" wrote:
Hi Mikolas,

The default constructor for BinaryReader is set to use UTF8 encoding
which
will be ascii for the first 127 byte characters but will mean unicode
for
characters above 127 (ie reading two bytes etc).

Use the encoding used in the text. This will create a reader with the
default windows page on your machine.
BinaryReader br = new BinaryReader(stream, Encoding.Default);


I admit I don't know much about all those different encodings, but
.Default
sounds a bit dangerous since I'm writing code that should be running on
different devices. So should I use UnicodeEncoding and then mask the
desired
byte?

thanks,
mikolas
--
Happy Coding!
Morten Wennevik [C# MVP]


Hard to say. If the text could be in different encodings you would
probably benefit from using Unicode or UTF8. If you only need the
standard default extended English ascii you could get that specific
encoding with
Encoding e = Encoding.GetEncoding("Windows-1252");
Encoding e = Encoding.GetEncoding("ISO-8859-1");
I believe the code table are the same for these two code pages. There is
also "US ASCII" listed under western code pages.

You can read more about Unicode and UTF8 on this page
http://www.pobox.com/~skeet/csharp/unicode.html

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5

P: n/a
Morten Wennevik <Mo************@hotmail.com> wrote:
Hard to say. If the text could be in different encodings you would
probably benefit from using Unicode or UTF8. If you only need the
standard default extended English ascii you could get that specific
encoding with
Encoding e = Encoding.GetEncoding("Windows-1252");
Encoding e = Encoding.GetEncoding("ISO-8859-1");
I believe the code table are the same for these two code pages.


No they're not - they differ between 128 and 140.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

P: n/a
On Tue, 7 Dec 2004 11:25:45 -0000, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
Morten Wennevik <Mo************@hotmail.com> wrote:
Hard to say. If the text could be in different encodings you would
probably benefit from using Unicode or UTF8. If you only need the
standard default extended English ascii you could get that specific
encoding with
Encoding e = Encoding.GetEncoding("Windows-1252");
Encoding e = Encoding.GetEncoding("ISO-8859-1");
I believe the code table are the same for these two code pages.


No they're not - they differ between 128 and 140.


I see. Which is considered the standard western codepage? Or is the
standard something that differs between europe and usa?

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #7

P: n/a
Morten Wennevik <Mo************@hotmail.com> wrote:
No they're not - they differ between 128 and 140.


I see. Which is considered the standard western codepage? Or is the
standard something that differs between europe and usa?


Encoding.Default is Windows 1252, I believe.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

P: n/a
On Tue, 7 Dec 2004 13:25:45 -0000, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
Morten Wennevik <Mo************@hotmail.com> wrote:
> No they're not - they differ between 128 and 140.


I see. Which is considered the standard western codepage? Or is the
standard something that differs between europe and usa?


Encoding.Default is Windows 1252, I believe.


Well, on this system, Norwegian Windows 98, Encoding.Default is
iso-8859-1. I believe Encoding.Default varies with whatever codepage the
OS uses.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #9

P: n/a
Morten Wennevik <Mo************@hotmail.com> wrote:
On Tue, 7 Dec 2004 13:25:45 -0000, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
Morten Wennevik <Mo************@hotmail.com> wrote:
> No they're not - they differ between 128 and 140.

I see. Which is considered the standard western codepage? Or is the
standard something that differs between europe and usa?
Encoding.Default is Windows 1252, I believe.


Well, on this system, Norwegian Windows 98, Encoding.Default is
iso-8859-1.


Interesting. I thought 1252 was the default throughout Western
Europe... Which property did you use to display iso-8859-1 though? The
BodyName property for 1252 returns iso-8859-1, but the others return
Windows-1252.
I believe Encoding.Default varies with whatever codepage the
OS uses.


It certainly does.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

P: n/a
On Tue, 7 Dec 2004 15:17:49 -0000, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
Morten Wennevik <Mo************@hotmail.com> wrote:
On Tue, 7 Dec 2004 13:25:45 -0000, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
> Morten Wennevik <Mo************@hotmail.com> wrote:
>> > No they're not - they differ between 128 and 140.
>>
>> I see. Which is considered the standard western codepage? Or is the
>> standard something that differs between europe and usa?
>
> Encoding.Default is Windows 1252, I believe.
>


Well, on this system, Norwegian Windows 98, Encoding.Default is
iso-8859-1.


Interesting. I thought 1252 was the default throughout Western
Europe... Which property did you use to display iso-8859-1 though? The
BodyName property for 1252 returns iso-8859-1, but the others return
Windows-1252.
I believe Encoding.Default varies with whatever codepage the
OS uses.


It certainly does.


Interesting, I only checked the BodyName and indeed the other values are
1252/windows 1252, but if BodyName reports the codename for mail agents
one would think the two are identical. Odd.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #11

P: n/a
Well, this is is strange. Obviously the encoding thing's pretty complex so do
I really have to deal with all this? All I want is to peek one byte ahead ...
I remember like 10 years ago when I started learning programming things like
that were possible(even here in th Czech rep.) - what is it that happened in
the meantime?

Many Thanks,
mikolas

"Morten Wennevik" wrote:
On Tue, 7 Dec 2004 15:17:49 -0000, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
Morten Wennevik <Mo************@hotmail.com> wrote:
On Tue, 7 Dec 2004 13:25:45 -0000, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:

> Morten Wennevik <Mo************@hotmail.com> wrote:
>> > No they're not - they differ between 128 and 140.
>>
>> I see. Which is considered the standard western codepage? Or is the
>> standard something that differs between europe and usa?
>
> Encoding.Default is Windows 1252, I believe.
>

Well, on this system, Norwegian Windows 98, Encoding.Default is
iso-8859-1.


Interesting. I thought 1252 was the default throughout Western
Europe... Which property did you use to display iso-8859-1 though? The
BodyName property for 1252 returns iso-8859-1, but the others return
Windows-1252.
I believe Encoding.Default varies with whatever codepage the
OS uses.


It certainly does.


Interesting, I only checked the BodyName and indeed the other values are
1252/windows 1252, but if BodyName reports the codename for mail agents
one would think the two are identical. Odd.

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #12

P: n/a


"Alexander Muylaert" wrote:
Don't you have PeekByte?


this would certainly be great and I wouldn't have to spend 1hour debugging
but I haven't seen it anywhere...

mikolas
Nov 16 '05 #13

P: n/a
On Wed, 8 Dec 2004 04:31:01 -0800, Mikolas
<Mi*****@discussions.microsoft.com> wrote:
Well, this is is strange. Obviously the encoding thing's pretty complex
so do
I really have to deal with all this? All I want is to peek one byte
ahead ...
I remember like 10 years ago when I started learning programming things
like
that were possible(even here in th Czech rep.) - what is it that
happened in
the meantime?

Many Thanks,
mikolas


Well, if you aren't going to read the bytes as characters just use the
overloaded
constructor for the BinaryReader.

new BinaryReader(stream, Encoding.Default);

This should force a character to be considered 8 bits in size so PeekChar
should return a single byte.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #14

P: n/a


"Morten Wennevik" wrote:
On Wed, 8 Dec 2004 04:31:01 -0800, Mikolas
<Mi*****@discussions.microsoft.com> wrote:
Well, this is is strange. Obviously the encoding thing's pretty complex
so do
I really have to deal with all this? All I want is to peek one byte
ahead ...
I remember like 10 years ago when I started learning programming things
like
that were possible(even here in th Czech rep.) - what is it that
happened in
the meantime?

Many Thanks,
mikolas


Well, if you aren't going to read the bytes as characters just use the
overloaded
constructor for the BinaryReader.

new BinaryReader(stream, Encoding.Default);

This should force a character to be considered 8 bits in size so PeekChar
should return a single byte.


I'm sorry but I still don't get it. How do you know that? My SW should also
be running on WinCE and as far as I know there's only unicode.
This is just so weird - why there's PeekChar and not PeekByte, one would
expect to use binary reader for binary reading....

thanks,
mikolas

Nov 16 '05 #15

P: n/a
Mikolas <Mi*****@discussions.microsoft.com> wrote:
This should force a character to be considered 8 bits in size so PeekChar
should return a single byte.


I'm sorry but I still don't get it. How do you know that? My SW should also
be running on WinCE and as far as I know there's only unicode.
This is just so weird - why there's PeekChar and not PeekByte, one would
expect to use binary reader for binary reading....


I know, it's very strange that there isn't a PeekByte :(

Rather than trust to Encoding.Default, you could use Encoding.ASCII or
Encoding.GetEncoding(28591), the latter of which is ISO-8859-1.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #16

P: n/a
Mikolas,

Hm, I mistakingly though Encoding.Default always would use an 8-bit
encoding, but a simple test proved me wrong. I would use
Encoding.GetEncoding("ISO-8859-1") or any of the encodings you know are
8-bit. You should not however use Encoding.ASCII as that is only 7-bit
and will truncate any values above 127.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #17

P: n/a
Morten Wennevik <Mo************@hotmail.com> wrote:
Hm, I mistakingly though Encoding.Default always would use an 8-bit
encoding, but a simple test proved me wrong. I would use
Encoding.GetEncoding("ISO-8859-1") or any of the encodings you know are
8-bit. You should not however use Encoding.ASCII as that is only 7-bit
and will truncate any values above 127.


I don't see why that causes an issue if the encoding is only being used
for PeekChar, to detect the end of the file. If it's *not* only being
used for PeekChar, then the encoding of the actual text in the file is
what should be used, whether that's ASCII, ISO-8859-1 or whatever.
Detecting the end of the file might then become significantly more
difficult if the desired encoding is multi-byte.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #18

P: n/a
On Fri, 10 Dec 2004 13:13:10 -0000, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
Morten Wennevik <Mo************@hotmail.com> wrote:
Hm, I mistakingly though Encoding.Default always would use an 8-bit
encoding, but a simple test proved me wrong. I would use
Encoding.GetEncoding("ISO-8859-1") or any of the encodings you know are
8-bit. You should not however use Encoding.ASCII as that is only 7-bit
and will truncate any values above 127.


I don't see why that causes an issue if the encoding is only being used
for PeekChar, to detect the end of the file. If it's *not* only being
used for PeekChar, then the encoding of the actual text in the file is
what should be used, whether that's ASCII, ISO-8859-1 or whatever.
Detecting the end of the file might then become significantly more
difficult if the desired encoding is multi-byte.


Well, the original message didn't say anything about only reading the end
of file, nor using it for reading text. PeekChar with Encoding.ASCII will
read 7 bits so a byte value of say 130 would be returned as 63, ie not
what you would want.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #19

P: n/a
Morten Wennevik <Mo************@hotmail.com> wrote:
Well, the original message didn't say anything about only reading the end
of file, nor using it for reading text. PeekChar with Encoding.ASCII will
read 7 bits so a byte value of say 130 would be returned as 63, ie not
what you would want.


Oops - I was getting confused with a different thread asking why there
isn't a PeekByte method...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.