将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"));

 

版权声明:本文为lingLuoChengMi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/lingLuoChengMi/p/10419238.html