将字节数组转换为字符串 [英] convert a byte array to string

查看:103
本文介绍了将字节数组转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Scala代码从字节流中接收了一个二进制文件,看起来像[61 62 63 64]。内容为 abcd。
我使用toString将其转换为p,但失败。
如何将其打印为字符串?

My Scala code received a binary from byte stream,it looks like [61 62 63 64].The content is "abcd". I use toString to convert it p, but failed. How do I print it as string ?

推荐答案

您可以将字节数组转换为char数组,然后从中构造一个字符串

You could convert the byte array to a char array, and then construct a string from that

scala> val bytes = Array[Byte]('a','b','c','d')
bytes: Array[Byte] = Array(97, 98, 99, 100)

scala> (bytes.map(_.toChar)).mkString 
res10: String = abcd

scala> 

这篇关于将字节数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