-
Mar 30th, 2021, 12:25 AM
#1
∞ in console with Server 2019?
Ok ... so in a command prompt:
...as expected in both Windows 10 and Server 2019
but for some reason when I go (and enter "∞" for the readline):
VB.Net Code:
Dim Data = Console.ReadLine()
Console.WriteLine(Data)
Console.WriteLine("∞")
Data gets set to 8, and both output lines in the console read "8"!
This however does not appear to happen on Windows 10, but does on Server 2019!
Any ideas?
-
Mar 30th, 2021, 06:11 AM
#2
Re: ∞ in console with Server 2019?
Looks like an issue with the text Encoding. '8' is typically the fallback character used by encodings that don't support the '∞' character, but do support fallback.
Try setting the Console.OutputEncoding and probably also the Console.InputEncoding to UTF-8 or UTF-16. I've found UTF-16 to be more reliable; UTF-8 can be a bit quirky, but I'm usually on Windows 7 with .NET Framework 4.6 so YMMV.
-
Mar 31st, 2021, 11:46 AM
#3
Re: ∞ in console with Server 2019?
Originally Posted by i00
Ok ... so in a command prompt:
...as expected in both Windows 10 and Server 2019
but for some reason when I go (and enter "∞" for the readline):
VB.Net Code:
Dim Data = Console.ReadLine()
Console.WriteLine(Data)
Console.WriteLine("∞")
Data gets set to 8, and both output lines in the console read "8"!
This however does not appear to happen on Windows 10, but does on Server 2019!
Any ideas?
So I created a Console application with this
Code:
Console.WriteLine("∞")
and the output was correct.
The encoding for both Out and In is
System.Text.SBCSCodePageEncoding
-
Mar 31st, 2021, 12:47 PM
#4
Re: ∞ in console with Server 2019?
Yeah, several of the single byte encodings have the '∞' character, but many don't.
The 'en-US' culture defaults to Encoding IBM437 for the console, and that has a mapping for '∞'. However 'en-GB' defaults to IBM850 which doesn't have it. I think i00 is from the UK, so that may be the root of the problem.
-
Mar 31st, 2021, 06:54 PM
#5
Re: ∞ in console with Server 2019?
Originally Posted by Inferrd
I think i00 is from the UK, so that may be the root of the problem.
So racist!
-
Apr 1st, 2021, 09:14 AM
#6
Re: ∞ in console with Server 2019?
Originally Posted by Inferrd
I think i00 is from the UK, so that may be the root of the problem.
I am actually in Australia ... so all 8's and ∞'s are always written upside down here :P ...
Server 2019:
{System.Text.SBCSCodePageEncoding}
BodyName: "ibm850"
CodePage: 850
DecoderFallback: {System.Text.InternalDecoderBestFitFallback}
EncoderFallback: {System.Text.InternalEncoderBestFitFallback}
EncodingName: "Western European (DOS)"
HeaderName: "ibm850"
Windows 10:
{System.Text.SBCSCodePageEncoding}
BodyName: "IBM437"
CodePage: 437
DecoderFallback: {System.Text.InternalDecoderBestFitFallback}
EncoderFallback: {System.Text.InternalEncoderBestFitFallback}
EncodingName: "OEM United States"
HeaderName: "IBM437"
Both systems region options appear to be the same??
What should I do to make this support the infinity symbol correctly (also I don't want to change something only to break something else)?
Thanks
EDIT:
I have set it to:
VB.Net Code:
Console.InputEncoding = System.Text.Encoding.Unicode
Console.OutputEncoding = System.Text.Encoding.Unicode
Assume that this won't break anything?
Last edited by i00; Apr 1st, 2021 at 09:20 AM.
-
Apr 1st, 2021, 10:39 AM
#7
Re: ∞ in console with Server 2019?
Originally Posted by i00
I am actually in Australia
You put the queen on your money, you're British.
Originally Posted by i00
EDIT:
I have set it to:
I think you should be fine with that.
-
Apr 5th, 2021, 11:04 PM
#8
Re: ∞ in console with Server 2019?
Originally Posted by dday9
I think you should be fine with that.
Ok...
It seemed to be at first glance ... but now when entering > 126 chrs with Console.ReadLine I need to press enter 2x to get it to register?
Also Console.BufferWidth = 120 ... so probably isn't to do with that
Any ideas?
-
Apr 6th, 2021, 09:36 AM
#9
Re: ∞ in console with Server 2019?
Sorry for the delay in replying. Just back from mandatory cultural sensitivity training
According to the documentation for the Console.ReadLine Method, there's a 256 character limit to the size of the standard input buffer, and that includes the NewLine character(s):
By default, the method reads input from a 256-character input buffer. Because this includes the Environment.NewLine character(s), the method can read lines that contain up to 254 characters. To read longer lines, call the OpenStandardInput(Int32) method.
For some weird reason, when using UTF-16 (which is a fixed width, 2 byte encoding), that translates to 256 bytes, not Chars. So your 127 chrs plus the NewLine would take up the 256 bytes (each UTF-16 Char takes 2 bytes to represent it). Seems odd though. Maybe it's using UTF-32 internally or something. Don't know.
So following the advice in the docs above, you could try something like:
Code:
Sub Main()
Console.InputEncoding = Text.Encoding.Unicode
Console.OutputEncoding = Text.Encoding.Unicode
Dim strm = Console.OpenStandardInput(0)
Dim reader = New IO.StreamReader(strm, Text.Encoding.Unicode)
Console.SetIn(reader)
Console.WriteLine("enter x to exit")
Console.WriteLine()
Dim input As String
Do
input = Console.In.ReadLine()
Console.WriteLine()
Console.WriteLine($"echo> {input}")
Console.WriteLine()
Loop Until input = "x"
End Sub
which calls Console.OpenStandardInput to open an input Stream, points the input reader to that Stream with Console.SetIn, and replaces all calls to Console.Readline() with calls to Console.In.ReadLine() instead.
That said, I've not been able to enter more than 512 UTF-16 Chars including NewLine using this approach. Hopefully you don't need to??
-
Apr 6th, 2021, 01:28 PM
#10
Re: ∞ in console with Server 2019?
Originally Posted by Inferrd
That said, I've not been able to enter more than 512 UTF-16 Chars including NewLine using this approach. Hopefully you don't need to??
After a little more thought, I remembered there's an overload of the StreamReader constructor that accepts a buffer size, and sure enough, specifying a larger buffer size allows more characters to be typed at the keyboard. The buffer size is given as the number of Bytes, so for UTF-16 (what Microsoft name as 'Unicode' in their encoding schemes) you'd need to specify twice as many bytes as the number of characters you are likely to require ('Unicode' encoding is a two byte encoding).
This would also explain why you were only able to enter 127 'Unicode' characters even though Microsoft talk about a 256-character input buffer: they are thinking of single byte characters, which is the default for the Console, no doubt for historical reasons.
So in the code in my previous post, you'd create the StreamReader along the lines of:
Code:
Dim characterBufferSize = 8192
Dim reader = New IO.StreamReader(strm, Text.Encoding.Unicode, True, characterBufferSize * 2)
Console.SetIn(reader)
-
Apr 8th, 2021, 09:01 PM
#11
Re: ∞ in console with Server 2019?
Nice; thanks for that
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|