Click here to Skip to main content
15,743,427 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:

I'd like to convert "€" to its binary form, 10000000, in VB.NET.

The problem is that the code:

Dim c as String = "€"
Dim binary As String = System.Convert.ToString(AscW(c), 2).PadLeft(8, "0")

produces 10000010101100.

How should I change the code to get it to result in 10000000?

The ASCII code Unicode code-point for the € symbol is &H20AC, which in binary would be: 10000010101100[^]

The binary value 10000000 (&H80, or 128 in decimal) only represents the Euro symbol in certain encodings. Depending on your regional settings, you might be able to use Asc (rather than AscW) to get the value you're expecting:
Dim c As String = "€"
Dim binary As String = System.Convert.ToString(Asc(c), 2).PadLeft(8, "0")
Share this answer
Sergey Alexandrovich Kryukov 19-May-15 14:45pm    
I voted 4, because this link is misleading. This is not what usually called ASCII. Apparently, the table shows Unicode code point. Also, I'm afraid the inquirer won't get it. Please see some explanations I provided in Solution 2.
Richard Deeming 19-May-15 14:48pm    
I've updated the answer - if you use Asc(c) instead of AscW(c), it should produce the expected output, but it obviously depends on the current regional settings.
Sergey Alexandrovich Kryukov 19-May-15 15:14pm    
I didn't check it up, but 5ed. (I have no idea why the inquirer would need it.)
Member 11450536 19-May-15 14:58pm    
Regardless of link validity, Asc() worked. Thanks :3
Officially, there is no such thing as "extended ASCII", and, anyway, it is not directly supported by .NET. The question does not make sense. Only a string can be binary of non-binary, and, for "binary" representation it does not matter what is the encoding — data is data.
Dim c as String = "€" is the Unicode string. The function AscW (I have no idea why is it called like that; it's better not to call VB.NET-specific methods) gives you the character code and nothing else. You got what you wanted. I have no idea where did you get "10000000" and why are you even thinking about this "Extended ASCII".

.NET strings are always Unicode strings, as soon as you don't convert the to something else, usually to array of byte data corresponding to some particular encoding. The internal encoding in memory is UTF-16LE, but the major part of string API is built to be agnostic to this fact; you should avoid using this information directly. Always remember that CLR is multiplatform.

Please see:[^],[^],[^].

Share this answer
Member 11450536 19-May-15 14:57pm    
"10000000" was found from a website; a screenshot of which can be seen here:
So if 1000000 can be converted to €, why is it converted back to 10000010101100 instead?
Sergey Alexandrovich Kryukov 19-May-15 15:16pm    
Not clear. I cannot understand why would you ever need this "10000000". 1) .NET works only with Unicode; 2) you should avoid using the information on any particular encoding; you should not even count character number using the number of bytes.
Member 11450536 19-May-15 16:31pm    
I'm making a cryptography algorithm and part of it involves calculating new binary values... and one specific value was converted to €.
Sergey Alexandrovich Kryukov 19-May-15 16:46pm    
You are doing it wrong. Cryptography works with arrays of bytes and nothing else, ever.
You should never convert string/character data by conversion or casting. The only valid way is System.Text.Encoding.GetBytes.
Please see And nothing else, period.

That should give you the final solution for your problem. Are you going to accept it formally now?


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900