C++ URL 编码与解码

以下为 C++ 语言中的 URL 编码和解码代码:

URL 编码

std::string urlencode(const std::string& str) {
  std::string encoded;
  for (auto c : str) {
    if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
      encoded.push_back(c);
    } else {
      encoded.append("%" + hex << setw int="">(c));
    }
  }
  return encoded;
}

URL 解码

std::string urldecode(const std::string& str) {
  std::string decoded;
  for (int i = 0; i < str xss=removed xss=removed xss=removed xss=removed>= '0' && str[j] <= '9') {
          c += str[j] - '0';
        } else if (str[j] >= 'a' && str[j] <= 'f') {
          c += str[j] - 'a' + 10;
        } else if (str[j] >= 'A' && str[j] <= 'F') {
          c += str[j] - 'A' + 10;
        }
      }
      i += 2;
      decoded.push_back(c);
    } else {
      decoded.push_back(str[i]);
    }
  }
  return decoded;
}
cpp 文件大小:5.32KB