二进制是Binary,简写为B。
八进制是Octal,简写为O。
十进制为Decimal,简写为D。
十六进制为Hexadecimal,简写为H。
一、求二进制中1的个数:
int f(unsigned int n)
{
unsigned int number=0;
for(number=0;n;++number)
{
n&=(n-1); //每与一次就少一个1
}
return number; //number即为二进制中1的个数
}
二、求二进制中0的个数:
int f(unsigned int n)
{
unsigned int number = 0;
while(n != 0) {
if((n&1)==0)
number++;
n>>=1;
}
return number; //number即二进制中0的个数
}
三、返回数组中最大值的下标
#include <stdio.h>
int fun ( int * s,int n ) //n为指针s的长度
{
int i,k;
for ( i=0, k = i; i < n; i++)
if ( s[i] > s[k])
k = i;
return (k);
}
四、选择排序(降序)int a[]={4,0,2,3,1},i,j,t;
for(i=1;i<5;i++)
{
t=a[i];
j=i-1;
while(j>=0&&t>a[j])
{
a[j+1]=a[j];
--j;
}
a[j+1]=t;
}
五、将十六进制的字符串转化为二进制的字符串 int i = 1024; char bs[16],b[16]; sscanf(hs,"%x",&i); itoa(i,b,2); sprintf(bs,"%016s",b); printf("\nhs=%s,bs=%s\n",hs,bs); //hs为原始的十六进制,bs为转化完成的二进制字符串void itoa(unsigned long val,char *buf,unsigned radix) //buf为转化完成的二进制字符串
{
char *p; /* pointer to traverse string */
char *firstdig; /* pointer to first digit */
char temp; /* temp char */
unsigned digval; /* value of digit */
p = buf;
firstdig = p; /* save pointer to first digit */
do{
digval = (unsigned)(val%radix);
val /= radix; /* get next digit */
/* convert to ascii and store */
if(digval > 9)
*p++ = (char)(digval - 10 + 'a'); /* a letter */
else
*p++ = (char)(digval + '0'); /* a digit */
}while(val>0);/* We now have the digit of the number in the buffer, but in reverse order. Thus we reverse them now. */
*p-- = '\0'; /* terminate string; p points to last digit */
do{
temp = *p;
*p = *firstdig;
*firstdig = temp; /* swap *p and *firstdig */
--p;
++firstdig; /* advance to next two digits */
}while(firstdig < p); /* repeat until halfway */
}
六、字符串转化为十六进制 pbDest1为输出的十六进制 pbSrc1为输入的字符串/*// C prototype : void StrToHex(byte *pbDest, char *pszSrc, int nLen)// parameter(s): [OUT] pbDest - 输出缓冲区// [IN] pszSrc - 字符串// [IN] nLen - 16进制数的字节数(字符串的长度/2)// return value:// remarks : 将字符串转化为16进制数*/void StrToHex( char *pbDest1, char *pbSrc1, int nLen)
{
char h1,h2;
unsigned char s1,s2;
int i;
for (i=0; i<nLen; i++)
{
h1 = pbSrc1[2*i];
h2 = pbSrc1[2*i+1];
s1 = toupper(h1) - 0x30;
if (s1 > 9)
s1 -= 7;
s2 = toupper(h2) - 0x30;
if (s2 > 9)
s2 -= 7;
pbDest1[i] = s1*16 + s2;
}
}
七、十六进制转化为字符串 pbDest2为输出的字符串 pbSrc2为输入的十六进制/*// C prototype : void HexToStr(char *pszDest, byte *pbSrc, int nLen)// parameter(s): [OUT] pszDest - 存放目标字符串// [IN] pbSrc - 输入16进制数的起始地址// [IN] nLen - 16进制数的字节数// return value:// remarks : 将16进制数转化为字符串*/void HexToStr( char *pbDest2, unsigned char *pbSrc2, int nLen)
{
char ddl,ddh;
int i;
for (i=0; i<nLen; i++)
{
ddh = 48 + pbSrc2[i] / 16;
ddl = 48 + pbSrc2[i] % 16;
if (ddh > 57) ddh = ddh + 7;
if (ddl > 57) ddl = ddl + 7;
pbDest2[i*2] = ddh;
pbDest2[i*2+1] = ddl;
}
pbDest2[nLen*2] = '\0';
}
八、十六进制字符串转化为十进制数int tolower(int c)
{
if (c >= 'A' && c <= 'Z')
{
return c + 'a' - 'A';
}
else
{
return c;
}
}
int htoi(char s[])
{
int i;
int n = 0;
if (s[0] == '0' && (s[1]=='x' || s[1]=='X'))
{
i = 2;
}
else
{
i = 0;
}
for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z');++i)
{
if (tolower(s[i]) > '9')
{
n = 16 * n + (10 + tolower(s[i]) - 'a');
}
else
{
n = 16 * n + (tolower(s[i]) - '0');
}
}
return n;
}
来自为知笔记(Wiz)
标签:函数,16,int,unsigned,number,char,字符串,好用 From: https://www.cnblogs.com/lisongzzx/p/18283978