溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++中string庫(kù)對(duì)字符串的URL編碼/解碼

發(fā)布時(shí)間:2024-10-09 18:37:16 來(lái)源:億速云 閱讀:78 作者:小樊 欄目:編程語(yǔ)言

在 C++ 中,可以使用標(biāo)準(zhǔn)庫(kù)中的 <string><codecvt> 頭文件來(lái)實(shí)現(xiàn)字符串的 URL 編碼和解碼。具體實(shí)現(xiàn)方式如下:

URL 編碼

可以使用 std::url_encode 函數(shù)將字符串轉(zhuǎn)換為 URL 編碼格式。該函數(shù)接受兩個(gè)參數(shù),第一個(gè)參數(shù)為需要編碼的字符串,第二個(gè)參數(shù)為編碼后的字符串。函數(shù)返回值為編碼后的字符串。

示例代碼:

#include <iostream>
#include <string>
#include <codecvt>
#include <url>

int main() {
    std::string str = "Hello, World!";
    std::string encoded_str = std::url_encode(str);
    std::cout << "Encoded string: " << encoded_str << std::endl;
    return 0;
}

輸出結(jié)果:

Encoded string: Hello%2C+World%21

URL 解碼

可以使用 std::url_decode 函數(shù)將 URL 編碼格式的字符串轉(zhuǎn)換為正常字符串。該函數(shù)接受兩個(gè)參數(shù),第一個(gè)參數(shù)為需要解碼的字符串,第二個(gè)參數(shù)為解碼后的字符串。函數(shù)返回值為解碼后的字符串。

示例代碼:

#include <iostream>
#include <string>
#include <codecvt>
#include <url>

int main() {
    std::string encoded_str = "Hello%2C+World%21";
    std::string decoded_str = std::url_decode(encoded_str);
    std::cout << "Decoded string: " << decoded_str << std::endl;
    return 0;
}

輸出結(jié)果:

Decoded string: Hello, World!
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI