Convert a string of integers to byte array in Ruby

微笑、不失礼 提交于 2020-01-03 00:32:08

问题


I have a string of integer "72101108108111" but it is a string representing bytes of original string "Hello".

How can I convert "72101108108111" to Ascii string "Hello" in Ruby?


回答1:


s = '72101108108111'
pattern=/^[a-zA-Z]/
index,res=0,''
while index<s.size
  len=0
  while (s[index..index+len].to_i.chr=~pattern).nil?
    len+=1
  end
  res << s[index..index+len].to_i.chr
  index+=len+1
end

p res

Try this , because the length of every string to be decoded is not certain.

For example, "72->'H' , 101->'e' , 23->'\x17'"

So every time we find the character decoded is not "a~z" or "A~Z" (ex:\x17),

we just add the length and parse until we find character we want

For this case , the answer will be correctly "Hello"

It may malfunction on some case , but it works well now for my test

Just take it a look

It only works without Exception on the case containing only "A-Z and a-z"




回答2:


Answering your question as clarified in comments (which has nothing to do with the title):

I need to encode/decode a string to Base58

EDIT: now as a class (using base58 gem):

require 'base58'

class Base58ForStrings
  def self.encode(str)
    Base58.encode(str.bytes.inject { |a, b| a * 256 + b })
  end

  def self.decode(b58)
    b = []
    d = Base58.decode(b58)
    while (d > 0)
      d, m = d.divmod(256)
      b.unshift(m)
    end
    b.pack('C*').force_encoding('UTF-8')
  end
end

Base58ForStrings.encode('Hello こんにちは')
# => "5scGDXBpe3Vq7szFXzFcxHYovbD9c" 
Base58ForStrings.decode('5scGDXBpe3Vq7szFXzFcxHYovbD9c')
# => "Hello こんにちは"

Works for any UTF-8 string.



来源:https://stackoverflow.com/questions/35050295/convert-a-string-of-integers-to-byte-array-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!