Ruby: create a String from bytes

前端 未结 5 1317
眼角桃花
眼角桃花 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:27

    This isn't the OP's question, but if you have just a single byte (not in an array) and want to make a string out of it, use chr

    c = 65
    => 65
    c.chr
    => "A"
    c.chr.class
    => String
    

提交回复
热议问题