1

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?

user3075906
  • 725
  • 1
  • 8
  • 19
  • 1
    You can't. There's no way to tell whether the first byte is `7` or `72`. If we guess `72` correctly there's no way to tell if the second byte is `1`, `10`, or `101`, and so on. However this string was generated, it wasn't generated with decoding in mind. – Jordan Running Jan 27 '16 at 23:48
  • Can we do it if we assume 2 digits make a byte? i.e. its 72,10,11 etc? – user3075906 Jan 28 '16 at 00:12
  • That wouldn't give you "Hello." The ASCII code for `H` is 72, but `e` is 101. – Jordan Running Jan 28 '16 at 00:23
  • I need to encode/decode a string to Base58. Found a gem which converts an integer to Base58. Trying to figure out if I can convert string to bytes and then encode and vice versa. – user3075906 Jan 28 '16 at 00:29
  • 2
    Concatenating the base 10 ASCII codes into a string of digits isn't the right way to "convert a string to bytes." – Jordan Running Jan 28 '16 at 00:52

2 Answers2

0

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.

Amadan
  • 191,408
  • 23
  • 240
  • 301
0
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"

DumDumGenius
  • 163
  • 2
  • 7