如何将OMstring类型转换为char*

How to typecast OMstring to char *

本文关键字:char 类型转换 OMstring      更新时间:2023-10-16

我正在使用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个字符)。