c語言把16進位制文字轉16進位制陣列
阿新 • • 發佈:2022-04-01
直接上程式碼
#include<stdio.h> #include<stdlib.h> #include<string.h> void str2hex(unsigned char* value,int len,unsigned char* out); int main(){ unsigned char *str = "81709944E0CB2E1DB5B0A477D18539B"; unsigned char out[16]; str2hex(str,32,out); int i; /* for ( i = 0; i < 32; i++) { printf("%02x ",str[i]); } printf("\n"); */ for ( i = 0; i < 16; i++) { printf("%02x ",out[i]); } printf("\n"); } void str2hex(unsigned char* value,int len,unsigned char* out){ int i; int j = 0; unsigned int HIGH_BYTE; unsigned int LOW_BYTE; for ( i = 0; i < len; i+=2) { //high 4 if (value[i]>=0x30 && value[i]<=0x39){ //printf("high1 = %d \n",value[i]-0x30); //printf("%d \n",value[i]-0x30); HIGH_BYTE = value[i]-0x30; }else if (value[i]>=0x41 && value[i]<=0x46) { //printf("high2 = %d \n",value[i]); // printf("%d ",value[i]-0x37); HIGH_BYTE = value[i]-0x37; }else if (value[i]>=0x61 && value[i]<=0x66) { //printf("high3 = %d \n",value[i]); // printf("%d ",value[i]-0x57); HIGH_BYTE = value[i]-0x57; } //low 4 if (value[i+1]>=0x30 && value[i+1]<=0x39){ // printf("%d \n",value[i+1]-0x30); LOW_BYTE = value[i+1]-0x30; }else if (value[i+1]>=0x41 && value[i+1]<=0x46) { // printf("%d ",value[i+1]-0x37); LOW_BYTE = value[i+1]-0x37; }else if (value[i+1]>=0x61 && value[i+1]<=0x66) { // printf("%d ",value[i+1]-0x57); LOW_BYTE = value[i+1]-0x57; } out[j] = (HIGH_BYTE<<4) + LOW_BYTE; j++; } }
執行結果
執行平臺 https://c.runoob.com/compile/11/
轉載地址https://blog.51cto.com/u_15361941/3835298