uint8_t/unsigned char 与string互转

这篇博客介绍了C++中如何将uint8_t数组转换为string,以及将string转换为const char*的过程。示例代码展示了如何遍历uint8_t数组构建string,并将两个string拼接,最后输出转换后的字符串及其对应的十六进制表示。
ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

一、uint8_t转string

#include <iostream>
using namespace std;
int main() {
    typedef uint8_t  U8;
    U8 Data[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F,0x2E };
        std::string temp;
        for (uint8_t i : Data)
        {
            temp += i;
        }

        std::cout << temp << std::endl;
        std::string tempa="a";
        temp=tempa+temp;
         std::cout << temp << std::endl;
	return 0;
}

 

二、string转char

#include <iostream>
using namespace std;
int main() {
    typedef uint8_t  U8;
    U8 Data[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F,0x2E };
        std::string temp;
        for (uint8_t i : Data)
        {
            temp += i;
        }

        std::cout << temp << std::endl;
        std::string tempa="a";
        temp=tempa+temp;
        std::cout << temp << std::endl;
        const char* mlog_s=temp.c_str();
        cout<<"a"<<mlog_s<<endl;
        while(* mlog_s!=0)
       {
         printf("%02x ", *mlog_s);
         mlog_s++;
       }
   return 0;
}

 

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

if(pThis->e_disp_type == 0x01)//emergency Tx, ID { if(bAlarmScrollState) { uint8_to_str(g_pstApp2Ps_Interface->digital.digital_signal.txDestId.dword_value,channelAlias); } else { MemClr(channelAlias, sizeof(channelAlias)); memcpy(channelAlias, st_DigitalEmergencyCtl.alias,sizeof(st_DigitalEmergencyCtl.alias)); UnicodeString2GbkString(channelAlias, sizeof(channelAlias)); } LcdDisplayStringReq(format_display_string_to_middle(channelAlias),16,5,155,0,channelAlias,BLACK,COLORNESS); } else if(pThis->e_disp_type == 0x02)//emergency Rx, ID { if(bAlarmScrollState) { uint8_to_str(g_pstApp2Ps_Interface->digital.digital_signal.txDestId.dword_value,channelAlias); } else { MemClr(channelAlias, sizeof(channelAlias)); memcpy(channelAlias, st_DigitalEmergencyCtl.alias,sizeof(st_DigitalEmergencyCtl.alias)); UnicodeString2GbkString(channelAlias, sizeof(channelAlias)); } LcdDisplayStringReq(format_display_string_to_middle(channelAlias),16,5,155,0,channelAlias,BLACK,COLORNESS); } uint32_t LcdDisplayStringReq(uint8_t x0,uint8_t y0,uint8_t startX,uint8_t endX,uint8_t offset,uint8_t *ptr,uint16_t fontcolor,uint16_t backcolor) { uint16_t char_data; uint8_t char_byte; uint32_t Cur_x = x0; GUI_CHARINFO *pFont; if(ptr == NULL) return -1; while(*ptr != '\0') { char_byte = GetOneChar2(ptr,&char_data); ptr += char_byte; pFont = GetFont(char_data); LCD_PutChar(Cur_x,y0,startX,endX,pFont,offset,fontcolor,backcolor); Cur_x += pFont->XSize; } return Cur_x; } void UnicodeString2GbkString(uint8_t *pbUnicodeStr, uint16_t len) ST_DIGITAL_EMERGENCY_CTL st_DigitalEmergencyCtl; typedef struct //数字警报系统 { uint8_t alias[32]; uint8_t Alarmtype; uint8_t AlarmMode; uint8_t PoliteRetries; uint8_t ImpoliteRetries; uint16_t RevertChannel; uint16_t EmergencyChannelBkp; uint8_t HotMicDurations; uint8_t EmergencyAlertIndex; timerId_t ChaInitTimer; uint8_t TxPromptEnable:1; uint8_t TxPromptIndex:5; uint8_t TxbitReserved:2; uint8_t RxPromptEnable:1; uint8_t RxPromptIndex:5; uint8_t bitEmergencyAliasDisplay:1; uint8_t bitExitRxEmergWithVoiceModeType:1; uint8_t tx_emerg_with_voice_cycle; uint8_t tx_emerg_with_voice_cycle_cnt; uint8_t RxCycleTime; timerId_t EmergVoiceTxTimer; timerId_t EmergVoiceRxTimer; }ST_DIGITAL_EMERGENCY_CTL; 这代码注释掉两行memcpy(channelAlias, st_DigitalEmergencyCtl.alias,sizeof(st_DigitalEmergencyCtl.alias));就能编译通过,否则报错:note: expected 'unsigned char *' but argument is of type 'const unsigned char *' 这是为什么
05-21
<think>我们遇到了多个类型冲突的错误。这些错误通常发生在以下情况: 1. 同一个类型在不同的地方被重复定义(例如,在头文件中多次包含导致重复定义,或者在不同的头文件中定义了相同的结构)。 2. 函数或类型的声明实际定义不一致。 3. 使用了未完全定义的类型。 根据错误信息,问题集中在`font_def_t`结构体和两个函数`oled_draw_char`、`oled_draw_string`上。 错误信息解读: 1. `conflicting types for 'font_def_t'`:在`font.h`文件的第13行,编译器认为`font_def_t`的类型之前定义的类型冲突。这里提到是`struct <anonymous>`,可能是指一个匿名结构体。这通常是因为在定义`font_def_t`时,可能在不同的地方有不同的定义方式(比如,一次定义为结构体,另一次定义为结构体别名?),或者重复定义。 2. `conflicting types for 'oled_draw_char'`和`conflicting types for 'oled_draw_string'`:这两个错误表明在`oled.c`文件中,函数的定义之前的声明不一致。注意,函数声明和定义必须完全一致(包括返回类型、参数类型和顺序)。 解决步骤: ### 步骤1:检查`font.h`中的`font_def_t`定义 在`font.h`中,我们可能定义了`font_def_t`结构体。请确保该结构体只定义一次,并且没有在其他地方重复定义。同时,注意是否使用了条件编译防止重复包含。 例如,在`font.h`中: ```c #ifndef FONT_H #define FONT_H // 定义字体结构体 typedef struct { const uint8_t width; // 字符宽度 const uint8_t height; // 字符高度 const uint16_t *data; // 字符点阵数据指针 } font_def_t; #endif // FONT_H ``` ### 步骤2:检查函数声明和定义的一致性 在头文件(可能是`oled.h`)中,应该有`oled_draw_char`和`oled_draw_string`的声明。在`oled.c`中,是它们的定义。我们需要确保声明和定义完全一致。 例如,在`oled.h`中: ```c #ifndef OLED_H #define OLED_H #include "font.h" // 函数声明 int oled_draw_char(oled_dev_t *dev, uint8_t x, uint8_t y, char c, const font_def_t *font, uint8_t color); int oled_draw_string(oled_dev_t *dev, uint8_t x, uint8_t y, const char *s, const font_def_t *font, uint8_t color); #endif // OLED_H ``` 在`oled.c`中,定义应该声明完全匹配: ```c #include "oled.h" int oled_draw_char(oled_dev_t *dev, uint8_t x, uint8_t y, char c, const font_def_t *font, uint8_t color) { // 函数体 } int oled_draw_string(oled_dev_t *dev, uint8_t x, uint8_t y, const char *s, const font_def_t *font, uint8_t color) { // 函数体 } ``` ### 步骤3:检查头文件包含顺序和依赖 确保在`oled.c`中首先包含`oled.h`,然后再包含其他头文件。这样,在`oled.h`中声明的函数在`oled.c`中定义时,编译器能够看到声明。 ### 步骤4:检查类型`oled_dev_t`是否完整定义 如果`oled_dev_t`在函数声明之前没有被完整定义,也可能导致问题。在`oled.h`中,应该包含定义`oled_dev_t`的头文件,或者前置声明。 例如: ```c // oled.h #ifndef OLED_H #define OLED_H // 前置声明oled_dev_t,如果不需要知道其内部结构 typedef struct oled_dev_t oled_dev_t; #include "font.h" // 函数声明... #endif ``` ### 步骤5:防止头文件重复包含 确保所有头文件都有`#ifndef ... #define ... #endif`保护。 ### 步骤6:检查是否在多个地方定义了`font_def_t` 如果在不同的头文件中都定义了`font_def_t`,则会出现冲突。应该只在一个地方定义它。 ### 示例修正后的代码 假设我们有以下文件结构: **font.h** ```c #ifndef FONT_H #define FONT_H #include <stdint.h> typedef struct { const uint8_t width; const uint8_t height; const uint16_t *data; } font_def_t; #endif // FONT_H ``` **oled.h** ```c #ifndef OLED_H #define OLED_H #include <stdint.h> #include "font.h" // 前置声明oled_dev_t,因为这里可能不需要其完整定义 typedef struct oled_dev_t oled_dev_t; int oled_draw_char(oled_dev_t *dev, uint8_t x, uint8_t y, char c, const font_def_t *font, uint8_t color); int oled_draw_string(oled_dev_t *dev, uint8_t x, uint8_t y, const char *s, const font_def_t *font, uint8_t color); #endif // OLED_H ``` **oled.c** ```c #include "oled.h" #include "font.h" // 函数定义 int oled_draw_char(oled_dev_t *dev, uint8_t x, uint8_t y, char c, const font_def_t *font, uint8_t color) { // 实现代码 return 0; } int oled_draw_string(oled_dev_t *dev, uint8_t x, uint8_t y, const char *s, const font_def_t *font, uint8_t color) { // 实现代码 return 0; } ``` ### 如果问题仍然存在 1. 检查编译器是否在编译不同文件时定义了不同的宏,导致结构体定义不同。 2. 检查是否有同名的结构体标签(struct tag)和typedef名称冲突。 ### 相关问题 根据以上问题,提出以下相关问题:</think>### 解决C/C++类型冲突问题 #### 问题分析 您遇到的错误是典型的C/C++类型冲突问题,主要涉及: 1. `font_def_t`结构体类型冲突 2. `oled_draw_char`和`oled_draw_string`函数声明定义不一致 根本原因通常是头文件包含顺序问题或声明/定义不匹配。 #### 解决方案 ##### 1. **头文件结构调整 (font.h)** ```c // font.h #ifndef FONT_H #define FONT_H #include <stdint.h> // 明确定义结构体标签 typedef struct font_def_t { uint8_t width; uint8_t height; const uint16_t *data; } font_def_t; #endif // FONT_H ``` ##### 2. **OLED驱动修正 (oled.h)** ```c // oled.h #ifndef OLED_H #define OLED_H #include "font.h" // 确保先包含font.h typedef struct { // OLED设备参数 } oled_dev_t; // 函数声明font.h中的定义保持一致 int oled_draw_char(oled_dev_t *dev, uint8_t x, uint8_t y, char c, const font_def_t *font, uint8_t color); int oled_draw_string(oled_dev_t *dev, uint8_t x, uint8_t y, const char *str, const font_def_t *font, uint8_t color); #endif // OLED_H ``` ##### 3. **OLED实现修正 (oled.c)** ```c // oled.c #include "oled.h" // 只包含oled.h,避免重复包含font.h // 函数定义声明完全一致 int oled_draw_char(oled_dev_t *dev, uint8_t x, uint8_t y, char c, const font_def_t *font, uint8_t color) { // 实现代码... return 0; } int oled_draw_string(oled_dev_t *dev, uint8_t x, uint8_t y, const char *str, const font_def_t *font, uint8_t color) { // 实现代码... return 0; } ``` #### 关键修正点解释 1. **结构体定义冲突** - 问题:匿名结构体(`struct <anonymous>`)导致类型不明确 - 解决: ```c // 错误方式 typedef struct { ... } font_def_t; // 正确方式 typedef struct font_def_t { ... } font_def_t; ``` 2. **函数签名不一致** - 问题:声明和定义中的参数类型不匹配 - 解决原则: - 声明和定义必须完全一致(包括`const`修饰符) - 使用头文件作为唯一声明源 3. **头文件包含顺序** ```mermaid graph LR A[oled.c] --> B[oled.h] B --> C[font.h] C --> D[stdint.h] ``` - 确保在需要使用`font_def_t`的地方,其定义已可见 - 避免循环包含 4. **类型明确化** - 对`uint8_t`等类型使用`#include <stdint.h>` - 避免隐式类型换 #### 完整项目结构建议 ``` AD7190_EVM/ ├── Core/ │ ├── Inc/ │ │ ├── font.h // 字体定义 │ │ ├── oled.h // OLED驱动声明 │ │ └── oled_dev.h // OLED设备结构体 │ ├── Src/ │ │ ├── oled.c // OLED驱动实现 │ │ └── main.c // 主程序 ├── Drivers/ └── ... ``` #### 调试技巧 如果问题仍然存在,添加类型检查代码: ```c // 在oled.c开头添加 static_assert(sizeof(font_def_t) == 12, "font_def_t size mismatch"); ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值