Linux系统下,C语言:时间转为字符数组和long整形转换为字节数组demo
1, 时间转换为字符数组
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#define NLMSG_MAX_SIZE 64
// Linux系统下,C语言:时间转为数组demo
int main() {
struct timespec ts;
long cur_time_us;
char tx_test[NLMSG_MAX_SIZE];
long long time_ns;
long long time_vs;
char test[NLMSG_MAX_SIZE] = {0x01, 0x02, '1'};
if (clock_gettime(CLOCK_REALTIME, &ts) == 0) {
printf("REALTIME: %lld s, %ld ns \n", (long long)ts.tv_sec, ts.tv_nsec);
printf("REALTIME: %lld ms\n", (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
printf("sizeof(tx_test): %d\n", sizeof(tx_test));
//memset(tx_test, 0, sizeof(tx_test));
time_ns = (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
snprintf(tx_test, NLMSG_MAX_SIZE, "TIME_NS=%llu", time_ns);
time_vs = (long long)ts.tv_sec * 1000 * 1000 + ts.tv_nsec / 1000;
snprintf(tx_test, NLMSG_MAX_SIZE, "TIME_VS=%llu", time_vs);
printf("tx_test: %s\n", tx_test);
for (int i = 0; tx_test[i] != '\0' && i < NLMSG_MAX_SIZE ; i ++) {
printf("char[%d]=%c : %x \n", i, tx_test[i], tx_test[i]);
}
for (int i = 0; test[i] != '\0' && i < NLMSG_MAX_SIZE ; i ++) {
printf("char[%d]=%c : %x \n", i, test[i], test[i]);
}
}
}
上面程序运行结果:
REALTIME: 1750147934 s, 133826268 ns
REALTIME: 1750147934133 ms
sizeof(tx_test): 64
tx_test: TIME_VS=1750147934133826
char[0]=T : 54
char[1]=I : 49
char[2]=M : 4d
char[3]=E : 45
char[4]=_ : 5f
char[5]=V : 56
char[6]=S : 53
char[7]== : 3d
char[8]=1 : 31
char[9]=7 : 37
char[10]=5 : 35
char[11]=0 : 30
char[12]=1 : 31
char[13]=4 : 34
char[14]=7 : 37
char[15]=9 : 39
char[16]=3 : 33
char[17]=4 : 34
char[18]=1 : 31
char[19]=3 : 33
char[20]=3 : 33
char[21]=8 : 38
char[22]=2 : 32
char[23]=6 : 36
char[0]= : 1
char[1]= : 2
char[2]=1 : 31
2, union转换long long整型数到字节数组如下:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#define NLMSG_MAX_SIZE 64
union U64ToBytes
{
unsigned long long u64;
unsigned char u8[8];
};
union U32ToBytes
{
unsigned long long u32;
unsigned char u8[4];
};
int main() {
struct timespec ts;
long cur_time_us;
char tx_test[NLMSG_MAX_SIZE];
long long time_ns;
long long time_vs;
union U64ToBytes u64ToBytes;
union U64ToBytes bytes8;
//u64ToBytes.u64 = 162826951238;
//u64ToBytes.u64 = 0x123456789ABCDEF0;
u64ToBytes.u64 = 49861866; // 49861866的十六进制是:0x2F8D4EA
for (int i = 0; i < 8 ; i++) {
printf("U64ToBytes: Byte [%i]: 0x%x \n", i, u64ToBytes.u8[i]);
}
snprintf(bytes8.u8, 8, "%s", (char *)u64ToBytes.u8);
//bytes8.u64 = 49861866;
for (int i = 0; i < 8 ; i++) {
printf("bytes8: Byte [%i]: 0x%x \n", i, bytes8.u8[i]);
}
printf("bytes8.u64: %llu \n", bytes8.u64);
union U32ToBytes u32ToBytes;
u32ToBytes.u8[0] = 0xEA;
u32ToBytes.u8[1] = 0xD4;
u32ToBytes.u8[2] = 0xF8;
u32ToBytes.u8[3] = 0x2;
printf("U32ToBytes: %d \n", u32ToBytes.u32);
}
运行结果如下:
U64ToBytes: Byte [0]: 0xea
U64ToBytes: Byte [1]: 0xd4
U64ToBytes: Byte [2]: 0xf8
U64ToBytes: Byte [3]: 0x2
U64ToBytes: Byte [4]: 0x0
U64ToBytes: Byte [5]: 0x0
U64ToBytes: Byte [6]: 0x0
U64ToBytes: Byte [7]: 0x0
bytes8: Byte [0]: 0xea
bytes8: Byte [1]: 0xd4
bytes8: Byte [2]: 0xf8
bytes8: Byte [3]: 0x2
bytes8: Byte [4]: 0x0
bytes8: Byte [5]: 0x0
bytes8: Byte [6]: 0x0
bytes8: Byte [7]: 0x0
bytes8.u64: 49861866
U32ToBytes: 49861866
3, linux内核提供了宏来帮助开发者在不同的字节序之间转换。
cpu_to_be64()和cpu_to_le64() 分别将u64值从主机字节序转换为大端字节序和小端字节序。
反之可以使用be64_to_cpu()和le64_to_cpu()来转换回主机字节序。


被折叠的 条评论
为什么被折叠?



