char a[]={'1','A','B','5'}
那么如何将其转换为对应的十六进制:
1H,AH,BH,5H
13 个解决方案
#1
int char2hex(const char ch)
{
if('0'<=ch && ch<='9') return ch-'0';
return ch-'A'+10;
}
#2
#include <cstdio>
#include <cctype>
int main()
{
char a[]={'1','A','B','5','c'};
for(int i=0; i<sizeof(a)/sizeof(a[0]); ++i) {
char c = a[i];
printf("%X ", c-(isdigit(c) ? '0' : (isupper(c) ? ('A'-16) : ('a'-16))));
}
}
#3
如果是一个字符,1L足矣
#4
首先转换成十进制,其次从十进制转换成16进制
#5
要是两个字符呢?
#6
比如是'1A',要转换成1AH,如何?
#7
说错了!
比如是"1A",要转换成1AH,如何?
#8
学习
#9
/* strtol example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
char * pEnd;
long int li1, li2, li3, li4;
li1 = strtol (szNumbers,&pEnd,10);
li2 = strtol (pEnd,&pEnd,16);
li3 = strtol (pEnd,&pEnd,2);
li4 = strtol (pEnd,NULL,0);
printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
return 0;
}
#10
你是想直接在字符后加H表示16进制,还是把字符转换成16进制?
#11
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned int n;
char str[]="1a",s[10];
sscanf(str,"%x",&n);//转化成16进制值
printf("%x %u\n",n,n);
snprintf(s,sizeof(str)+1,"%XH",n);//转化成带H形式
//puts(s);
getchar();
return 0;
}
#include <stdlib.h>
int main(void)
{
unsigned int n;
char str[]="1a",s[10];
sscanf(str,"%x",&n);//转化成16进制值
printf("%x %u\n",n,n);
snprintf(s,sizeof(str)+1,"%XH",n);//转化成带H形式
//puts(s);
getchar();
return 0;
}
#12
查表法。
#13
int char2hex(const char ch)
{
if('0'<=ch && ch<='9')
return ch-'0';
else
return ch-'A'+10;
}
{
if('0'<=ch && ch<='9')
return ch-'0';
else
return ch-'A'+10;
}
#1
int char2hex(const char ch)
{
if('0'<=ch && ch<='9') return ch-'0';
return ch-'A'+10;
}
#2
#include <cstdio>
#include <cctype>
int main()
{
char a[]={'1','A','B','5','c'};
for(int i=0; i<sizeof(a)/sizeof(a[0]); ++i) {
char c = a[i];
printf("%X ", c-(isdigit(c) ? '0' : (isupper(c) ? ('A'-16) : ('a'-16))));
}
}
#3
如果是一个字符,1L足矣
#4
首先转换成十进制,其次从十进制转换成16进制
#5
要是两个字符呢?
#6
比如是'1A',要转换成1AH,如何?
#7
说错了!
比如是"1A",要转换成1AH,如何?
#8
学习
#9
/* strtol example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
char * pEnd;
long int li1, li2, li3, li4;
li1 = strtol (szNumbers,&pEnd,10);
li2 = strtol (pEnd,&pEnd,16);
li3 = strtol (pEnd,&pEnd,2);
li4 = strtol (pEnd,NULL,0);
printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
return 0;
}
#10
你是想直接在字符后加H表示16进制,还是把字符转换成16进制?
#11
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned int n;
char str[]="1a",s[10];
sscanf(str,"%x",&n);//转化成16进制值
printf("%x %u\n",n,n);
snprintf(s,sizeof(str)+1,"%XH",n);//转化成带H形式
//puts(s);
getchar();
return 0;
}
#include <stdlib.h>
int main(void)
{
unsigned int n;
char str[]="1a",s[10];
sscanf(str,"%x",&n);//转化成16进制值
printf("%x %u\n",n,n);
snprintf(s,sizeof(str)+1,"%XH",n);//转化成带H形式
//puts(s);
getchar();
return 0;
}
#12
查表法。
#13
int char2hex(const char ch)
{
if('0'<=ch && ch<='9')
return ch-'0';
else
return ch-'A'+10;
}
{
if('0'<=ch && ch<='9')
return ch-'0';
else
return ch-'A'+10;
}