Ruby: create a String from bytes

前端 未结 5 1311
眼角桃花
眼角桃花 2020-12-23 19:44

I would like to build a string from a byte value.

I currently use:

str = \" \"
str[0] = byte

This seems to work fine but I find it

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 20:23

    If bytes is an array of Fixnum's you could try this:

    bytes.map {|num| num.chr}.join
    

    or this:

    s = ''
    bytes.each {|i| s << i}
    

提交回复
热议问题