下面是一个可以将字符串112233转换为0x11 0x22 0x33的C语言函数:

#include <stdio.h>
#include <string.h>

void convertToHex(const char* str) {
    int len = strlen(str);
    if (len % 2 != 0) {
        printf("Invalid input!\n");
        return;
    }
    
    for (int i = 0; i < len; i += 2) {
        char byte[3];
        byte[0] = str[i];
        byte[1] = str[i + 1];
        byte[2] = '\0';
        
        int hex;
        sscanf(byte, "%x", &hex);
        printf("0x%02X ", hex);
    }
    printf("\n");
}

int main() {
    const char* str = "112233";
    convertToHex(str);
    return 0;
}

运行上述代码会输出结果:0x11 0x22 0x3

帮我写个C语言函数可以将字符串112233转换为0x11 0x22 0x33

原文地址: http://www.cveoy.top/t/topic/iy9c 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录