<bdo id='lyUX4'></bdo><ul id='lyUX4'></ul>
    <i id='lyUX4'><tr id='lyUX4'><dt id='lyUX4'><q id='lyUX4'><span id='lyUX4'><b id='lyUX4'><form id='lyUX4'><ins id='lyUX4'></ins><ul id='lyUX4'></ul><sub id='lyUX4'></sub></form><legend id='lyUX4'></legend><bdo id='lyUX4'><pre id='lyUX4'><center id='lyUX4'></center></pre></bdo></b><th id='lyUX4'></th></span></q></dt></tr></i><div id='lyUX4'><tfoot id='lyUX4'></tfoot><dl id='lyUX4'><fieldset id='lyUX4'></fieldset></dl></div>

    1. <legend id='lyUX4'><style id='lyUX4'><dir id='lyUX4'><q id='lyUX4'></q></dir></style></legend>

    2. <small id='lyUX4'></small><noframes id='lyUX4'>

      1. <tfoot id='lyUX4'></tfoot>
      2. 将 base64URL 解码为 base64 -- Swift

        时间:2024-04-14

        <small id='9tW1G'></small><noframes id='9tW1G'>

          1. <i id='9tW1G'><tr id='9tW1G'><dt id='9tW1G'><q id='9tW1G'><span id='9tW1G'><b id='9tW1G'><form id='9tW1G'><ins id='9tW1G'></ins><ul id='9tW1G'></ul><sub id='9tW1G'></sub></form><legend id='9tW1G'></legend><bdo id='9tW1G'><pre id='9tW1G'><center id='9tW1G'></center></pre></bdo></b><th id='9tW1G'></th></span></q></dt></tr></i><div id='9tW1G'><tfoot id='9tW1G'></tfoot><dl id='9tW1G'><fieldset id='9tW1G'></fieldset></dl></div>
              • <bdo id='9tW1G'></bdo><ul id='9tW1G'></ul>
                  <tfoot id='9tW1G'></tfoot>
                  <legend id='9tW1G'><style id='9tW1G'><dir id='9tW1G'><q id='9tW1G'></q></dir></style></legend>

                    <tbody id='9tW1G'></tbody>
                  本文介绍了将 base64URL 解码为 base64 -- Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我还没有找到正确的方法来快速将 base64URL 解码为 base64 格式.

                  I haven't found properly way how to decode base64URL to base64 format in swift.

                  根据base64url转base64 hJQWHABDBjoPHorYF5xghQ(base64URL)应该是 hJQWHABDBjoPHorYF5xghQ==(base64).这里可能会有更多差异.

                  According to base64url to base64 hJQWHABDBjoPHorYF5xghQ(base64URL) should be hJQWHABDBjoPHorYF5xghQ==(base64). Here could be more differences.

                  stackoverflow 上没有解决方案.

                  There are no solutions on stackoverflow.

                  推荐答案

                  base64url"与标准Base64编码有两个不同:

                  "base64url" differs from the standard Base64 encoding in two aspects:

                  • 索引 62 和 63 使用不同的字符(-_+/)
                  • 没有强制填充 = 字符来使字符串长度四的倍数.
                  • different characters are used for index 62 and 63 (- and _ instead of + and /)
                  • no mandatory padding with = characters to make the string length a multiple of four.

                  (比较 https://en.wikipedia.org/wiki/Base64#Variants_summary_table).

                  这是一个可能的转换函数:

                  Here is a possible conversion function:

                  func base64urlToBase64(base64url: String) -> String {
                      var base64 = base64url
                          .replacingOccurrences(of: "-", with: "+")
                          .replacingOccurrences(of: "_", with: "/")
                      if base64.characters.count % 4 != 0 {
                          base64.append(String(repeating: "=", count: 4 - base64.characters.count % 4))
                      }
                      return base64
                  }
                  

                  例子:

                  let base64url = "hJQWHABDBjoPHorYF5xghQ"
                  let base64 = base64urlToBase64(base64url: base64url)
                  print(base64) // hJQWHABDBjoPHorYF5xghQ==
                  
                  if let data = Data(base64Encoded: base64) {
                      print(data as NSData) // <8494161c 0043063a 0f1e8ad8 179c6085>
                  }
                  

                  为了完整起见,这将是相反的转换:

                  For the sake of completeness, this would be the opposite conversion:

                  func base64ToBase64url(base64: String) -> String {
                      let base64url = base64
                          .replacingOccurrences(of: "+", with: "-")
                          .replacingOccurrences(of: "/", with: "_")
                          .replacingOccurrences(of: "=", with: "")
                      return base64url
                  }
                  

                  <小时>

                  Swift 4 更新:

                  func base64urlToBase64(base64url: String) -> String {
                      var base64 = base64url
                          .replacingOccurrences(of: "-", with: "+")
                          .replacingOccurrences(of: "_", with: "/")
                      if base64.count % 4 != 0 {
                          base64.append(String(repeating: "=", count: 4 - base64.count % 4))
                      }
                      return base64
                  }
                  

                  这篇关于将 base64URL 解码为 base64 -- Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Swift 中将 base64String 转换为 String? 下一篇:在 NSString 中解码 Base-64 编码的 PNG

                  相关文章

                  <tfoot id='VMP4w'></tfoot>
                  • <bdo id='VMP4w'></bdo><ul id='VMP4w'></ul>
                • <legend id='VMP4w'><style id='VMP4w'><dir id='VMP4w'><q id='VMP4w'></q></dir></style></legend>
                      <i id='VMP4w'><tr id='VMP4w'><dt id='VMP4w'><q id='VMP4w'><span id='VMP4w'><b id='VMP4w'><form id='VMP4w'><ins id='VMP4w'></ins><ul id='VMP4w'></ul><sub id='VMP4w'></sub></form><legend id='VMP4w'></legend><bdo id='VMP4w'><pre id='VMP4w'><center id='VMP4w'></center></pre></bdo></b><th id='VMP4w'></th></span></q></dt></tr></i><div id='VMP4w'><tfoot id='VMP4w'></tfoot><dl id='VMP4w'><fieldset id='VMP4w'></fieldset></dl></div>

                      <small id='VMP4w'></small><noframes id='VMP4w'>