gpt4 book ai didi

java - 如何正确计算字符串字节数?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:45:04 24 4
gpt4 key购买 nike

A 包含特殊字符(例如 ç)的每个特殊字符占用两个字节的大小,但是 String length method或者使用从 getBytes method 返回的字节数组获取它的长度不返回计为两个字节的特殊字符。

如何正确计算字符串中的字节数?

示例:

单词 endereço 应该返回长度为 9 而不是 8。

最佳答案

The word endereço should return me length 9 instead of 8.

如果您希望长度为 8 个字符的 "endereço" 字符串的大小为 9 个字节:7 个 ASCII 字符和 1 个非 ASCII 字符,我想你想使用 UTF-8 字符集,它对 ASCII 表中包含的字符使用 1 个字节,对其他字符使用更多字节。

but String length method or getting the length of it with the byte array returned from getBytes method doesn't return special chars counted as two bytes.


String length() 方法没有回答这个问题:使用了多少字节? 但是回答了:“有多少“UTF-16 代码单元”或更简单的 char 包含在其中?

字符串 length() Javadoc :

Returns the length of this string. The length is equal to the number of Unicode code units in the string.


不带参数的byte[] getBytes() 方法将字符串编码为字节数组。您可以使用返回数组的 length 属性来了解编码字符串使用了多少字节,但结果将取决于编码期间使用的字符集。但是 byte[] getBytes() 方法不允许指定字符集:它使用平台的默认字符集
因此,如果底层操作系统默认使用的字符集不是您要用于以字节为单位对字符串进行编码的字符集,则使用它可能不会给出预期的结果。
此外,根据部署应用程序的平台,字符串以字节为单位的编码方式可能会发生变化。这可能是不受欢迎的。
最后,如果 String 不能在默认字符集中编码,则行为未指定。
因此,这种方法应该非常谨慎地使用或根本不使用。

byte[] getBytes() Javadoc :

Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

The behavior of this method when this string cannot be encoded in the default charset is unspecified. The java.nio.charset.CharsetEncoder class should be used when more control over the encoding process is required.

在您的字符串示例 "endereço" 中,如果 getBytes() 返回一个大小为 8 而不是 9 的数组,这意味着您的操作系统不使用默认情况下 UTF-8 但字符集使用 1 字节固定宽度的字符,例如 ISO 8859-1 及其派生字符集,例如 windows-1252 基于 Windows 操作系统。

要了解应用程序运行的当前 Java 虚拟机的默认字符集,您可以使用此实用程序方法:Charset defaultCharset = Charset.defaultCharset()


解决方案

byte[] getBytes() 方法带有另外两个非常有用的重载:

  • byte[] java.lang.String.getBytes(String charsetName) 抛出 UnsupportedEncodingException

  • byte[] java.lang.String.getBytes(Charset 字符集)

与不带参数的 getBytes() 方法相反,这些方法允许指定在字节编码期间使用的字符集。

byte[] java.lang.String.getBytes(String charsetName) 抛出 UnsupportedEncodingException Javadoc :

Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.

The behavior of this method when this string cannot be encoded in the given charset is unspecified. The java.nio.charset.CharsetEncoder class should be used when more control over the encoding process is required.

byte[] java.lang.String.getBytes(Charset 字符集) Javadoc :

Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.

This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement byte array. The java.nio.charset.CharsetEncoder class should be used when more control over the encoding process is required.

您可以使用一个或另一个(虽然它们之间存在一些复杂性)将您的字符串编码为字节数组,使用 UTF-8 或任何其他字符集,并因此获得它的大小特定字符集。

例如,要使用 getBytes(String charsetName) 获取 UTF-8 编码字节数组,您可以这样做:

String yourString = "endereço";
byte[] bytes = yourString.getBytes("UTF-8");
int sizeInBytes = bytes.length;

你会得到你想要的9个字节的长度。

这是一个更全面的示例,显示了默认编码,默认字符集平台的字节编码,UTF-8UTF-16:

public static void main(String[] args) throws UnsupportedEncodingException {

// default charset
Charset defaultCharset = Charset.defaultCharset();
System.out.println("default charset = " + defaultCharset);

// String sample
String yourString = "endereço";

// getBytes() with default platform encoding
System.out.println("getBytes() with default charset, size = " + yourString.getBytes().length + System.lineSeparator());

// getBytes() with specific charset UTF-8
System.out.println("getBytes(\"UTF-8\"), size = " + yourString.getBytes("UTF-8").length);
System.out.println("getBytes(StandardCharsets.UTF_8), size = " + yourString.getBytes(StandardCharsets.UTF_8).length + System.lineSeparator());

// getBytes() with specific charset UTF-16
System.out.println("getBytes(\"UTF-16\"), size = " + yourString.getBytes("UTF-16").length);
System.out.println("getBytes(StandardCharsets.UTF_16), size = " + yourString.getBytes(StandardCharsets.UTF_16).length);
}

我基于 Windows 操作系统的机器上的输出:

default charset = windows-1252

getBytes() with default charset, size = 8

getBytes("UTF-8"), size = 9

getBytes(StandardCharsets.UTF_8), size = 9

getBytes("UTF-16"), size = 18

getBytes(StandardCharsets.UTF_16), size = 18

关于java - 如何正确计算字符串字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43195075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com