printf打印std::uint64类型丢失位数问题

本文解决了一个关于在Linux环境下使用printf函数打印std::uint64_t类型变量时丢失位数的问题。通过对比不同格式说明符,指出在GCC编译器中,正确的打印方法应使用%llu来确保无符号64位整数完整显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个小问题,却搞了半天,printf打印std::uint64类型总是丢失位数,后来发现打印方法应采用如下方法:

std::uint_64  _timestamp = 1546486706769573;

printf("timestamp= %lld \n", _timestamp);

在linux下gcc应该用“%lld”,使用无符号数时,将"%lld"改成"%llu"即可。

网上有人认为应该用"%I64d",经测,应该是不管用的。

记忆一下。

#include <iostream> #include <cmath> #include <chrono> #include <thread> #include <vector> #include <gmpxx.h> #include <fstream> #include <sstream> #include <iomanip> #include <mutex> #include <atomic> #include <sys/sysinfo.h> #include <sys/resource.h> #include <fstream> #include <unistd.h> #include <nvml.h> // NVIDIA GPU监控 std::mutex output_mutex; std::atomic<bool> stop_monitor(false); mpf_class pi_value(0, 0); // 高精度π值 std::atomic<int> iteration_count(0); // 获取CPU使用率 double get_cpu_usage() { static uint64_t last_total = 0, last_idle = 0; std::ifstream stat_file("/proc/stat"); std::string line; std::getline(stat_file, line); std::istringstream iss(line); std::string cpu; uint64_t user, nice, system, idle, iowait, irq, softirq, steal; iss >> cpu >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal; uint64_t total = user + nice + system + irq + softirq + steal; uint64_t idle_time = idle + iowait; double usage = 0.0; if (last_total != 0) { uint64_t total_diff = total - last_total; uint64_t idle_diff = idle_time - last_idle; usage = 100.0 * (1.0 - static_cast<double>(idle_diff) / total_diff); } last_total = total; last_idle = idle_time; return usage; } // 获取内存使用率 double get_memory_usage() { struct sysinfo mem_info; sysinfo(&mem_info); double total = mem_info.totalram; double free = mem_info.freeram; return 100.0 * (1.0 - free / total); } // 获取GPU使用率 (NVIDIA) double get_gpu_usage() { nvmlReturn_t result; nvmlDevice_t device; unsigned int utilization; result = nvmlInit(); if (result != NVML_SUCCESS) return -1.0; result = nvmlDeviceGetHandleByIndex(0, &device); if (result != NVML_SUCCESS) { nvmlShutdown(); return -1.0; } result = nvmlDeviceGetUtilizationRates(device, &utilization); if (result != NVML_SUCCESS) { nvmlShutdown(); return -1.0; } nvmlShutdown(); return static_cast<double>(utilization.gpu); } // 资源监控线程函数 void monitor_resources() { while (!stop_monitor) { double cpu = get_cpu_usage(); double mem = get_memory_usage(); double gpu = get_gpu_usage(); { std::lock_guard<std::mutex> lock(output_mutex); std::cout << "\033[2K"; // 清除当前行 std::cout << "\r[资源监控] CPU: " << std::fixed << std::setprecision(1) << cpu << "% | " << "内存: " << mem << "% | " << "GPU: " << (gpu >= 0 ? std::to_string(static_cast<int>(gpu)) + "%" : "N/A"); std::cout.flush(); } std::this_thread::sleep_for(std::chrono::seconds(1)); } } // Chudnovsky算法计算π mpf_class chudnovsky_pi(unsigned int precision, unsigned int iterations) { mpf_set_default_prec(precision); mpf_class pi(0, precision); mpf_class sum(0, precision); mpf_class k1(13591409, precision); mpf_class k2(545140134, precision); mpf_class k3(-262537412640768000, precision); mpf_class k4(426880, precision); mpf_class k5(10005, precision); mpf_class sqrt_c(0, precision); k5 = sqrt(k5); sqrt_c = k4 * k5; mpz_class a(1); mpz_class b(1); mpz_class a_prime(1); mpz_class b_prime(1); for (unsigned int k = 0; k < iterations; k++) { mpf_class numerator(0, precision); mpf_class denominator(0, precision); if (k == 0) { a = 1; b = 1; } else { // 使用二进制分裂法优化计算 a_prime = (6*k-5)*(2*k-1)*(6*k-1); b_prime = k*k*k * k3 / 24; a *= a_prime; b *= b_prime; } numerator = k1 + k2 * k; denominator = a * b; sum += numerator / denominator; iteration_count = k + 1; // 每100次迭代显示进度 if (k % 100 == 0) { pi = sqrt_c / sum; std::lock_guard<std::mutex> lock(output_mutex); std::cout << "\n[计算进度] 迭代: " << k << "/" << iterations << " | 当前π值: " << std::setprecision(15) << pi << std::endl; } } pi = sqrt_c / sum; return pi; } int main() { const unsigned int precision = 100000; // 二进制精度位 const unsigned int iterations = 1000; // 迭代次数 std::cout << "开始计算π值 (Chudnovsky算法)..." << std::endl; std::cout << "精度: " << precision << "位 | 最大迭代: " << iterations << std::endl; // 启动资源监控线程 std::thread monitor_thread(monitor_resources); // 记录开始时间 auto start_time = std::chrono::high_resolution_clock::now(); // 计算π值 pi_value = chudnovsky_pi(precision, iterations); // 记录结束时间 auto end_time = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time); // 停止监控线程 stop_monitor = true; monitor_thread.join(); // 输出最终结果 std::cout << "\n\n计算完成!" << std::endl; std::cout << "耗时: " << duration.count() / 1000.0 << " 秒" << std::endl; std::cout << "最终π值(前50位): " << std::setprecision(50) << pi_value << std::endl; return 0; } 此代码报错,修复错误 报错信息:default.cpp:6:10: fatal error: 'gmpxx.h' file not found #include <gmpxx.h> ^~~~~~~~~ 1 error generated. 输出完整代码
07-25
#include "stm32f10x.h" // Device header #include "Delay.h" #include "Key.h" #include "Led.h" #include "serial.h" #include "OLED.h" #include "Time.h" #include "Stack.h" static unsigned char Clear_Index=0; //清零检索 static unsigned char Count_Index=0; //计算检索 static uint32_t Index=0; static unsigned char Firmula[100]; //存储算数式子 static unsigned int Result; //存储结果 void USART1_IRQHandler(void); unsigned char a=19; int main(void) { Key_Init(); OLED_Init(); Serial_Init(); while(1) { if(Key_GetNum1()) //计算算数式 { Result=Deposit(Firmula); Count_Index=1; } if(Count_Index) //发送结果 { if(Key_GetNum2()) { printf("结果=%d",Result); OLED_ShowNum(1,1,Result,4); Index=0; Clear_Index=1; Count_Index=0; } } if(Clear_Index) //清零 { if(Key_GetNum3()) { Clear_Index=0; Init(); } } else if(!Clear_Index) { if(Key_GetNum3()) { printf("请输入运算式"); } } } } void USART1_IRQHandler(void) //串口中断函数 { Firmula[Index]=Serial_Getbyte(); printf("%c",Firmula[Index]); Index++; }#include "stm32f10x.h"// Device header #include "Stack.h" #include<ctype.h> #include "Serial.h" Stack_char Stack_CHAR; Stack_num Stack_NUM; uint8_t Push_char(Stack_char *stack,uint8_t CH); uint8_t Pop_char(Stack_char *stack,uint8_t *c); uint8_t Push_num(Stack_num *stack,unsigned int NUM); uint8_t Pop_num(Stack_num *stack,unsigned int *n); void Eval(void); uint16_t Priority(uint8_t ch); void Init(void) { Stack_NUM.top=0; Stack_CHAR.top=0; } uint16_t Priority(uint8_t ch) { switch(ch) { case '(' : case ')' : return 3; case '*' : case '/' : return 2; case '+' : case '-' : return 1; default : return 0; //分化优先级 } } uint32_t Deposit(uint8_t *String) { unsigned int i,j,index=0; uint8_t C; Init(); for(i = 0;String[i]!='\0'&&i < Stack_Size ;i++) { if(isdigit(String[i])) //判断是否 '0'<=string<='9' { index=0; j=i; for(;isdigit(String[j])&&j< Stack_Size;j++) { index=index*10+(String[j]-'0'); } Push_num(&Stack_NUM,index); i=j-1; //因为for循环多加了1,所以减去1 } else if(String[i]=='(') { Push_char(&Stack_CHAR,String[i]); } else if(String[i]==')') { while(Stack_CHAR.ch[Stack_CHAR.top] != '(') {Eval();} //直到遇到左括号,并且计算 if(Stack_CHAR.top != 0 && Stack_CHAR.ch[Stack_CHAR.top] == '(') { Pop_char(&Stack_CHAR,&C); //弹出左括号 } } else { while(Stack_CHAR.top!=0&&Stack_CHAR.ch[Stack_CHAR.top]!='('&&Priority(Stack_CHAR.ch[Stack_CHAR.top])>=Priority(String[i])) { Eval(); } Push_char(&Stack_CHAR,String[i]); } } while(Stack_CHAR.top) { Eval(); } //循环直至操作符为空 return Stack_NUM.num[Stack_NUM.top]; //此时数栈顶元素即为表达式值 } void Eval(void) { uint32_t a,x,b; uint8_t cha; Pop_num(&Stack_NUM,&b); Pop_num(&Stack_NUM,&a); //由于栈是陷进后出,与队列有区别(先进出) Pop_char(&Stack_CHAR,&cha); switch(cha) { case '*' : x=a*b;break; //计算 case '/' : { if(b==0) {printf("除数不能为0");x=0;} else {x=a/b;} break; } case '+' : {x=a+b;break;} case '-' : {x=a-b;break;} default :break; } Push_num(&Stack_NUM,x); } uint8_t Push_char(Stack_char *stack,uint8_t CH) { if(stack->top>=Stack_Size) { return 0; } stack->top++; stack->ch[stack->top]=CH; return 1; } uint8_t Push_num(Stack_num *stack,unsigned int NUM) { if(stack->top>=Stack_Size) { return 0; } stack->top++; stack->num[stack->top]=NUM; return 1; } uint8_t Pop_char(Stack_char *stack,uint8_t *c) { if(stack->top<=0) { return 0; } *c=stack->ch[stack->top]; stack->top--; return 1; } uint8_t Pop_num(Stack_num *stack,unsigned int *n) { if(stack->top<=0) { return 0; } *n=stack->num[stack->top]; stack->top--; return 1; } /* Copyright (C) ARM Ltd., 1999,2014 */ /* All rights reserved */ /* * RCS $Revision$ * Checkin $Date$ * Revising $Author: agrant $ */ #ifndef __stdint_h #define __stdint_h #define __ARMCLIB_VERSION 5060034 #ifdef __INT64_TYPE__ /* armclang predefines '__INT64_TYPE__' and '__INT64_C_SUFFIX__' */ #define __INT64 __INT64_TYPE__ #else /* armcc has builtin '__int64' which can be used in --strict mode */ #define __INT64 __int64 #define __INT64_C_SUFFIX__ ll #endif #define __PASTE2(x, y) x ## y #define __PASTE(x, y) __PASTE2(x, y) #define __INT64_C(x) __ESCAPE__(__PASTE(x, __INT64_C_SUFFIX__)) #define __UINT64_C(x) __ESCAPE__(__PASTE(x ## u, __INT64_C_SUFFIX__)) #if defined(__clang__) || (defined(__ARMCC_VERSION) && !defined(__STRICT_ANSI__)) /* armclang and non-strict armcc allow 'long long' in system headers */ #define __LONGLONG long long #else /* strict armcc has '__int64' */ #define __LONGLONG __int64 #endif #ifndef __STDINT_DECLS #define __STDINT_DECLS #undef __CLIBNS #ifdef __cplusplus namespace std { #define __CLIBNS std:: extern "C" { #else #define __CLIBNS #endif /* __cplusplus */ /* * 'signed' is redundant below, except for 'signed char' and if * the typedef is used to declare a bitfield. */ /* 7.18.1.1 */ /* exact-width signed integer types */ typedef signed char int8_t; typedef signed short int int16_t; typedef signed int int32_t; typedef signed __INT64 int64_t; /* exact-width unsigned integer types */ typedef unsigned char uint8_t; typedef unsigned short int uint16_t; typedef unsigned int uint32_t; typedef unsigned __INT64 uint64_t; /* 7.18.1.2 */ /* smallest type of at least n bits */ /* minimum-width signed integer types */ typedef signed char int_least8_t; typedef signed short int int_least16_t; typedef signed int int_least32_t; typedef signed __INT64 int_least64_t; /* minimum-width unsigned integer types */ typedef unsigned char uint_least8_t; typedef unsigned short int uint_least16_t; typedef unsigned int uint_least32_t; typedef unsigned __INT64 uint_least64_t; /* 7.18.1.3 */ /* fastest minimum-width signed integer types */ typedef signed int int_fast8_t; typedef signed int int_fast16_t; typedef signed int int_fast32_t; typedef signed __INT64 int_fast64_t; /* fastest minimum-width unsigned integer types */ typedef unsigned int uint_fast8_t; typedef unsigned int uint_fast16_t; typedef unsigned int uint_fast32_t; typedef unsigned __INT64 uint_fast64_t; /* 7.18.1.4 integer types capable of holding object pointers */ #if __sizeof_ptr == 8 typedef signed __INT64 intptr_t; typedef unsigned __INT64 uintptr_t; #else typedef signed int intptr_t; typedef unsigned int uintptr_t; #endif /* 7.18.1.5 greatest-width integer types */ typedef signed __LONGLONG intmax_t; typedef unsigned __LONGLONG uintmax_t; #if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) /* 7.18.2.1 */ /* minimum values of exact-width signed integer types */ #define INT8_MIN -128 #define INT16_MIN -32768 #define INT32_MIN (~0x7fffffff) /* -2147483648 is unsigned */ #define INT64_MIN __INT64_C(~0x7fffffffffffffff) /* -9223372036854775808 is unsigned */ /* maximum values of exact-width signed integer types */ #define INT8_MAX 127 #define INT16_MAX 32767 #define INT32_MAX 2147483647 #define INT64_MAX __INT64_C(9223372036854775807) /* maximum values of exact-width unsigned integer types */ #define UINT8_MAX 255 #define UINT16_MAX 65535 #define UINT32_MAX 4294967295u #define UINT64_MAX __UINT64_C(18446744073709551615) /* 7.18.2.2 */ /* minimum values of minimum-width signed integer types */ #define INT_LEAST8_MIN -128 #define INT_LEAST16_MIN -32768 #define INT_LEAST32_MIN (~0x7fffffff) #define INT_LEAST64_MIN __INT64_C(~0x7fffffffffffffff) /* maximum values of minimum-width signed integer types */ #define INT_LEAST8_MAX 127 #define INT_LEAST16_MAX 32767 #define INT_LEAST32_MAX 2147483647 #define INT_LEAST64_MAX __INT64_C(9223372036854775807) /* maximum values of minimum-width unsigned integer types */ #define UINT_LEAST8_MAX 255 #define UINT_LEAST16_MAX 65535 #define UINT_LEAST32_MAX 4294967295u #define UINT_LEAST64_MAX __UINT64_C(18446744073709551615) /* 7.18.2.3 */ /* minimum values of fastest minimum-width signed integer types */ #define INT_FAST8_MIN (~0x7fffffff) #define INT_FAST16_MIN (~0x7fffffff) #define INT_FAST32_MIN (~0x7fffffff) #define INT_FAST64_MIN __INT64_C(~0x7fffffffffffffff) /* maximum values of fastest minimum-width signed integer types */ #define INT_FAST8_MAX 2147483647 #define INT_FAST16_MAX 2147483647 #define INT_FAST32_MAX 2147483647 #define INT_FAST64_MAX __INT64_C(9223372036854775807) /* maximum values of fastest minimum-width unsigned integer types */ #define UINT_FAST8_MAX 4294967295u #define UINT_FAST16_MAX 4294967295u #define UINT_FAST32_MAX 4294967295u #define UINT_FAST64_MAX __UINT64_C(18446744073709551615) /* 7.18.2.4 */ /* minimum value of pointer-holding signed integer type */ #if __sizeof_ptr == 8 #define INTPTR_MIN INT64_MIN #else #define INTPTR_MIN INT32_MIN #endif /* maximum value of pointer-holding signed integer type */ #if __sizeof_ptr == 8 #define INTPTR_MAX INT64_MAX #else #define INTPTR_MAX INT32_MAX #endif /* maximum value of pointer-holding unsigned integer type */ #if __sizeof_ptr == 8 #define UINTPTR_MAX UINT64_MAX #else #define UINTPTR_MAX UINT32_MAX #endif /* 7.18.2.5 */ /* minimum value of greatest-width signed integer type */ #define INTMAX_MIN __ESCAPE__(~0x7fffffffffffffffll) /* maximum value of greatest-width signed integer type */ #define INTMAX_MAX __ESCAPE__(9223372036854775807ll) /* maximum value of greatest-width unsigned integer type */ #define UINTMAX_MAX __ESCAPE__(18446744073709551615ull) /* 7.18.3 */ /* limits of ptrdiff_t */ #if __sizeof_ptr == 8 #define PTRDIFF_MIN INT64_MIN #define PTRDIFF_MAX INT64_MAX #else #define PTRDIFF_MIN INT32_MIN #define PTRDIFF_MAX INT32_MAX #endif /* limits of sig_atomic_t */ #define SIG_ATOMIC_MIN (~0x7fffffff) #define SIG_ATOMIC_MAX 2147483647 /* limit of size_t */ #if __sizeof_ptr == 8 #define SIZE_MAX UINT64_MAX #else #define SIZE_MAX UINT32_MAX #endif /* limits of wchar_t */ /* NB we have to undef and redef because they're defined in both * stdint.h and wchar.h */ #undef WCHAR_MIN #undef WCHAR_MAX #if defined(__WCHAR32) || (defined(__ARM_SIZEOF_WCHAR_T) && __ARM_SIZEOF_WCHAR_T == 4) #define WCHAR_MIN 0 #define WCHAR_MAX 0xffffffffU #else #define WCHAR_MIN 0 #define WCHAR_MAX 65535 #endif /* limits of wint_t */ #define WINT_MIN (~0x7fffffff) #define WINT_MAX 2147483647 #endif /* __STDC_LIMIT_MACROS */ #if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) /* 7.18.4.1 macros for minimum-width integer constants */ #define INT8_C(x) (x) #define INT16_C(x) (x) #define INT32_C(x) (x) #define INT64_C(x) __INT64_C(x) #define UINT8_C(x) (x ## u) #define UINT16_C(x) (x ## u) #define UINT32_C(x) (x ## u) #define UINT64_C(x) __UINT64_C(x) /* 7.18.4.2 macros for greatest-width integer constants */ #define INTMAX_C(x) __ESCAPE__(x ## ll) #define UINTMAX_C(x) __ESCAPE__(x ## ull) #endif /* __STDC_CONSTANT_MACROS */ #ifdef __cplusplus } /* extern "C" */ } /* namespace std */ #endif /* __cplusplus */ #endif /* __STDINT_DECLS */ #ifdef __cplusplus #ifndef __STDINT_NO_EXPORTS using ::std::int8_t; using ::std::int16_t; using ::std::int32_t; using ::std::int64_t; using ::std::uint8_t; using ::std::uint16_t; using ::std::uint32_t; using ::std::uint64_t; using ::std::int_least8_t; using ::std::int_least16_t; using ::std::int_least32_t; using ::std::int_least64_t; using ::std::uint_least8_t; using ::std::uint_least16_t; using ::std::uint_least32_t; using ::std::uint_least64_t; using ::std::int_fast8_t; using ::std::int_fast16_t; using ::std::int_fast32_t; using ::std::int_fast64_t; using ::std::uint_fast8_t; using ::std::uint_fast16_t; using ::std::uint_fast32_t; using ::std::uint_fast64_t; using ::std::intptr_t; using ::std::uintptr_t; using ::std::intmax_t; using ::std::uintmax_t; #endif #endif /* __cplusplus */ #undef __INT64 #undef __LONGLONG #endif /* __stdint_h */ /* end of stdint.h */ 在不改变原代码变量名情况下,实现小数点预算
07-16
#pragma once #include <iostream> #include <chrono> #include <stdexcept> #include <cstdlib> #include <cstdio> #include <cstdint> /** * @enum ERROR_ID * @brief 错误状态枚举 */ enum ERROR_ID { NO_ERROR, ///< 无错误 ERROR_INIT, ///< 初始化错误 ERROR_DATAINPUT, ///< 数据输入错误 ERROR_DATAHANDLE, ///< 数据处理错误 ERROR_SHOWARRAY, ///< 显示数组错误 ERROR_BITHANDLE, ///< 位处理错误 ERROR_SHOWBYTE, ///< 显示字节错误 ERROR_CANFUNCTION ///< CAN功能错误 }; /** * @union CanData * @brief CAN数据结构 */ union CanData { uint8_t Can_array[8]; ///< CAN数据字节数组 uint64_t Can_data; ///< CAN数据64位表示 }; /** * @class CanFunc * @brief CAN功能类 */ class CanFunc { public: /** * @brief 构造函数 */ CanFunc() = default; /** * @brief 析构函数 */ ~CanFunc() = default; /** * @brief 初始化CAN功能 * @return 错误码 */ uint32_t Init(); /** * @brief 数据输入 * @return 错误码 */ uint32_t DataInput(); /** * @brief 数据处理 * @return 错误码 */ uint32_t DataHandle(); /** * @brief 显示数组内容 * @return 错误码 */ uint32_t ShowArray(); /** * @brief 位处理 * @return 错误码 */ uint32_t BitHandle(); /** * @brief 显示字节内容 * @return 错误码 */ uint32_t ShowByte(); /** * @brief CAN功能处理 * @return 错误码 */ uint32_t CanFuntion(); /** * @brief 获取CAN数据 * @return CAN数据 */ uint64_t GetData(); /** * @brief 获取ID * @return ID */ uint32_t GetId(); /** * @brief 获取DLC * @return DLC */ uint32_t GetDlc(); /** * @brief 获取起始位 * @return 起始位 */ uint32_t GetStartBit(); /** * @brief 获取长度 * @return 长度 */ uint32_t GetLen(); private: // ================== 常量定义 ================== static const uint32_t MATRIX_ROWS = 8; ///< 矩阵行数 static const uint32_t MATRIX_COLS = 8; ///< 矩阵列数 static const uint32_t MAX_BITS_PER_BYTE = 8; ///< 每字节最大位数 static const uint32_t BIT_WIDTH = 8; ///< 位宽 static const uint32_t BIT_LENGTH = 8; ///< 位长度 // ================== 成员变量 ================== uint32_t Id = 0; ///< CAN ID uint32_t Dlc = 0; ///< 数据长度码 uint32_t StartBit = 0; ///< 起始位 uint32_t Len = 0; ///< 数据长度 uint8_t TmpPkg[MATRIX_ROWS][MATRIX_COLS] = { 0 }; ///< 临时数据包 uint64_t SigVal = 0; ///< 信号值 CanData CanDataUnion = { 0 }; ///< CAN数据联合体 /** * @brief 自定义整数幂函数 * @param base 基数 * @param exponent 指数 * @return 结果值 */ uint64_t intPow(uint32_t base, uint32_t exponent) { if (exponent == 0) return 1; uint64_t result = 1; for (uint32_t i = 0; i < exponent; ++i) { result *= base; } return result; } }; #include "CanFunc.h" /** * @brief 初始化CAN功能 * @return 错误码 */ uint32_t CanFunc::Init() { return NO_ERROR; } /** * @brief 数据输入 * @return 错误码 */ uint32_t CanFunc::DataInput() { std::cout << "请输入CAN信号参数(ID DLC StartBit Len SigVal):\n"; std::cout << "示例: 320 8 16 13 300\n"; std::cout << "输入: "; if (!(std::cin >> Id >> Dlc >> StartBit >> Len >> SigVal)) { std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); return ERROR_DATAINPUT; } // 值范围校验 uint64_t maxSignalValue = intPow(2, Len) - 1; if (SigVal > maxSignalValue) { std::cerr << "错误: 信号值超出范围(0 ~ " << maxSignalValue << ")\n"; return ERROR_DATAINPUT; } return NO_ERROR; } /** * @brief 数据处理 * @return 错误码 */ uint32_t CanFunc::DataHandle() { uint32_t remainingLength = Len; uint64_t signalTemp = SigVal; int32_t row = StartBit / MAX_BITS_PER_BYTE; int32_t col = MAX_BITS_PER_BYTE - 1 - (StartBit % MAX_BITS_PER_BYTE); // 初始化TmpPkg for (int i = 0; i < MATRIX_ROWS; i++) { for (int j = 0; j < MATRIX_COLS; j++) { TmpPkg[i][j] = 0; } } // Motorola格式位填充 while (remainingLength > 0 && row >= 0) { while (remainingLength > 0 && col >= 0) { TmpPkg[row][col] = signalTemp % 2; signalTemp /= 2; remainingLength--; col--; } row--; col = MAX_BITS_PER_BYTE - 1; // 换行重置列索引 } return (remainingLength > 0) ? ERROR_DATAHANDLE : NO_ERROR; } /** * @brief 显示数组内容 * @return 错误码 */ uint32_t CanFunc::ShowArray() { std::cout << "CAN信号位图(8x8矩阵):\n"; for (uint32_t row = 0; row < MATRIX_ROWS; row++) { for (uint32_t col = 0; col < MATRIX_COLS; col++) { std::cout << static_cast<int>(TmpPkg[row][col]) << " "; } std::cout << "\n"; } return NO_ERROR; } /** * @brief 位处理 * @return 错误码 */ uint32_t CanFunc::BitHandle() { for (uint32_t row = 0; row < MATRIX_ROWS; row++) { uint8_t tempByte = 0; for (uint32_t col = 0; col < MATRIX_COLS; col++) { tempByte |= TmpPkg[row][col]; if (col != MATRIX_COLS - 1) { tempByte <<= 1; // 左移,最后一位除外 } } CanDataUnion.Can_array[row] = tempByte; } return NO_ERROR; } /** * @brief 显示字节内容 * @return 错误码 */ uint32_t CanFunc::ShowByte() { printf("CAN报文: ID:%u DLC:%u Data: ", Id, Dlc); for (uint32_t i = 0; i < MATRIX_ROWS; i++) { printf("%02X ", CanDataUnion.Can_array[i]); } printf("\n"); return NO_ERROR; } /** * @brief CAN功能处理 * @return 错误码 */ uint32_t CanFunc::CanFuntion() { uint32_t status = NO_ERROR; if ((status = DataInput()) != NO_ERROR) return status; if ((status = DataHandle()) != NO_ERROR) return status; ShowArray(); BitHandle(); ShowByte(); return NO_ERROR; } /** * @brief 获取CAN数据 * @return CAN数据 */ uint64_t CanFunc::GetData() { return CanDataUnion.Can_data; } /** * @brief 获取ID * @return ID */ uint32_t CanFunc::GetId() { return Id; } /** * @brief 获取DLC * @return DLC */ uint32_t CanFunc::GetDlc() { return Dlc; } /** * @brief 获取起始位 * @return 起始位 */ uint32_t CanFunc::GetStartBit() { return StartBit; } /** * @brief 获取长度 * @return 长度 */ uint32_t CanFunc::GetLen() { return Len; } 将上述的代码中的CAN信号位图(8x8矩阵)显示功能删除
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值