Java 的 ASC Visual Basic
ASC Visual Basic for Java
我需要 Java 上的一个函数,该函数的作用与 Visual Basic 上的 ASC 函数相同。我一直在网上寻找它,但找不到解决方案。
我必须知道代码的字符串是在 Visual Basic 上创建的。它符合 ISO 8859-1 和 Microsoft Windows Latin-1 字符。 Visual Basic 上的 ASC 函数知道这些代码,但在 Java 中,我找不到做同样事情的函数。
我知道在Java这句话:
String myString = "ÅÛ–ßÕÅÝ•ÞÃ";
int first = (int)string.chartAt(0); // "Å"- VB and Java returns: 197
int second = (int)string.chartAt(0); // "Û" - VB and Java returns: 219
int third = (int)string.chartAt(0); // "–" - VB returns: 150 and Java returns: 8211
前两个字符,我没有遇到问题,但第三个字符不是ASCII码。
如何在 VB 和 Java 中获得相同的代码?
首先,请注意 ISO 8859-1 != Windows Latin-1。 (参见 http://en.wikipedia.org/wiki/Windows-1252)
问题是 Java 将字符编码为 UTF16,因此转换为 int 通常会导致字符的 Unicode 值。
要获取字符的 Latin-1 编码,首先将其转换为 Latin-1 编码的字节数组:
public class Encoding {
public static void main(String[] args) {
// Cp1252 is Windows codepage 1252
byte[] bytes = "ÅÛ–ßÕÅÝ•ÞÃ".getBytes(Charset.forName("Cp1252"));
for (byte b: bytes) {
System.out.println(b & 255);
}
}
}
打印:
197
219
150
223
213
197
221
149
222
195
我需要 Java 上的一个函数,该函数的作用与 Visual Basic 上的 ASC 函数相同。我一直在网上寻找它,但找不到解决方案。
我必须知道代码的字符串是在 Visual Basic 上创建的。它符合 ISO 8859-1 和 Microsoft Windows Latin-1 字符。 Visual Basic 上的 ASC 函数知道这些代码,但在 Java 中,我找不到做同样事情的函数。
我知道在Java这句话:
String myString = "ÅÛ–ßÕÅÝ•ÞÃ";
int first = (int)string.chartAt(0); // "Å"- VB and Java returns: 197
int second = (int)string.chartAt(0); // "Û" - VB and Java returns: 219
int third = (int)string.chartAt(0); // "–" - VB returns: 150 and Java returns: 8211
前两个字符,我没有遇到问题,但第三个字符不是ASCII码。
如何在 VB 和 Java 中获得相同的代码?
首先,请注意 ISO 8859-1 != Windows Latin-1。 (参见 http://en.wikipedia.org/wiki/Windows-1252)
问题是 Java 将字符编码为 UTF16,因此转换为 int 通常会导致字符的 Unicode 值。
要获取字符的 Latin-1 编码,首先将其转换为 Latin-1 编码的字节数组:
public class Encoding {
public static void main(String[] args) {
// Cp1252 is Windows codepage 1252
byte[] bytes = "ÅÛ–ßÕÅÝ•ÞÃ".getBytes(Charset.forName("Cp1252"));
for (byte b: bytes) {
System.out.println(b & 255);
}
}
}
打印:
197
219
150
223
213
197
221
149
222
195