一、字符串轉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-hk/n/307308.html