新String和Bytes.toString之间的差异

时间:2017-01-16 23:22:12

标签: string byte

我正在使用hbase实用程序org.apache.hadoop.hbase.util.Bytes 我从一个字符串生成了一个字节数组(在Scala中的一个例子中):

val bytes = Bytes.toBytes("test")

并希望转换回String。

new String(bytes,"UTF-8")Bytes.toString(bytes)

之间有何区别?

他们都有效。

1 个答案:

答案 0 :(得分:0)

猜测你在谈论https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/util/Bytes.html,基本上没什么:Bytes.toString会调用new String,除非数组是空的。您可以看到名为here的方法:

public static String toString(final byte [] b, int off, int len) {
    if (b == null) {
      return null;
    }
    if (len == 0) {
      return "";
    }
    return new String(b, off, len, UTF8_CHARSET);
}

将来,请提及您在问题中使用的任何库(问题与Scala完全无关)。

相关问题