开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 264|回复: 2
收起左侧

[已解决] c++时间戳写到字节集

 关闭 [复制链接]
结帖率:83% (19/23)
发表于 2025-11-9 15:15:58 | 显示全部楼层 |阅读模式   河北省廊坊市
8精币
这两天需要写个64位,易语言没法弄,看了2天c++,弄得是头昏脑胀,也搞不明白。

auto now = std::chrono::system_clock::now();
std::time_t current_time = std::chrono::system_clock::to_time_t(now);
// 格式化为可读字符串
std::tm* time_info = std::localtime(&current_time);


这个时间戳,current_time,写到文本就没问题

vector<unsigned char> replace_bytes = float_to_bytes(static_cast<float>(current_time));

然后我将它转换然后写到字节集,再取字节集数据(小数)读出来,数据就不对了。。。。求朋友给改一下,实在搞不明白

最佳答案

查看完整内容

#include #include #include #include #include #include // 通用类型转字节集 template std::vector to_bytes(const T& value) { std::vector bytes(sizeof(T)); std::memcpy(bytes.data(), &value, sizeof(T)); return bytes; } // 打印字节集(十六进制) void print_bytes(const std::vector& bytes) { std::cout

回答提醒:如果本帖被关闭无法回复,您有更好的答案帮助楼主解决,请发表至 源码区 可获得加分喔。
友情提醒:本版被采纳的主题可在 申请荣誉值 页面申请荣誉值,获得 1点 荣誉值,荣誉值可兑换荣誉会员、终身vip用户组。
快捷通道:申请荣誉值
结帖率:100% (21/21)
发表于 2025-11-9 15:15:59 | 显示全部楼层   安徽省滁州市
#include <iostream>
#include <iomanip>
#include <chrono>
#include <vector>
#include <cstring>
#include <ctime>

// 通用类型转字节集
template <typename T>
std::vector<unsigned char> to_bytes(const T& value) {
    std::vector<unsigned char> bytes(sizeof(T));
    std::memcpy(bytes.data(), &value, sizeof(T));
    return bytes;
}

// 打印字节集(十六进制)
void print_bytes(const std::vector<unsigned char>& bytes) {
    std::cout << "字节集内容(十六进制):";
    for (unsigned char b : bytes) {
        std::cout << std::hex << std::setw(2) << std::setfill('0')
                  << static_cast<int>(b) << " ";
    }
    std::cout << std::dec << std::endl;
}

// 将时间戳格式化为中文年月日(带星期)
std::string format_chinese_date(std::time_t time) {
    std::tm* time_info = std::localtime(&time);
    if (!time_info) {
        return "时间格式转换失败";
    }

    // 手动映射星期(避免依赖系统locale)
    const char* weekdays[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    int wday = time_info->tm_wday;  // 0=周日,6=周六
    const char* weekday_str = (wday >= 0 && wday < 7) ? weekdays[wday] : "未知";

    // 格式化年月日
    char buffer[100];
    std::strftime(buffer, sizeof(buffer), "%Y年%m月%d日 ", time_info);
    return std::string(buffer) + weekday_str;
}

int main() {
    // 1. 获取原始时间戳
    auto now = std::chrono::system_clock::now();
    std::time_t original_time = std::chrono::system_clock::to_time_t(now);
   
    // 输出中文年月日
    std::string chinese_date = format_chinese_date(original_time);
    std::cout << "原始时间戳(秒数):" << original_time << " → 中文日期:"
              << chinese_date << std::endl;

    // 2. 转换为64位整数(无精度损失,推荐)
    uint64_t time_as_uint64 = static_cast<uint64_t>(original_time);
    std::cout << "转换为uint64_t后的值:" << time_as_uint64 << std::endl;

    // 3. 转为字节集
    std::vector<unsigned char> bytes = to_bytes(time_as_uint64);
    print_bytes(bytes);

    // 4. 从字节集读回
    uint64_t read_back_uint64;
    std::memcpy(&read_back_uint64, bytes.data(), sizeof(uint64_t));
    std::time_t recovered_time = static_cast<std::time_t>(read_back_uint64);
    std::cout << "从字节集读回的uint64_t:" << read_back_uint64
              << " → 转回时间戳:" << recovered_time << std::endl;

    // 验证恢复的时间是否正确(中文格式)
    std::string recovered_chinese_date = format_chinese_date(recovered_time);
    std::cout << "恢复的中文日期:" << recovered_chinese_date << std::endl;

    // 5. 验证是否一致
    if (recovered_time == original_time) {
        std::cout << "✅ 转换成功!前后时间戳一致" << std::endl;
    } else {
        std::cout << "❌ 转换失败!前后时间戳不一致" << std::endl;
    }

    return 0;
}

评分

参与人数 2好评 +1 荣誉 +1 收起 理由
笨潴 + 1 热心帮助他人,荣誉+1,希望继续努力(*^__^*) 嘻嘻!
96692 + 1 欢迎常来帮助新人,谢谢~

查看全部评分

回复

使用道具 举报

结帖率:83% (19/23)
 楼主| 发表于 2025-11-9 16:32:12 | 显示全部楼层   河北省廊坊市
安慕希ii 发表于 2025-11-9 16:12
#include
#include
#include

非常感谢,c++太麻烦了, sshot-5.png
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报QQ: 793400750,邮箱:wp@125.la
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备2025452707号) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表