String(byte[] bytes)
构造函数和 String.getBytes()
方法不是由 GWT JRE 仿真字符串类实现的。
有人知道实现吗?我不想使用 char[]
, 不过好像也没有别的办法了。
最佳答案
如果您在 Chrome 中创建大型数组,您可能会遇到 Uncaught RangeError: Maximum call stack size exceeded异常(exception)。可以修改 LINEMAN78 中的代码以使用 StringBuilder,从而避免了此问题。
public static String getString(byte[] bytes, int bytesPerChar)
{
if (bytes == null) throw new IllegalArgumentException("bytes cannot be null");
if (bytesPerChar < 1) throw new IllegalArgumentException("bytesPerChar must be greater than 1");
final int length = bytes.length / bytesPerChar;
final StringBuilder retValue = new StringBuilder();
for (int i = 0; i < length; i++)
{
char thisChar = 0;
for (int j = 0; j < bytesPerChar; j++)
{
int shift = (bytesPerChar - 1 - j) * 8;
thisChar |= (0x000000FF << shift) & (((int) bytes[i * bytesPerChar + j]) << shift);
}
retValue.append(thisChar);
}
return retValue.toString();
}
关于string - 如何使用 GWT 将字节数组转换为字符串,并将字符串转换为字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4584998/