字符串中小写字母转换为大写
void LowerToCap(char *str,char len)
{
char i;
for(i=0;i<len;i++)
{
if((96<str[i])&&(str[i]<123)) //小写字母
str[i]=str[i]-32; //转换为大写
}
}
int main(int argc, char *argv[])
{
char a[30]="asdddc";
LowerToCap(a,strlen(a));
printf("转换的值为:%s\n",a);
return 0;
}
解析命令
#include <stdio.h>
#include <string.h>
//用于命令解析用的命令值
#define LED1ON 1
#define LED1OFF 2
#define BEEPON 3
#define BEEPOFF 4
#define COMMANDERR 0XFF
char CommandProcess(char *str)
{
char CommandValue=COMMANDERR;
if(strcmp((char*)str,"LED1ON")==0) CommandValue=LED1ON;
else if(strcmp((char*)str,"LED1OFF")==0) CommandValue=LED1OFF;
else if(strcmp((char*)str,"BEEPON")==0) CommandValue=BEEPON;
else if(strcmp((char*)str,"BEEPOFF")==0) CommandValue=BEEPOFF;
return CommandValue;
}
int main(int argc, char *argv[])
{
char b=0;
b=CommandProcess("LED1ON");
printf("转换的值为:%d\n",b);
return 0;
}
strcmp(const char *s1,const char * s2)
1:这里面只能比较字符串
2:当两个字符串完全相同的时候返回一个0
找出第几位是1
#include <stdio.h>
//找出i个位进行寻找,找出其中所有1所在的位置并打印出来
int main(int argc, char *argv[])
{
//a是1的位置,temp要测得数
int temp = 0x14, a = 0,dat=0;//0001 0100
for (int i = 0; i < 8; i++)//这个i只是在这个for语句中起作用,出了for就相当于没有定义i
{
dat = temp>>i;
if ((dat & 0x01)==1)
{
a = i+1;
printf("%d\n", a);
}
}
return 0;
}
数据类型之间的转换
.c
#include "core_string.h"
//字符串转无符号整形
int32_t core_str2uint(char *input, uint8_t input_len, uint32_t *output)
{
uint8_t index = 0;
uint32_t temp = 0;
for (index = 0; index < input_len; index++)
{
if (input[index] < '0' || input[index] > '9')
{
return STATE_USER_INPUT_OUT_RANGE;
}
temp = temp * 10 + input[index] - '0';
}
*output = temp;
return STATE_SUCCESS;
}
int32_t core_str2uint64(char *input, uint8_t input_len, uint64_t *output)
{
uint8_t index = 0;
uint64_t temp = 0;
for (index = 0; index < input_len; index++) {
if (input[index] < '0' || input[index] > '9') {
return STATE_USER_INPUT_OUT_RANGE;
}
temp = temp * 10 + input[index] - '0';
}
*output = temp;
return STATE_SUCCESS;
}
int32_t core_uint2str(uint32_t input, char *output, uint8_t *output_len)
{
uint8_t i = 0, j = 0;
char temp[10] = {
0};
do {
temp[i++] = input % 10 + '0';
} while ((input /= 10) > 0);
do {
output[--i] = temp[j++];
} while (i > 0);
if (output_len) {
*output_len = j;
}
return STATE_SUCCESS;
}
int32_t core_uint642str(uint64_t input, char *output, uint8_t *output_len)
{
uint8_t i = 0, j = 0;
char temp[20] = {
0};
do {
temp[i++] = input % 10 + '0';
} while ((input /= 10) > 0);
do {
output[--i] = temp[j++];
} while (i > 0);
if (output_len) {
*output_len = j;
}
return STATE_SUCCESS;
}
int32_t core_int2str(int32_t input, char *output, uint8_t *output_len)
{
uint8_t i = 0, j = 0, minus = 0;
char temp[11] = {
0};
int64_t in = input;
if (in < 0) {
minus = 1;
in = -in;
}
do {
temp[minus + (i++)] = in % 10 + '0';
} while ((in /= 10) > 0);
do {
output[minus + (--i)] = temp[minus + (j++)];
} while (i > 0);
if (minus == 1) {
output[0] = '-';
}
if (output_len) {
*output_len = j + minus;
}
return STATE_SUCCESS;
}
int32_t core_hex2str(uint8_t *input, uint32_t input_len, char *output, uint8_t lowercase)
{
char *upper = "0123456789ABCDEF";
char *lower = "0123456789abcdef";
char *encode = upper;
int i = 0, j = 0;
if (lowercase) {
encode = lower;
}
for (i = 0; i < input_len; i++) {
output[j++] = encode[(input[i] >> 4) & 0xf];
output[j++] = encode[(input[i