Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 与base64类方法用法的混淆 有谁能帮助我理解我们需要考虑的问题。 以下4种方法: strict_decode64(str) strict_encode64(bin) urlsafe_encode64(bin) urlsafe_decode64(str)_Ruby - Fatal编程技术网

Ruby 与base64类方法用法的混淆 有谁能帮助我理解我们需要考虑的问题。 以下4种方法: strict_decode64(str) strict_encode64(bin) urlsafe_encode64(bin) urlsafe_decode64(str)

Ruby 与base64类方法用法的混淆 有谁能帮助我理解我们需要考虑的问题。 以下4种方法: strict_decode64(str) strict_encode64(bin) urlsafe_encode64(bin) urlsafe_decode64(str),ruby,Ruby,从医生那里我也没有得到任何例子。因此,请举例说明 解释可能有助于我理解 提前感谢编码和解码做相反的事情:第一个将普通字符串转换为编码字符串,第二个将编码字符串转换为普通字符串 str = "Hello!" str == decode64(encode64(str)) # This is true strict_和urlsafe_之间的区别在于编码字符串中将使用的字符。当您需要在URL内传递字符串时,不允许使用所有字符(例如/,因为它在URL中有特殊含义),因此您应该使用urlsafe\uuuu

从医生那里我也没有得到任何例子。因此,请举例说明 解释可能有助于我理解


提前感谢

编码和解码做相反的事情:第一个将普通字符串转换为编码字符串,第二个将编码字符串转换为普通字符串

str = "Hello!"
str == decode64(encode64(str)) # This is true

strict_
urlsafe_
之间的区别在于编码字符串中将使用的字符。当您需要在URL内传递字符串时,不允许使用所有字符(例如
/
,因为它在URL中有特殊含义),因此您应该使用
urlsafe\uuuuu
版本。

用法示例如下:

require "base64"
Base64.strict_encode64('Stuff to be encoded')
Base64.strict_decode64("U3R1ZmYgdG8gYmUgZW5jb2RlZA==")
严格意味着在解码时拒绝使用空格/CR/LF,在编码时不添加空格/CR/LF

请注意,如果接受下列条件:

Base64.decode64("U3R1ZmYgdG8gYmUgZW5jb2RlZA==\n")
使用strict时,由于后面的
\n
(换行符)而不接受上述内容,并且以下行将抛出
ArgumentError:invalid base64
异常:

Base64.strict_decode64("U3R1ZmYgdG8gYmUgZW5jb2RlZA==\n")
因此,strict在解码时只接受/期望字母数字字符,在编码时只返回字母数字字符

请尝试以下操作,并查看如何使用
'\n'
(换行符)对每60个字符的行进行编码换行,而strict没有:

print Base64.encode64('I will not use spaces and new lines. I will not use spaces and new lines. I will not use spaces and new lines. I will not use spaces and new lines.I will not use spaces and new lines.')


print Base64.strict_encode64('I will not use spaces and new lines. I will not use spaces and new lines. I will not use spaces and new lines. I will not use spaces and new lines.I will not use spaces and new lines.')

文档中不清楚的是什么?缺少示例。那里只有几个字。因此无法理解它的用法。它实际上意味着什么严格,它能做什么,而正常的
编码
解码
却做不到helps@iAmRubuuu以上两个示例都是针对str的,bin表示使用了其他二进制表示形式,但它仍然是字符串ex:
Base64.strict\u encode64(“\xaa\xab\xac\xad”)
Base64.strict_decode64(“\x553R1ZmYgdG8gYmUgZW5jb2RlZA==”)
注意答案示例中的
U
\x55