首页 > 编程语言 >C++ int32, int64 和十六进制字符串的转换

C++ int32, int64 和十六进制字符串的转换

时间:2024-08-10 11:39:16浏览次数:14  
标签:std 十六进制 int32 C++ LEN int64Value BYTE hexStr

 

 

 

#include <iostream>  
#include <string>  
#include <cstring>  // 用于 memset, strlen
#include <algorithm>

/**
 * @brife: 将一个 int64 数字转为 十六进制字符串
 * @note:
    int64Value: 0,     hexStr:0000000000000000
    int64Value: -1,    hexStr:ffffffffffffffff
    int64Value: 1,     hexStr:0000000000000001
    int64Value: 1,     hexStr:0000000000000001
    int64Value: 12,    hexStr:000000000000000c
    int64Value: 123,   hexStr:000000000000007b
    int64Value: 1234,  hexStr:00000000000004d2
    int64Value: 12345, hexStr:0000000000003039
    int64Value: -8056929837760094207, hexStr:9030090000106001
    int64Value: 1166442199094681601,  hexStr:1030090000106001
 */
std::string Int64ToHex(int64_t value) {  
    int BYTE_LEN = 16;  // int64_t 占 16 字节,可以表示 16 个 十六进制字符
    // 处理负数的一种方式是将其视为无符号数的补码  
    // 注意:这里我们直接以无符号方式处理,因为十六进制通常不直接表示符号  
    uint64_t uvalue = static_cast<uint64_t>(value);  
  
    // 创建一个足够大的字符数组来存储结果(包括终止符'\0')  
    // 64位整数最多需要16个十六进制数字加上终止符  
    char hexStr[BYTE_LEN+1] = {0};  
  
    // 使用sprintf将整数转换为十六进制字符串  
    // 注意:%llx 是用于无符号 long long 的十六进制格式说明符  
    // 在大多数平台上,int64_t 和 uint64_t 都是通过 long long 实现的  
    snprintf(hexStr, sizeof(hexStr), "%llx", uvalue);

    // 计算实际写入的字符数(不包括终止符'\0')  
    size_t len = strlen(hexStr);  
  
    // 如果长度小于16,则前面补0  
    if (len < BYTE_LEN) {  
        std::memmove(hexStr + (BYTE_LEN - len), hexStr, len + 1); // 移动字符串以腾出空间  
        memset(hexStr, '0', BYTE_LEN - len); // 在前面填充0  
    } 
  
    // 将C风格字符串转换为std::string并返回  
    return std::string(hexStr);  
}  



/**
 * @brife: 将一个 int32 数字转为 十六进制字符串
 * @note:
    int32Value: 0,        hexStr:00000000
    int32Value: -1,       hexStr:ffffffff
    int32Value: 1,        hexStr:00000001
    int32Value: 12,       hexStr:0000000c
    int32Value: 123,      hexStr:0000007b
    int32Value: 1234,     hexStr:000004d2
    int32Value: 12345,    hexStr:00003039
    int32Value: -12345,   hexStr:ffffcfc7
    int32Value: 12345678, hexStr:00bc614e
 */
std::string Int32ToHex(int64_t value) {  
    int BYTE_LEN = 8;  // int32_t 占 8 字节,可以表示 8 个 十六进制字符
    // 处理负数的一种方式是将其视为无符号数的补码  
    // 注意:这里我们直接以无符号方式处理,因为十六进制通常不直接表示符号  
    uint32_t uvalue = static_cast<uint32_t>(value);  
  
    // 创建一个足够大的字符数组来存储结果(包括终止符'\0')  
    // 32位整数最多需要 8个 十六进制数字加上终止符  
    char hexStr[BYTE_LEN+1] = {0};  
  
    // 使用sprintf将整数转换为十六进制字符串  
    // 注意:%x 是用于无符号 int 的十六进制格式说明符  
    snprintf(hexStr, sizeof(hexStr), "%x", uvalue);

    // 计算实际写入的字符数(不包括终止符'\0')  
    size_t len = strlen(hexStr);  
  
    // 如果长度小于 8 ,则前面补0  
    if (len < BYTE_LEN) {  
        std::memmove(hexStr + (BYTE_LEN - len), hexStr, len + 1); // 移动字符串以腾出空间  
        memset(hexStr, '0', BYTE_LEN - len); // 在前面填充0  
    } 
  
    // 将C风格字符串转换为std::string并返回  
    return std::string(hexStr);  
}  



