C語言 6位BCD時間轉為long型時間戳

2021-09-12 03:39:01 字數 2049 閱讀 2143

原始碼:

#include#include#include typedef unsigned char byte;

typedef unsigned int dword;

typedef unsigned short word;

#define pri(fmt, ...) printf("[%s]-%d: " ,__function__,__line__ );\

printf(fmt, ##__va_args__);

int main()

; begin_time_buf[0] = 0x19;

begin_time_buf[1] = 0x03;

begin_time_buf[2] = 0x06;

begin_time_buf[3] = 0x14;

begin_time_buf[4] = 0x52;

begin_time_buf[5] = 0x33;

byte tmp_time[16] = ;

tmp_time[0] = (begin_time_buf[0] >> 4) +(0x30);

tmp_time[1] = (begin_time_buf[0] & 0x0f) + (0x30);

tmp_time[2] = (begin_time_buf[1] >> 4) + (0x30);

tmp_time[3] = (begin_time_buf[1] & 0x0f) + (0x30);

tmp_time[4] = (begin_time_buf[2] >> 4) + (0x30);

tmp_time[5] = (begin_time_buf[2] & 0x0f) + (0x30);

tmp_time[6] = (begin_time_buf[3] >> 4) + (0x30);

tmp_time[7] = (begin_time_buf[3] & 0x0f) + (0x30);

tmp_time[8] = (begin_time_buf[4] >> 4) +(0x30);

tmp_time[9] = (begin_time_buf[4] & 0x0f) + (0x30);

tmp_time[10] = (begin_time_buf[5] >> 4) + (0x30);

tmp_time[11] = (begin_time_buf[5] & 0x0f) + (0x30);

pri("tmp_time : %s \n", tmp_time);

struct tm stamp;

unsigned long start_time=0;

strptime(tmp_time, "%y%m%d%h%m%s", &stamp);

start_time = mktime(&stamp);

pri("start_time : %ld \n", start_time);

return 0;

}

先把bcd碼轉為字串。

然後再通過strptime()函式轉為時間戳。

其中%y%m%d%h%m%s為:190307160123格式。

如果需要2019格式,則前面的%y改為%y即可。

%y代表年份後兩位,%y代表4位的年份。

執行結果:

root@ubuntu:/mnt/hgfs/ubuntu12.04-share/03_test/01_c/3_file# gcc -o 6 6.c

root@ubuntu:/mnt/hgfs/ubuntu12.04-share/03_test/01_c/3_file# ./6

[main]-38: tmp_time : 190306145233

[main]-47: start_time : 1551855153

root@ubuntu:/mnt/hgfs/ubuntu12.04-share/03_test/01_c/3_file#

非壓縮BCD碼轉壓縮BCD碼組合語言

datas segment 此處輸入資料段 buf dw 0302h,0908h,0705h,0102h res db?buf size db?res size db?datas ends stacks segment 此處輸入堆疊段 stacks ends codes segment assume...

C語言 日期時間轉秒數

將例如 2020年8月14日 12 30 15 的日期時間轉換為從1970年1月1日0時0分0秒開始至今的utc時間秒數,不計閏秒。中國大陸 中國香港 中國澳門 中國台灣與utc的時差為 8 include include include time t datetime2sec int year,i...

32位二進位制數轉BCD碼

32位二進位制數轉bcd碼 基本思想是逢十進1 module bin bcd 4 clk,a,bww,bqw,baw,bsw,bw,bq,bb,bs,bg input clk input 31 0 a 二進位制輸入資料 output 3 0 bww,bqw,baw,bsw,bw,bq,bb,bs,b...