将字符从扩展ASCII转换为二进制 [英] Converting characters from extended ASCII to binary

查看:79
本文介绍了将字符从扩展ASCII转换为二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我想在VB.NET中将€转换为二进制格式10000000.



问题是代码:



Dim c as String =€

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



产生10000010101100.



如何更改代码以使其产生10000000?

Hello,

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?

推荐答案

ASCII代码 €符号的 Unicode代码点& H20AC ,二进制代码为: 10000010101100

http://www.eurosymbol.eu/ascii-code [< a href =http://www.eurosymbol.eu/ascii-codetarget =_ blanktitle =New Window> ^ ]



二进制值 10000000 & H80 ,或 128 十进制)仅代表某些编码中的欧元符号。根据您的区域设置,您可以使用 Asc (而不是 AscW )来获取您的值'期待:

The ASCII code Unicode code-point for the € symbol is &H20AC, which in binary would be: 10000010101100
http://www.eurosymbol.eu/ascii-code[^]

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")


官方说,有没有扩展ASCII这样的东西,无论如何,它不是.NET直接支持的。这个问题没有意义。只有一个字符串可以是非二进制的二进制,并且,对于二进制表示,编码是什么并不重要 - 数据是数据。

Dim c as String = €是Unicode字符串。函数 AscW (我不知道为什么会这样调用;最好不要调用VB.NET特定的方法)给你字符代码


.NET字符串总是Unicode字符串,尽快因为你没有将其转换为其他东西,通常是对应于某些特定编码的字节数据数组。内存中的内部编码是UTF-16LE,但字符串API的主要部分是为了与这个事实无关;你应该避免直接使用这些信息。永远记住CLR是多平台的。



请参阅:

https://msdn.microsoft.com/en-us/library/zew1e4wc%28v=vs.90%29.aspx [ ^ ],

http://en.wikipedia.org/wiki/Extended_ASCII [ ^ ],

http://en.wikipedia.org/wiki/Unicode [ ^ ]。



-SA
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:
https://msdn.microsoft.com/en-us/library/zew1e4wc%28v=vs.90%29.aspx[^],
http://en.wikipedia.org/wiki/Extended_ASCII[^],
http://en.wikipedia.org/wiki/Unicode[^].

—SA


这篇关于将字符从扩展ASCII转换为二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