/**
 * @brife: 将一个 十六进制字符串,转为int64数字
 * @note:
    hexStr:0,                int64Value:0
    hexStr:1,                int64Value:1
    hexStr:12,               int64Value:18
    hexStr:123,              int64Value:291
    hexStr:1234,             int64Value:4660
    hexStr:0000000000000001, int64Value:1
    hexStr:0000000000000000, int64Value:0
    hexStr:0000000000000012, int64Value:18
    hexStr:0000000000000123, int64Value:291
    hexStr:9030090000106001, int64Value:-8056929837760094207
    hexStr:1030090000106001, int64Value:1166442199094681601 
 */
int64_t HexToInt64(const std::string& hexStr) {
    int64_t int64Value = 0x00;
    int BYTE_LEN = 16;  //如果长度超过了 16 字节,则只取前16字节
    if(hexStr.length() > BYTE_LEN){
        std::cout<<"hexStr is out of int64 range, the back data will lost! hexStr.len:"<<hexStr.length()<<std::endl;
    }
    for(int i=0; i<hexStr.length() && i<BYTE_LEN; ++i){
        int8_t oneByte = hexStr.at(i) - '0';
        int64Value = int64Value<<4 | oneByte;
    }
    return int64Value;  
}

/**
 * @brife: 将一个 十六进制字符串,转为int32数字
 * @note:
 */
int32_t HexToInt32(const std::string& hexStr) {
    int32_t int32Value = 0x00;
    int BYTE_LEN = 8; //如果长度超过了 8 字节,则只取前8字节
    if(hexStr.length() > BYTE_LEN){
        std::cout<<"hexStr is out of int32 range, the back data will lost! hexStr.len:"<<hexStr.length()<<std::endl;
    }
    for(int i=0; i<hexStr.length() && i<BYTE_LEN; ++i){
        int8_t oneByte = hexStr.at(i) - '0';
        int32Value = int32Value<<4 | oneByte;
    }
    return int32Value;  
}
 

void  test_int64_2_hex(){
    std::cout<<std::endl<<"=============== test_int64_2_hex =============="<<std::endl;
    {
        int64_t int64Value = 0;  
        std::string hexStr = Int64ToHex(int64Value);  
        std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl;  
    }

    {
        int64_t int64Value = -1;  
        std::string hexStr = Int64ToHex(int64Value);  
        std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl;  
    }

    {
        int64_t int64Value = 1;  
        std::string hexStr = Int64ToHex(int64Value);  
        std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl;  
    }

    {
        int64_t int64Value = 1;  
        std::string hexStr = Int64ToHex(int64Value);  
        std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl;  
    }

    {
        int64_t int64Value = 12;  
        std::string hexStr = Int64ToHex(int64Value);  
        std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl;  
    }

    {
        int64_t int64Value = 123;  
        std::string hexStr = Int64ToHex(int64Value);  
        std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl;  
    }

    {
        int64_t int64Value = 1234;  
        std::string hexStr = Int64ToHex(int64Value);  
        std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl;  
    }

    {
        int64_t int64Value = 12345;  
        std::string hexStr = Int64ToHex(int64Value);  
        std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl;  
    }

    {
        int64_t int64Value = -8056929837760094207;  
        std::string hexStr = Int64ToHex(int64Value);  
        std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl;  
    }

    {
        int64_t int64Value = 1166442199094681601;  
        std::string hexStr = Int64ToHex(int64Value);  
        std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl;  
    }
}

