简体   繁体   中英

how can i convert bytes into base64 using ruby

i want to convert bytes into the base64 format using ruby i tried this program but am not getting the proper output please help me

require 'base64'
require 'open-uri'
str = File.new("/path/to/file.mp3")
a = Base64.encode64(str.each_byte{|byte| byte})
puts a

Use File.read and not File.new

TO ENCODE WITHOUT CONVERTING TO BYTES USE:

require 'base64'
str = File.read("/path/to/file.mp3")
a = Base64.encode64(str)
puts a

EDIT to convert to bytes first before encoding use:

require 'base64'
str = File.read("/path/to/file.mp3")
a = Base64.encode64(str.each_byte.to_a.join)
puts a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM