c++ - 从 const 字符串转换为 const u8_t 时出现问题 *
问题描述
我有以下代码
在 .hpp 文件中
class A
{
private:
static std::string hello;
}
在 .cpp 文件中
const std::string hello= "hi";
B(hello, strlen(hello)); // where B(const u8_t* a, u8_t b)
我应该使用哪个演员表?我想const u8_t *
最终将 const 字符串转换为。u8_t 是无符号字符
解决方案
u8_t
是非标准类型。我假设它与标准相同uint8_t
。
另一个悬而未决的问题是该B
类型是否以任何特殊方式处理空字节,我假设不是。
所以strlen(hello)
不编译。那应该替换为hello.size()
,这是获取 C++ 字符串大小的正确方法。
最后回答你的问题,std::string
有一个data
方法可以返回指向字符数据的指针。该指针是char*
您需要u8_t*
为构造函数强制转换的类型。
所以把它放在一起你得到
B(reinterpret_cast<u8_t*>(hello.data()), hello.size());
推荐阅读
- google-apps-script - 您的脚本(Gmail 的每月统计信息)最近发送邮件失败
- c# - ASP.NET MVC iTextSharp,是否可以在代码隐藏中动态生成 PDF 文件?
- python - 在 Python 中查找/测试未修饰的字符串文字(没有 b" 或 u")
- c++ - 使用递归反转链表
- python - 在 tensorflow 中索引 3D 批处理张量
- c++ - 设置窗口像素的正确(且无闪烁)方式?
- reactjs - 无法读取未定义的属性“setData” - 反应触摸事件
- azure - Azure ML 在 MS Excel 中添加错误
- spring - Spring Boot 2.0.2.RELEASE 的模型验证异常
- python - Python MySQLdb,完成插入查询执行,忽略错误插入