void  test_int32_2_hex(){
    std::cout<<std::endl<<"=============== test_int32_2_hex =============="<<std::endl;
    {
        int64_t int32Value = 0;  
        std::string hexStr = Int32ToHex(int32Value);  
        std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl;  
    }
    {
        int64_t int32Value = -1;  
        std::string hexStr = Int32ToHex(int32Value);  
        std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl;  
    }
    {
        int64_t int32Value = 1;  
        std::string hexStr = Int32ToHex(int32Value);  
        std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl;  
    }
    {
        int64_t int32Value = 12;  
        std::string hexStr = Int32ToHex(int32Value);  
        std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl;  
    }
    {
        int64_t int32Value = 123;  
        std::string hexStr = Int32ToHex(int32Value);  
        std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl;  
    }
    {
        int64_t int32Value = 1234;  
        std::string hexStr = Int32ToHex(int32Value);  
        std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl;  
    }
    {
        int64_t int32Value = 12345;  
        std::string hexStr = Int32ToHex(int32Value);  
        std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl;  
    }
    {
        int64_t int32Value = -12345;  
        std::string hexStr = Int32ToHex(int32Value);  
        std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl;  
    }
    {
        int64_t int32Value = 12345678;  
        std::string hexStr = Int32ToHex(int32Value);  
        std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl;  
    }

}

void test_hex_2_int64(){
    std::cout<<std::endl<<"=============== test_hex_2_int64 =============="<<std::endl;

    {
        std::string hexStr = "0";
        int64_t int64Value = HexToInt64(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl;
    }
    {
        std::string hexStr = "1";
        int64_t int64Value = HexToInt64(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl;
    }
    
    {
        std::string hexStr = "12";
        int64_t int64Value = HexToInt64(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl;
    }
    {
        std::string hexStr = "123";
        int64_t int64Value = HexToInt64(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl;
    }
    
    {
        std::string hexStr = "1234";
        int64_t int64Value = HexToInt64(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl;
    }
    
    {
        std::string hexStr = "0000000000000001";
        int64_t int64Value = HexToInt64(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl;
    }
    {
        std::string hexStr = "0000000000000000";
        int64_t int64Value = HexToInt64(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl;
    }
    
    {
        std::string hexStr = "0000000000000012";
        int64_t int64Value = HexToInt64(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl;
    }
    
    {
        std::string hexStr = "0000000000000123";
        int64_t int64Value = HexToInt64(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl;
    }
    
    {
        std::string hexStr = "9030090000106001";
        int64_t int64Value = HexToInt64(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl;
    }
    
    {
        std::string hexStr = "1030090000106001";
        int64_t int64Value = HexToInt64(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl;
    }

    {
        std::string hexStr = "103009000010600101";
        int64_t int64Value = HexToInt64(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl;
    }
}


void test_hex_2_int32(){
    std::cout<<std::endl<<"=============== test_hex_2_int32 =============="<<std::endl;

    {
        std::string hexStr = "0";
        int64_t int32Value = HexToInt32(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl;
    }
    {
        std::string hexStr = "F";
        int64_t int32Value = HexToInt32(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl;
    }
    {
        std::string hexStr = "00";
        int64_t int32Value = HexToInt32(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl;
    }
    {
        std::string hexStr = "00000000";
        int64_t int32Value = HexToInt32(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl;
    }
    {
        std::string hexStr = "00000001";
        int64_t int32Value = HexToInt32(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl;
    }
    {
        std::string hexStr = "00000100";
        int64_t int32Value = HexToInt32(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl;
    }
    {
        std::string hexStr = "00010000";
        int64_t int32Value = HexToInt32(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl;
    }
    {
        std::string hexStr = "12340000";
        int64_t int32Value = HexToInt32(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl;
    }
    {
        std::string hexStr = "00001234";
        int64_t int32Value = HexToInt32(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl;
    }
    {
        std::string hexStr = "12345678";
        int64_t int32Value = HexToInt32(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl;
    }
    {
        std::string hexStr = "87654321";
        int64_t int32Value = HexToInt32(hexStr);
        std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl;
    }
}

int main() {  

    test_int64_2_hex();
    test_int32_2_hex();

    test_hex_2_int64();
    test_hex_2_int32();

    return 0;  
}

// g++ convertBCD.cpp -o ./bin/cvt2 -std=c++11

 

标签:std,十六进制,int32,C++,LEN,int64Value,BYTE,hexStr
From: https://www.cnblogs.com/music-liang/p/18352114

相关文章

  • C++ 11 auto(自动类型推导) 和 decltype(获取表达式类型)
    C++(2)auto占位符自动类型推导auto能够实现类型的自我推导,并不代表一个实际的类型声明。auto只是一个类型声明的占位符。auto声明的变量,必须马上初始化,以让编译器推断出它的实际类型,并在编译时将auto占位符替换为真正的类型。注意:C++11中auto不能用于函......
  • 【C++】decltype
    1、简介我们之前使用的typeid运算符来查询一个变量的类型,这种类型查询在运行时进行。RTTI机制为每一个类型产生一个type_info类型的数据,而typeid查询返回的变量相应type_info数据,通过name成员函数返回类型的名称。同时在C++11中typeid还提供了hash_code这个成员函数,用于返回类型......
  • C++入门基础知识(笔记):成员变量和成员函数分开存储,非静态成员变量,是属于类的对象上,空对
    在C++中,类内的成员变量和成员函数分开存储只有非静态成员变量才属于类的对象上。1.空对象占用内存空间为:1个字节,代码演示:#include<iostream>usingnamespacestd;//成员变量和成员函数分开存储classPerson{};//这是一个空对象voidtest01(){ Personp;......
  • 2024年华为OD机试真题-求幸存数之和-(C++/Java/python)-OD统一考试(C卷D卷)
    2024华为OD机试真题目录-(B卷C卷D卷)-【C++JavaPython】_华为od机试csdn-CSDN博客 题目描述给一个正整数数列nums,一个跳数jump,及幸存数量left。运算过程为:从索引0的位置开始向后跳,中间跳过J个数字,命中索引为J+1的数字,该数被敲出,并从该点起跳,以此类推,直到幸存le......
  • C++入门:C语言到C++的过渡
    前言:C++——为弥补C缺陷而生的语言        C++起源于1979年,当时BjarneStroustrup在贝尔实验室工作,面对复杂软件开发任务,他感到C语言在表达能力、可维护性和可扩展性方面存在不足。        1983年,BjarneStroustrup在C语言基础上添加面向对象编程......
  • 第18 章探讨 C++新标准 移动语义和右值引用
    第18章探讨C++新标准移动语义和右值引用第18章探讨C++新标准移动语义和右值引用文章目录第18章探讨C++新标准移动语义和右值引用18.2移动语义和右值引用程序清单18.2useless.cpp18.2移动语义和右值引用现在介绍本书前面未讨论的主题。C++11支持移动语......
  • 【C++进阶学习】第十二弹——C++ 异常处理:深入解析与实践应用
    前言:在C++编程语言中,异常处理是一种重要的机制,它允许程序员在运行时捕获和处理错误或异常情况。本文将详细介绍C++异常处理的相关知识点,包括异常的定义、抛出与捕获、异常处理的原则、以及在实际编程中的应用。目录1.异常处理的基本概念1.1异常的定义1.2异常的抛出......
  • c++入门这一篇就够了!!!
    c++简介“c++”中的++来自于c语言中的递增运算符++,该运算符将变量加1。c++起初也叫”cwithclsss”.通过名称表明,c++是对C的扩展,因此c++是c语言的超集,这意味着任何有效的c程序都是有效的c++程序。c++程序可以使用已有的c程序库。     为什么c++不叫++c呢?因为它虽然对......
  • 【C++】模板(相关知识点讲解 + STL底层涉及的模板应用)
    目录模板是什么?模板格式模板本质函数模板格式介绍显式实例化模板参数匹配原则类模板类模板的实例化非类型模板参数模板特化——概念函数模板特化类模板的特化全特化半特化偏特化三种类特化例子(放一起比较)模板分离编译STL中比较经典的模板应用(不包含argus)......
  • 移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——4.模板
    1.泛型编程如何实现一个通用的交换函数呢?voidSwap(int&left,int&right){inttemp=left;left=right;right=temp;}voidSwap(double&left,double&right){doubletemp=left;left=right;right=temp;}voidSwap(char&left,char&right)......