糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > printf格式化输出几点注记

printf格式化输出几点注记

时间:2021-01-05 16:58:53

相关推荐

printf格式化输出几点注记

搞了很多年c/c++,有很多细小的东西,曾经不止一次遇到,可是一直都是放在零散的地方,要用的时候怎么也找不到,今天,我痛下决心,改掉不良习惯,把这些经验或是tips记录在这里,便于日后查找。

1.在统计网络下载信息时,如何表达文件大小?

下面是输出结果

2.打印size_t类型数据的长度,使用%lu。

下面是一个使用OpenSSL中HMAC-Sha1算法计算加密字符串的例子,记录如下。

//gcc -g hmac_sha1_demo1.c -o hmac_sha1_demo1 -lcrypto --std=c99#include <stdio.h>#include <string.h>#include <openssl/hmac.h>int main(){// The key to hashchar key[] = "012345678";// The data that we're going to hash using HMACchar data[] = "hello world";unsigned char* digest;// Using sha1 hash engine here.// You may use other hash engines. e.g EVP_md5(), EVP_sha224, EVP_sha512, etcdigest = HMAC(EVP_sha1(), key, strlen(key), (unsigned char*)data, strlen(data), NULL, NULL);printf("%s, len %lu\n", digest, strlen(digest));// Be careful of the length of string with the choosen hash engine. SHA1 produces a 20-byte hash value which rendered as 40 characters.// Change the length accordingly with your choosen hash enginechar mdString[41] = {'\0'};for(int i = 0; i < 20; i++)sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);printf("HMAC digest: %s\n", mdString);return 0;}

运行结果

3.打印time_t类型的数据,使用%lu,参见下面的小例子:时间字符串转换为时间戳

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>time_t str2time(const char* s){struct tm tm_time;strptime(s, "%Y%m%d%H%M%S", &tm_time);time_t p = mktime(&tm_time);return p;}int main(int argc, char* argv[]){const char* str = "0622193125";time_t timep;timep = str2time(str);printf("%lu\n", timep);time(&timep);printf("%lu\n", timep);return 0;}

下面是运行结果:

如果觉得《printf格式化输出几点注记》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。