将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

posted on 2021-09-08 14:25  我是cdt  阅读(565)  评论(0编辑  收藏  举报