as3 tcpSocket发送HEX数据 发送16进制数据

将string转为HEX

function tenToHexFunc(_info:String):ByteArray{
            
            _info=_info.split(" ").join("");//删除所有的空格
            
            var byte:ByteArray = new ByteArray();
                for (var i:uint = 0; i < _info.length; i = i + 2)
                {
                    //trace(i);
                    byte.writeByte(uint("0x" + _info.substr(i, 2)))
                    trace(_info.substr(i, 2));
                }
                //writeBytes(byte)
            
            
            return byte;
        }

然后将转好的HEX数据发送给服务端

//向服务端发送HEX数据
        public function sendHEXFunc(msg:ByteArray):void
        {
            try
            {
                if( _client != null && _client.connected )
                {
                    _client.writeBytes(msg);
                    _client.flush(); 
                    //log( "Sent message to " + clientSocket.remoteAddress + ":" + clientSocket.remotePort );
                }
                else log("No socket connection.");
                if(!_client.connected){
                    _client=null;
                    collect(clientHost,clientPort);
                    _client.writeBytes(msg);
                    _client.flush();
                }
            }
            catch ( error:Error )
            {
                log( error.message );
                
            }
        }

发送的调用方法如下

tcpClient.sendHEXFunc(tenToHexFunc("aa ff ff 1b 01 cc bf 28"));

转载于:https://www.cnblogs.com/lingLuoChengMi/p/10419238.html

原文地址:https://www.cnblogs.com/dt1991/p/15242582.html