一、字元串轉16進位
字元串轉16進位是指將字元串表示的數據轉換成16進位的數據形式,方便數據的傳輸和存儲。在c++中,可以使用std::stringstream和std::hex實現字元串轉16進位。
std::string str = "hello";
std::stringstream ss;
ss << std::hex;
for (size_t i = 0; i < str.length(); ++i) {
ss << std::setw(2) << std::setfill('0') << static_cast(str[i]);
}
std::string hex_str = ss.str(); // hex_str: "68656c6c6f"
在上面的代碼實現中,首先定義了一個字元串str,接著使用std::stringstream將數據以std::hex的格式輸出,然後循環遍歷字元串中的每一個字元,使用std::setw()和std::setfill()設置每個字元的輸出寬度和填充字元(‘0’),最後將轉換後的16進位字元串存儲到變數hex_str中。
二、c字元串轉16進位
c字元串是指以’\0’結尾的一段字元數組,將其轉換成16進位時需要注意字元串的結尾字元不需要轉換。
char c_str[] = "hello";
std::string hex_str;
for (size_t i = 0; i < strlen(c_str); ++i) {
std::stringstream ss;
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast(c_str[i]);
hex_str += ss.str();
}
// hex_str: "68656c6c6f"
在此示例中,定義了一個c字元串c_str,然後將每個字元轉換成16進位並存儲到空的字元串hex_str中。
三、字元串轉16進位 c
c語言中的字元串需要注意字符集的問題,以及需要充分使用指針操作來遍歷字元串,進行字元轉換。
#include
#include
#include
void str_to_hex(char* str, char* hex_str) {
int i;
int size = strlen(str);
for (i = 0; i < size; ++i) {
sprintf(&hex_str[i*2], "%02X", str[i]);
}
}
int main() {
char* str = "hello";
char hex_str[13];
str_to_hex(str, hex_str);
printf("%s", hex_str); // "68656C6C6F"
return 0;
}
該示例中,定義了一個字元串str,然後使用函數str_to_hex()將該字元串轉換成16進位字元串,最後輸出結果。函數sprintf()可以將格式化的字元串存儲到指定位置,其中的格式化字元串”%02X”即表示2位16進位數,不足位數使用’0’補齊。
四、c字元串轉16進位數字
c字元串也可以轉換成16進位數字,可以利用字元串分離成單個字元,然後分別進行轉換。
#include
#include
#include
int main() {
char* str = "hello";
int i, size = strlen(str);
for (i = 0; i < size; ++i) {
printf("%02X", str[i]);
}
return 0;
}
在該示例中,c字元串str轉換成了16進位數字,並使用printf()輸出結果。
五、字元串轉16進位在線工具
使用在線工具可以快速地將字元串轉換成16進位表示。
實例網址:https://www.jacksondunstan.com/articles/4079
六、字元串轉16進位函數
將字元串轉換成16進位可以封裝成函數,方便調用。
std::string str2hex(std::string str) {
std::stringstream ss;
ss << std::hex;
for (size_t i = 0; i < str.length(); ++i) {
ss << std::setw(2) << std::setfill('0') << static_cast(str[i]);
}
return ss.str();
}
int main() {
std::string str = "hello";
std::string hex_str = str2hex(str);
// hex_str: "68656c6c6f"
return 0;
}
在該示例中,定義了一個函數str2hex(),使用std::stringstream和其它函數將字元串轉換成16進位,然後返回結果。
七、字元串轉16進位byte數組
16進位byte數組和16進位字元串可以互相轉換。
std::string str = "hello";
std::vector bytes(str.begin(), str.end());
std::string hex_str(bytes.size() * 2, ' ');
auto table = "0123456789ABCDEF";
for (size_t i = 0; i > 4];
hex_str[i*2+1] = table[bytes[i] & 0x0f];
}
// hex_str: "68656C6C6F"
該示例中使用std::vector存儲每個字元的byte值,使用字元數組將byte值轉換成16進位數,並拼接成16進位字元串。
八、字元串轉16進位字元串
字元串可以直接轉換成16進位字元串,例如使用iostream。
#include
#include
std::string str = "hello";
std::ostringstream oss;
for (size_t i = 0; i < str.length(); ++i) {
oss << std::hex << std::setfill('0') << std::setw(2) << (int) str[i];
}
std::string hex_str = oss.str(); // hex_str: "68656c6c6f"
該示例中使用了std::ostringstream作為輸出緩存,分別使用std::hex和其他函數轉換成16進位數,然後使用stream對象輸出結果。
九、c將字元串轉換成16進位
c語言中也可以使用內置函數將字元串轉換成16進位。
#include
#include
#include
int main() {
char* str = "hello";
char hex_str[13];
int i;
for (i = 0; i < strlen(str); i++) {
sprintf(hex_str+i*2, "%02X", str[i]);
}
printf("%s", hex_str); // hex_str: "68656C6C6F"
return 0;
}
在該示例中,使用了函數sprintf()將轉換後的結果存儲到變數hex_str中,並輸出結果。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/307308.html
微信掃一掃
支付寶掃一掃