如何将OMstring类型转换为char*
How to typecast OMstring to char *
我正在使用Rhapsody OMString,我的代码需要将OMString类型转换为char*
reader.readStream((unsigned char *)MachineType,machineTypeDataSize); // MachineType is OMString
是否可以将OMString类型转换为char*
如果您查看文档(http://pic.dhe.ibm.com/infocenter/rhaphlp/v7r6/index.jsp?topic=%2Fcom.ibm.rhp.frameworks.doc%2Ftopics%2Frhp_r_fw_omstring_class.html),您可以始终使用operator[]为自己构建一个小型实用程序函数,将OMString对象转换为std::string,然后使用std::string的c_str()方法获取其const char*content:
#include <string>
#include <omstring.h>
std::string omStringToStdString( const OMString& in )
{
std::string result( in.GetLength(), ' ' );
for ( std::string::size_type i = 0; i < result.length(); ++i )
{
result[ i ] = in[ i ];
}
return result;
}
编辑:显然OMString有一个运算符*,它返回一个指向字符串内容的const char*指针:(http://pic.dhe.ibm.com/infocenter/rhaphlp/v7r6/index.jsp?topic=%2Fcom.ibm.rhp.frameworks.doc%2Ftopics%2Frhp_r_fw_operator_customize.html),所以你实际上并不需要我上面发布的转换函数。
编辑#2:好吧,在重读OP的问题时,我现在的印象是,他不想从OMString中阅读,而是想写OMString。在这种情况下,OMString::GetBuffer()方法可以直接访问对象的内部缓冲区。但是要小心,如果写入的字符数超过缓冲区的空间,则很容易损坏OMString对象的内部状态(并且可能在缓冲区实际结束之前插入\0个字符)。
- 使用char类型将decimal转换为string,将string转换为decimal
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 从"wcslen"替换到"strnlen_s"时,用"const char*"进行类型转换是正确的方法吗?
- 此错误的含义是什么:从 'char* [40] 转换为非标量类型 'std::string
- 如何将std :: Basic_string类型转换为char类型的数组
- 为什么C/C++会自动将char/wchar_t/short/bool/enum类型转换为int
- 使用Arduino将String转换为const-char*类型
- 如何将uint32_t转换为char*类型
- 如何将DWORD或char*类型的变量转换为LPCWSTR
- 无法将'DATA_UNIT::getc1'从类型 'char (DATA_UNIT::)()' 转换为类型 'char'
- 将静态数组转换为 char* 并管理类型转换
- char(*)[]类型转换的向量
- 如何将OMstring类型转换为char*
- 如果我不做const_cast<char*>而简单地使用(char *)类型转换有什么害处吗?
- 在静态const成员数组中键入将char强制转换为字符串的类型
- 将char[]强制转换为usingned int给出:取消引用类型的punned指针将打破严格的别名规则
- 将fpos_t转换为int或char类型
- 无法将char类型转换为int
- 错误:在 C++ 中从 'const char [5]' 类型转换为非标量类型
- 将任何有符号char类型转换为无符号char类型