1.获取当前系统时间
源码
# include <stdio.h>
# include <stdint.h>
# include <sys/time.h>
# include <unistd.h>
uint64_t time_now ( void )
{
struct timeval lTime;
gettimeofday ( & lTime, 0 ) ;
return ( lTime. tv_sec * 1000 + lTime. tv_usec/ 1000 ) ;
}
int main ( )
{
uint64_t time_1 = time_now ( ) ;
usleep ( 1000 * 1000 ) ;
uint64_t time_2 = time_now ( ) ;
printf ( "time1:[%ld] time2:[%ld] interval:[%ld]\n" , time_1, time_2, time_2- time_1) ;
return 0 ;
}
运行结果
2.按位设置byte值
源码
# include <stdio.h>
# include <stdint.h>
int setBitValue ( uint8_t * data, int bit, int value)
{
if ( value> 1 || value< 0 || NULL == data || bit> 7 || bit< 0 )
{
return - 1 ;
}
if ( value == 0 )
{
* data &= ~ ( 0x1 << bit) ;
}
else {
* data |= ( 0x1 << bit) ;
}
return 0 ;
}
int main ( )
{
uint8_t data[ 2 ] = { 0x0 } ;
setBitValue ( & data[ 0 ] , 3 , 1 ) ;
setBitValue ( & data[ 0 ] , 4 , 1 ) ;
setBitValue ( & data[ 1 ] , 6 , 1 ) ;
setBitValue ( & data[ 1 ] , 2 , 1 ) ;
char result[ 16 ] = { 0x0 } ;
for ( int i = 0 ; i< 2 ; i++ )
{
printf ( "%x\n" , data[ i] ) ;
}
return 0 ;
}
运行结果
3.获取文件大小
源码
# include <stdio.h>
# include <sys/stat.h>
# include <string.h>
size_t get_file_size ( const char * filepath)
{
if ( NULL == filepath)
{
printf ( "File path is null...\n" ) ;
return 0 ;
}
struct stat filestat;
memset ( & filestat, 0 , sizeof ( struct stat ) ) ;
if ( 0 == stat ( filepath, & filestat) )
{
return filestat. st_size;
}
else {
printf ( "stat %s size failed...\n" , filepath) ;
return 0 ;
}
}
int main ( )
{
printf ( "%ld\n" , get_file_size ( "123.txt" ) ) ;
return 0 ;
}
运行结果
4.读取文件内容到buff
源码
# include <stdio.h>
# include <sys/stat.h>
# include <string.h>
# include <stdint.h>
# include <stdlib.h>
size_t get_file_size ( const char * filepath)
{
if ( NULL == filepath)
{
printf ( "File path is null...\n" ) ;
return 0 ;
}
struct stat filestat;
memset ( & filestat, 0 , sizeof ( struct stat ) ) ;
if ( 0 == stat ( filepath, & filestat) )
{
return filestat. st_size;
}
else {
printf ( "stat %s size failed...\n" , filepath) ;
return 0 ;
}
}
uint8_t * read_file_to_buf ( const char * filepath)
{
if ( NULL == filepath)
{
printf ( "File path is null...\n" ) ;
return NULL ;
}
size_t f_size = get_file_size ( filepath) ;
if ( 0 == f_size)
{
printf ( "File is empty...\n" ) ;
return NULL ;
}
uint8_t * buf = ( uint8_t * ) malloc ( f_size + 1 ) ;
if ( NULL == buf)
{
printf ( "Failed to malloc for file..." ) ;
return NULL ;
}
memset ( buf, 0 , f_size + 1 ) ;
FILE * fp = fopen ( filepath, "r" ) ;
if ( NULL == fp)
{
printf ( "Failed to open %s\n" , filepath) ;
return NULL ;
}
size_t readSize = fread ( buf, 1 , f_size, fp) ;
fclose ( fp) ;
if ( readSize != f_size)
{
free ( buf) ;
buf = NULL ;
return NULL ;
}
buf[ f_size] = 0 ;
return buf;
}
int main ( )
{
uint8_t * buff = NULL ;
buff = read_file_to_buf ( "123.txt" ) ;
printf ( "buff:%s\n" , buff) ;
free ( buff) ;
buff = NULL ;
return 0 ;
}
运行结果
5.将字符串转换成整数
源码
# include <stdio.h>
int my_atoi ( char * str)
{
int value = 0 ;
int sign = 1 ;
if ( * str == '-' )
{
sign = - 1 ;
str++ ;
}
while ( * str != '\0' )
{
if ( * str>= 48 && * str <= 57 )
{
value = value* 10 + ( * str - '0' ) ;
}
str++ ;
}
return value* sign;
}
int main ( )
{
char str_1[ 16 ] = "112233" ;
printf ( "str1:%d\n" , my_atoi ( str_1) ) ;
char str_2[ 16 ] = "11ks22ll33" ;
printf ( "str2:%d\n" , my_atoi ( str_2) ) ;
char str_3[ 16 ] = "-11ks22ll33" ;
printf ( "str3:%d\n" , my_atoi ( str_3) ) ;
return 0 ;
}
运行结果
6.字符串分割
源码
# include <stdio.h>
# include <string.h>
int my_atoi ( char * str)
{
int value = 0 ;
int sign = 1 ;
if ( * str == '-' )
{
sign = - 1 ;
str++ ;
}
while ( * str != '\0' )
{
if ( * str>= 48 && * str <= 57 )
{
value = value* 10 + ( * str - '0' ) ;
}
str++ ;
}
return value* sign;
}
int my_strtok ( char * str, char sep, int * output)
{
char token[ 8 ] = { 0x0 } ;
int index = 0 ;
int i = 0 ;
printf ( "src_str:%s\n" , str) ;
while ( * str != '\0' )
{
if ( * str != sep)
{
token[ index++ ] = * str;
}
else {
output[ i++ ] = my_atoi ( token) ;
index = 0 ;
}
str++ ;
}
output[ i] = my_atoi ( token) ;
return 0 ;
}
int main ( )
{
char str0[ 40 ] = "1>2>3" ;
char str1[ 40 ] = "11-22-33" ;
char str2[ 40 ] = "-111>222>333" ;
int output[ 3 ] = { 0x0 } ;
my_strtok ( str0, '>' , output) ;
for ( int i = 0 ; i< 3 ; i++ )
{
printf ( "output[%d]=%d " , i, output[ i] ) ;
}
printf ( "\n" ) ;
bzero ( output, sizeof ( output) ) ;
my_strtok ( str1, '-' , output) ;
for ( int i = 0 ; i< 3 ; i++ )
{
printf ( "output[%d]=%d " , i, output[ i] ) ;
}
printf ( "\n" ) ;
bzero ( output, sizeof ( output) ) ;
my_strtok ( str2, '>' , output) ;
for ( int i = 0 ; i< 3 ; i++ )
{
printf ( "output[%d]=%d " , i, output[ i] ) ;
}
printf ( "\n" ) ;
return 0 ;
}
运行结果
7.模块初始化全局变量
源码
# include <stdio.h>
struct _model_test_globalvariables {
int a;
int b;
int c;
_model_test_globalvariables ( ) : a ( 3 ) , b ( 4 ) , c ( 5 ) { } ;
} test_gv;
int main ( )
{
printf ( "a=%d b=%d c=%d\n" , test_gv. a, test_gv. b, test_gv. c) ;
}
运行结果
8.switch常规用法
源码
# include <stdio.h>
void test_auto_switch ( int status)
{
switch ( status)
{
case 1 :
printf ( "input status:%d This is test 1\n" , status) ;
case 2 :
printf ( "input status:%d This is test 2\n" , status) ;
default :
printf ( "input status:%d This is test default\n" , status) ;
break ;
}
}
void test_value_range ( int value)
{
switch ( value)
{
case 1 . . . 100 :
printf ( "input value:%d Range 1 ~ 100\n" , value) ;
break ;
case 101 . . . 200 :
printf ( "input value:%d Range 101 ~ 200\n" , value) ;
break ;
case 201 . . . 300 :
printf ( "input value:%d Range 201 ~ 300\n" , value) ;
break ;
default :
break ;
}
}
int main ( )
{
test_auto_switch ( 1 ) ;
test_auto_switch ( 2 ) ;
test_value_range ( 155 ) ;
test_value_range ( 219 ) ;
test_value_range ( 100 ) ;
test_value_range ( 101 ) ;
return 0 ;
}
运行结果
9.获取数据或文件的md5值
源码
# include <stdio.h>
# include <stdint.h>
# include <string.h>
# define MD5_FILE_NAME "md5.txt"
# define GENERATE_MD5_CMD "md5sum md5.txt"
# define DELETE_MD5_CMD "rm -f md5.txt"
uint8_t payload[ 5 ] = { 0x31 , 0x32 , 0x33 , 0x34 , 0x35 } ;
int get_data_md5_value ( uint8_t * val, uint32_t nval, char * out)
{
FILE* fp = fopen ( MD5_FILE_NAME, "w" ) ;
if ( NULL == fp)
{
printf ( "Failed to open %s.\n" , MD5_FILE_NAME) ;
return - 1 ;
}
size_t ret_size = fwrite ( val, 1 , nval, fp) ;
printf ( "Write %ld byte to %s.\n" , ret_size, MD5_FILE_NAME) ;
fclose ( fp) ;
FILE* pp = popen ( GENERATE_MD5_CMD, "r" ) ;
if ( NULL == pp)
{
printf ( "Failed to exec %s.\n" , GENERATE_MD5_CMD) ;
return - 2 ;
}
char buf[ 1024 ] = { 0x0 } ;
while ( NULL != fgets ( buf, sizeof ( buf) , pp) )
{
printf ( "%s" , buf) ;
}
memcpy ( out, buf, 32 ) ;
pclose ( pp) ;
# if 0
FILE* dp = popen ( DELETE_MD5_CMD, "r" ) ;
if ( NULL == dp)
{
printf ( "Failed to exec %s.\n" , DELETE_MD5_CMD) ;
return - 3 ;
}
pclose ( dp) ;
# endif
return 0 ;
}
int main ( )
{
char md5_str[ 64 ] = { 0x0 } ;
int ret = get_data_md5_value ( payload, sizeof ( payload) , md5_str) ;
if ( ret == 0 )
{
printf ( "md5 str:[%s]\n" , md5_str) ;
}
return 0 ;
}
执行结果
10.将hexString转成hexByteArray
源码
# include <stdio.h>
# include <stdint.h>
# include <string.h>
char hexString[ ] = "827ccb0eea8a706c4c34a16891f84e7b" ;
uint8_t hexString2HexArray ( char * str, uint8_t * out)
{
int index = 0 ;
int str_len = strlen ( str) ;
if ( str_len % 2 != 0 )
{
printf ( "It is not a std hex string\n" ) ;
}
char * p = str;
uint8_t high = 0x0 ;
uint8_t low = 0x0 ;
for ( index = 0 ; index< str_len/ 2 ; index++ )
{
high = ( * p > '9' && * p <= 'f' ) ? * p - 55 : * p - 48 ;
p++ ;
low = ( * p > '9' && * p <= 'f' ) ? * p - 55 : * p - 48 ;
out[ index] = ( ( high & 0xf ) << 4 | ( low & 0xf ) ) ;
printf ( "high = %02x low = %02x out = %02x \n" , high, low, out[ index] ) ;
p++ ;
}
if ( str_len % 2 == 1 )
{
out[ index] = ( * p > '9' && * p <= 'f' ) ? * p - 55 : * p - 48 ;
}
return str_len/ 2 + str_len % 2 ;
}
int main ( )
{
uint8_t md5_value[ 16 ] = { 0x0 } ;
int len = hexString2HexArray ( hexString, md5_value) ;
printf ( "input str:%s\n" , hexString) ;
for ( int i= 0 ; i< len; i++ )
{
printf ( "0x%02x " , md5_value[ i] ) ;
}
printf ( "\n" ) ;
return 0 ;
}
执行结果
11.时间戳和时间字符串相互转化
源码
# include <stdio.h>
# include <time.h>
int convert_string_to_time ( )
{
struct tm date = { 0 } ;
date. tm_year = 2024 - 1900 ;
date. tm_mon = 5 - 1 ;
date. tm_mday = 10 ;
date. tm_hour = 16 ;
date. tm_min = 20 ;
date. tm_sec = 50 ;
time_t now = mktime ( & date) ;
printf ( "time:%ld\n" , now) ;
return 0 ;
}
int convert_time_to_string ( )
{
time_t now = time ( NULL ) ;
printf ( "time_now:%ld\n" , now) ;
struct tm * tm_local = localtime ( & now) ;
static char date[ 32 ] ;
# if 1
sprintf ( date, "%04d/%02d/%02d %02d:%02d:%02d" ,
tm_local-> tm_year + 1900 ,
tm_local-> tm_mon + 1 ,
tm_local-> tm_mday,
tm_local-> tm_hour,
tm_local-> tm_min,
tm_local-> tm_sec) ;
# else
strftime ( date, sizeof ( date) , "%Y/%m/%d %H:%M:%S" , tm_local) ;
# endif
printf ( "Date: %s\n" , date) ;
return 0 ;
}
int main ( )
{
convert_time_to_string ( ) ;
convert_string_to_time ( ) ;
return 0 ;
}
运行结果