C++ char* 与 string 互相转换

string 是c++标准库里面其中一个,封装了对字符串的操作,可以与char*进行互相转换。

string 转 char*

1.

string str="abc"; 
char *p=str.data(); 

string的data()函数返回的字符串指针不会以'\0'结束,千万不可忽视。

2.

string str="gdfd"; 
char *p=str.c_str();

char* 转 string

对于C风格的字符串比如char*,char str[],转换为 string
可用"="直接进行赋值
如:

char* chstr;
char arstr[];
string str1 = chstr;
string str2 = arstr;

socket解包-as3方式

protected function onProgressData(event:ProgressEvent):void
    {
        _buffer.position = _bufferPosition;
        readBytes( _buffer , _buffer.length , bytesAvailable );
        
        while( _buffer.bytesAvailable > 1 )
        {
            var leftDataLength:int = _buffer.length - _bufferPosition;
            var msgDataLength:int = _buffer.readShort();
            if( leftDataLength >= msgDataLength )
            {
                var byteArray:ByteArray = new ByteArray();
                byteArray.endian = Endian.LITTLE_ENDIAN;
                _buffer.position = _bufferPosition;
                _buffer.readBytes( byteArray , 0 , msgDataLength);
                parseOneMessageBytes( byteArray );
                _bufferPosition += msgDataLength;//指针增加已读数据
            }
            else
            {//如果不够一个消息的长度,等待下个进度事件时再拼装
                break;
            }
        }
    }
    
    /**
     *        解析原始消息数据,并做分发 
     * @param bytes
     * 
     */        
    protected function parseOneMessageBytes( bytes:ByteArray ):void
    {
        bytes.endian = Endian.LITTLE_ENDIAN;
        var len:int;
        var msgId:int;
        var check:int;
        var rawData:ByteArray;
        try
        {
            len = bytes.readShort();        //2 Bytes
            msgId = bytes.readShort();        //2 Bytes
            check = bytes.readInt();        //4 Bytes
        }
        catch(e:Error)
        {
            throw new IllegalOperationError("wrong protocal header!");
        }
        rawData = new ByteArray();
        rawData.endian = Endian.LITTLE_ENDIAN;
        bytes.readBytes( rawData , 0 , len - 8 );
        rawData.position = 0;
        
        var decodeInfos:Array = _decodeMessages[ msgId ];
        if( decodeInfos == null )
        {
            dispatchEvent( new NetEvent( NetEvent.NO_REGISTED_DECODE_CLASS_FOR_MSG , msgId) );
        }
        //挨个通知解码类
        for each (var decodeInfo:HttpDecodeClassVo in decodeInfos) 
        {
            var decodeClass:Class = decodeInfo.decodeClass;
            var msg:BaseMsg = new decodeClass();
            try{
                msg.decode( rawData );
            }
            catch( error:Error )
            {
                if( error.errorID == 2030 )
                    throw new IllegalOperationError( "消息" + msgId + "解析时遇到文件尾! ");
            }
            msg.traceBinary( msgId , rawData );
            if( rawData.bytesAvailable )
            {
                dispatchEvent( new NetEvent( NetEvent.RAW_DATA_LEFT , msgId) );
            }
            else
            {
                if( msg.errorId )
                {
                    var msgErrorVo:MsgErrorVo = new MsgErrorVo();
                    msgErrorVo.msgId = msgId;
                    msgErrorVo.errorId = msg.errorId;
                    dispatchEvent( new NetEvent( NetEvent.MSG_ERROR , msgErrorVo ) );
                }
            }
            if(msg.errorId == 0 || decodeInfo.allowError)
            {
                if( decodeInfo.callbackFunction != null )
                {
                    decodeInfo.callbackFunction( msg );
                }
            }
            rawData.position = 0;
        }
    }