C语言字符数组如何转成16进制 C语言如何把11位16进制字符串转成16进制数?
c\u8bed\u8a00\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5e76\u5b58\u50a8\u5230\u6570\u7ec4\u4e2dC\u8bed\u8a00\u5b57\u7b26\u4e32\u6570\u7ec4\u672c\u8eab\u5c31\u662f\u7528\u5341\u516d\u8fdb\u5236\u5b58\u50a8\u7684\uff0c\u53ea\u662f\u4f60\u770b\u5230\u7684\u90fd\u662f\u4e00\u4e2a\u4e2a\u5b57\u7b26\uff0c\u4f46\u8ba1\u7b97\u673a\u5b9e\u9645\u5b58\u50a8\u7684\u662f\u5b57\u7b26\u5bf9\u5e94\u7684ASCII\u7801\u3002\u5b57\u7b26\u4e32\u6570\u7ec4\u6700\u540e\u4e00\u4e2a\u5355\u5143\u7684\u5185\u5bb9\u662f'/0',\u5341\u516d\u8fdb\u5236\u5c31\u662f0x00\u3002
\u4f7f\u7528\u4e32\u53e3\u8f93\u51fa\u8f93\u51fa\u65f6\uff0c\u76f4\u63a5\u628a\u5b57\u7b26\u4e32\u9996\u5730\u5740str\u7ed9\u4e32\u53e3\uff0c\u6570\u7ec4\u957f\u5ea6\u7528strlen(str)+1\uff08+1\u662f\u8981\u628a\u7ed3\u5c3e/0\u8ba1\u7b97\u4e0a\uff09\uff0c\u6253\u5f00\u4e32\u53e3\u8c03\u8bd5\u5de5\u5177\uff0c\u7528\u6587\u672c\u663e\u793a\uff0c\u5c31\u53ef\u770b\u5230\u5b57\u7b26\u4e32\u6570\u7ec4\uff1b\u7528\u5341\u516d\u8fdb\u5236\u663e\u793a\uff0c\u53ef\u4ee5\u770b\u5230\u5b57\u7b26\u4e32\u7684\u5341\u516d\u8fdb\u5236ASCII\u7801
#include
#include
voidmain()
{
char*p="0x1b";
char*str;
longi=strtol(p,&str,16);
printf("%d\r\n",i);
}
//\u8f93\u51fa\u503c\u4e3a27
\u53c2\u6570base\u4ee3\u8868\u91c7\u7528\u7684\u8fdb\u5236\u65b9\u5f0f\uff0c\u5982base\u503c\u4e3a10\u5219\u91c7\u752810\u8fdb\u5236\uff0c\u82e5base\u503c\u4e3a16\u5219\u91c7\u752816\u8fdb\u5236\u7b49\u3002\u5f53base\u503c\u4e3a0\u65f6\u5219\u662f\u91c7\u752810\u8fdb\u5236\u505a\u8f6c\u6362\uff0c\u4f46\u9047\u5230\u5982\u20190x\u2019\u524d\u7f6e\u5b57\u7b26\u5219\u4f1a\u4f7f\u752816\u8fdb\u5236\u505a\u8f6c\u6362\u3001\u9047\u5230\u20190\u2019\u524d\u7f6e\u5b57\u7b26\u800c\u4e0d\u662f\u20190x\u2019\u7684\u65f6\u5019\u4f1a\u4f7f\u75288\u8fdb\u5236\u505a\u8f6c\u6362\u3002
\u6269\u5c55\u8d44\u6599
C\u8bed\u8a00\u5341\u8fdb\u5236\u8f6c2\uff5e16\u8fdb\u5236
#include
voidtrans(intdata,intbase)
{
intx[64],k,i=0;
while(data)
{
x[i]=data%base;
data/=base;
i++;
}
for(k=i-1;k>=0;k--)
if(x[k]<10)
printf("%d",x[k]);
elseif(x[k]<=16)
printf("%c",'A'+x[k]-10);
}
intmain(void)
{
intnum,base;
scanf("%d%d",&num,&base);
if(base16)
{
printf("dataerror\n");
return;
}
printf("number=%dbase=%d\n",num,base);
trans(num,base);
printf("\n");
return0;
}
需要准备的材料分别有:电脑、C语言编译器。
1、首先,打开C语言编译器,新建一个初始.cpp文件,例如:test.cpp。
2、在test.cpp文件中,输入C语言代码:
char *pKey = "AA1F1905A8F3E4D3C3C536FE615C93BC";
for (int i = 0; i < strlen(pKey) - 1; i += 2) {
printf("0x%c%c
", pKey[i], pKey[i+1]);
}
3、编译器运行test.cpp文件,此时成功将字符串数组转为了16进制的形式进行了输出。
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(void)
{
char *pKey = "AA1F1905A8F3E4D3C3C536FE615C93BC";
int n=strlen(pKey)/2,i;
char *Key=(char *)malloc(sizeof(char)*n);
for(i=0;i<n;++i)
{
sscanf(pKey+2*i,"%2X",Key+i);
}
for(i=0;i<n;++i)
{
printf("%#02hhX ",Key[i]);
}
return 0;
}
绛旓細1銆侀鍏堬紝鎵撳紑C璇█缂栬瘧鍣紝鏂板缓涓涓垵濮.cpp鏂囦欢锛屼緥濡傦細test.cpp銆2銆佸湪test.cpp鏂囦欢涓紝杈撳叆C璇█浠g爜锛歝har *pKey = "AA1F1905A8F3E4D3C3C536FE615C93BC";for (int i = 0; i < strlen(pKey) - 1; i += 2) { printf("0x%c%c\n", pKey[i], pKey[i+1]);} 3銆佺紪璇戝櫒杩...
绛旓細main(){ char a[]={'0','0','a','2','3','2','8','e'};int n=0,i;for(i=0;i<sizeof(a);i++){ if(a[i]>='0'&&a[i]<='9'){ n=n*16+a[i]-'0';} else if(a[i]>='A'&&a[i]<='F'){ n=n*16+a[i]-'A'+10;} else if(a[i]>='a'&&a[i...
绛旓細char* str = "鎴戜滑";char t[20];sprintf(t, "%X %X %X %X\n", str[0]&0xff,str[1]&0xff,str[2]&0xff,str[3]&0xff);
绛旓細鍙互鍒╃敤C璇█灏瀛楃杞寲鎴16杩涘埗鏁存暟锛岃繖灏遍渶瑕佷簡瑙e畠浠箣闂寸殑杞寲瑙勫垯锛岃嚜宸卞埗瀹氫竴涓皬绋嬪簭灏辫兘瀹炵幇涓閿浆鍖
绛旓細浠g爜:awk -v string=ABC 'BEGIN{split(string,array,"");print array[1],array[2],array[3]}'
绛旓細涓昏鏈変袱涓柟娉曪紝鍏跺疄閮芥槸瀵圭幇鏈夊嚱鏁扮殑浣跨敤锛氥1銆佹柟娉1: sscanf()鍑芥暟鍚: sscanf鍔 鑳: 浠瀛楃涓鏍煎紡鍖栬緭鍏ョ敤 娉: int sscanf(char *string, char *format[,argument,...]);浠ヤ笂鐨 format 涓 %x 灏辨槸灏嗗瓧绗︿覆鏍煎紡鍖涓 16 杩涘埗鏁颁緥瀛愶細include <stdio.h>void main(){char* p = "0x1a...
绛旓細灏嗕綘瑕佽浆鍖栫殑鏁板间緥濡00010001浠庡彸寮濮嬫暟,4浣嶆暟涓娆,姣4浣杞寲鎴16杩涘埗鐨瀛楃灏卞彲浠ヤ簡.4涓鸿浆16杩涘埗鍙互浣跨敤绌蜂妇娉,0001=1,0010=2,...1001=9,1010=A,...,1111=F 鎬诲叡鍐16涓氨OK浜!
绛旓細scr:婧瀛楃涓 鎴戝洖鍘昏瘯浜嗕竴涓嬶紝涓嶈 涓鑸殑sprintf(*dst,"%s"锛*src)鑳芥妸src杞崲鎴鎸囧畾鐨勬牸寮 鍙槸sprintf(*dst,"%x"锛*src)鏃讹紝*dst瀛樹簡鍦板潃锛伮蜂慨鏀逛簡涓涓嬶紝浠ヤ笅鏄緥瀛愶紝鏈夋晥鏋滀簡锛歩nclude "stdafx.h"include<stdio.h> void main(){ char a[9]="a1234",b[20], *p=a;int j=0;w...
绛旓細voidmain(){ char*p="0x1b";char*str;longi=strtol(p,&str,16);printf("%d\r\n",i);} //杈撳嚭鍊间负27 鍙傛暟base浠h〃閲囩敤鐨勮繘鍒舵柟寮忥紝濡俠ase鍊间负10鍒欓噰鐢10杩涘埗锛岃嫢base鍊涓16鍒欓噰鐢16杩涘埗绛夈傚綋base鍊间负0鏃跺垯鏄噰鐢10杩涘埗鍋杞崲锛屼絾閬囧埌濡傗0x鈥欏墠缃瀛楃鍒欎細浣跨敤16杩涘埗鍋氳浆鎹侀亣鍒扳0鈥...
绛旓細include <stdio.h> include <stdlib.h> void main(){ char buff[]="123456789abcdefghijk";int len = sizeof(buff);for(int i=0;i<len-1;i++)printf("%x ",buff[i]);}