Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java/Scala中将字节数组转换为字符串时修剪字节数组_Java_String_Scala_Bytearray - Fatal编程技术网

在Java/Scala中将字节数组转换为字符串时修剪字节数组

在Java/Scala中将字节数组转换为字符串时修剪字节数组,java,string,scala,bytearray,Java,String,Scala,Bytearray,使用ByteBuffer,我可以将字符串转换为字节数组: val x = ByteBuffer.allocate(10).put("Hello".getBytes()).array() > Array[Byte] = Array(104, 101, 108, 108, 111, 0, 0, 0, 0, 0) 将字节数组转换为字符串时,我可以使用新字符串(x)。 但是,字符串变为hello?????,我需要在将字节数组转换为字符串之前对其进行精简。我该怎么做 我使用这段代码来减少零,但我想

使用ByteBuffer,我可以将字符串转换为字节数组:

val x = ByteBuffer.allocate(10).put("Hello".getBytes()).array()
> Array[Byte] = Array(104, 101, 108, 108, 111, 0, 0, 0, 0, 0)
将字节数组转换为字符串时,我可以使用
新字符串(x)
。 但是,字符串变为
hello?????
,我需要在将字节数组转换为字符串之前对其进行精简。我该怎么做

我使用这段代码来减少零,但我想知道是否有更简单的方法

def byteArrayToString(x: Array[Byte]) = {
    val loc = x.indexOf(0)
    if (-1 == loc)
      new String(x)
    else if (0 == loc)
      ""
    else
      new String(x.slice(0,loc))
}

如果您只有一个
字符串
,我将使用
.getBytes()
-

输出为

x: Array[Byte] = Array(72, 101, 108, 108, 111)
x: Array[Byte] = Array(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33)
对于不止一个
字符串
,我会使用-

val baos = new java.io.ByteArrayOutputStream(10); //  <-- I might not use 10.
                                                  //  <-- Smells of premature opt.
baos.write("Hello".getBytes("UTF-8"));
baos.write(", World!".getBytes("UTF-8"));

val x:Array[Byte] = baos.toByteArray(); // <-- x:Array[Byte], to specify the type.
有几个接受偏移+长度到
字节[]
-这就不需要在创建新的修剪数组之前

使用其中一个重载构造函数可能如下所示:

def byteArrayToString(x: Array[Byte]) = {
    val loc = x.indexOf(0)
    if (-1 == loc)
      new String(x)
    else if (0 == loc)
      ""
    else
      new String(x, 0, loc, "UTF-8") // or appropriate encoding
}
或者,保持指数的微小变化:

def byteArrayToString(arr: Array[Byte]) = {
    val loc = arr.indexOf(0)
    // length passed to constructor can be 0..arr.length
    new String(arr, 0, if (loc >= 0) loc else arr.length, "UTF-8")
}
或者,一行(感谢find/Option):


关于编码的思考:

  • 通常建议使用显式编码,并且应使用在
    getBytes
    中指定的相同编码,作为。这是你的电话号码

  • 字节
    0
    可能出现在数据结束前的编码输出中,具体取决于字符串输入(即NUL)和使用的编码


  • 你可以这样做:

    val bb = java.nio.ByteBuffer.allocate(10).put("Hello".getBytes)
    val s = new String(bb.array, 0, bb.position)
    

    虽然这并不能在ByteBuffer中表明你读过任何东西。通常的模式是翻转并使用限制,但如果您只是抓取数组,那么您最好使用定位,并在阅读更多内容之前清除。

    假设
    0:Byte
    是尾随值,然后

    implicit class RichToString(val x: java.nio.ByteBuffer) extends AnyVal {
      def byteArrayToString() = new String( x.array.takeWhile(_ != 0), "UTF-8" )
    }
    
    因此

    val x = ByteBuffer.allocate(10).put("Hello".getBytes())
    
    x.byteArrayToString
    res: String = Hello
    
    implicit class RichToString(val x: java.nio.ByteBuffer) extends AnyVal {
      def byteArrayToString() = new String( x.array.takeWhile(_ != 0), "UTF-8" )
    }
    
    val x = ByteBuffer.allocate(10).put("Hello".getBytes())
    
    x.byteArrayToString
    res: String = Hello