Why does Encoding.Default.GetBytes () return different results in VB.NET and C #?

We recently came across some sample code from a provider for hashing a private key to call a web service, their sample was in VB.NET, which we converted to C #. This led to the hash creating different input data. It turns out that the way to generate the key for encryption was by converting the char array to a string and back to an array of bytes. This led me to the discovery that VB.NET and C # encoders work with some characters by default.

FROM#:

Console.Write(Encoding.Default.GetBytes(new char[] { (char)149 })[0]); 

VB:

 Dim b As Char() = {Chr(149)} Console.WriteLine(Encoding.Default.GetBytes(b)(0)) 

C # output is 63, and VB is the correct value for byte 149. if you use any other value, for example 145, etc., the result is consistent.

Passing through debugging, both VB and C # by default encodes SBCSCodePageEncoding.

Does anyone know why this is?

I adjusted the sample code by directly initializing the byte array, which should have been in the first place, but I still want to know why the encoder, which should not be language specific, looks exactly like that.

+6
c # encoding
source share
5 answers

If you use ChrW (149), you will get a different result - 63, the same as C #.

 Dim b As Char() = {ChrW(149)} Console.WriteLine(Encoding.Default.GetBytes(b)(0)) 

Read the documentation to see the difference that will explain the answer.

+11
source share

The VB Chr function takes an argument between 0 and 255 and converts it to a character using the current default code page. It throws an exception if you pass an argument outside of this range.

ChrW will take a 16-bit value and return the corresponding System.Char value without using encoding - therefore, it will give the same result as the C # code that you published.

An approximate equivalent of your VB C # code without using the VB Strings class (a class containing Chr and ChrW):

 char[] chars = Encoding.Default.GetChars(new byte[] { 149 }); Console.Write(Encoding.Default.GetBytes(chars)[0]); 
+4
source share

the default encoding is machine dependent and also dependent on the stream because it uses the current code page. Usually you should use something like Encoding.UTF8 to not worry about what happens when one machine uses unicode and the other uses 1252-ANSI.

0
source share

Different operating systems may use different default encodings. Consequently, data transferred from one operating system to another may be translated incorrectly. To ensure that encoded bytes are decoded correctly, your application must use Unicode encoding, i.e., UTF8Encoding, UnicodeEncoding or UTF32Encoding, with a preamble. Another option is to use a higher level so that the same format is used for encoding and decoding.

from http://msdn.microsoft.com/en-us/library/system.text.encoding.default.aspx

can you check what each language produces when explicitly encoded using utf8?

0
source share

I believe that the equivalent in VB is ChrW (149).

So this VB code ...

  Dim c As Char() = New Char() { Chr(149) } 'Dim c As Char() = New Char() { ChrW(149) } Dim b As Byte() = System.Text.Encoding.Default.GetBytes(c) Console.WriteLine("{0}", Convert.ToInt32(c(0))) Console.WriteLine("{0}", CInt(b(0))) 

produces the same result as this C # code ...

  var c = new char[] { (char)149 }; var b = System.Text.Encoding.Default.GetBytes(c); Console.WriteLine("{0}", (int)c[0]); Console.WriteLine("{0}", (int) b[0]); 
0
source share

All Articles