JS常用代码
本文地址:http://tongxinmao.com/Article/Detail/id/181
function toAscii(valueStr) { // valueStr = document.form1.hex.value; valueStr = valueStr.toLowerCase(); var hex = "0123456789abcdef"; var text = ""; var i=0; for( i=0; i<valueStr.length; i=i+2 ) { var char1 = valueStr.charAt(i); if ( char1 == ' ' ) { i++; char1 = valueStr.charAt(i); } var char2 = valueStr.charAt(i+1); var num1 = hex.indexOf(char1); var num2 = hex.indexOf(char2); var value = num1 << 4; value = value | num2; var valueInt = parseInt(value); var symbolIndex = valueInt - 32; var ch = '?'; if ( symbolIndex >= 0 && value <= 126 ) { ch = symbols.charAt(symbolIndex) } if(valueInt==9){ //tab ch=" "; } if(valueInt==0x0a){ ch="<br/>"; } text += ch; } //document.form1.ascii.value = text; return text; } function toHex(valueStr) { // var valueStr = document.form1.ascii.value; var hexChars = "0123456789abcdef"; var text = ""; for( i=0; i<valueStr.length; i++ ) { var oneChar = valueStr.charAt(i); var asciiValue = symbols.indexOf(oneChar) + 32; var index1 = asciiValue % 16; var index2 = (asciiValue - index1)/16; if ( text != "" ) text += " "; text += hexChars.charAt(index2); text += hexChars.charAt(index1); } // document.form1.hex.value = text; return text; } // Utilities function $(id){ return document.getElementById(id); } function onkey(event){ if( event.keyCode==13){ send(); } } function getTimeStr() { var now = new Date(); var timestr =now.getFullYear()+"-"+(now.getMonth()+1)+"-"+now.getDate()+" "+ now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()+":"+now.getMilliseconds()+" "; return timestr; } function getTime() { $t=time(); //timestamp return (date("Y-m-d H:i:s",$t)); } function getRealIp(){ $ip=false; if(!empty($_SERVER["HTTP_CLIENT_IP"])){ $ip = $_SERVER["HTTP_CLIENT_IP"]; } if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']); if ($ip) { array_unshift($ips, $ip); $ip = FALSE; } for ($i = 0; $i < count($ips); $i++) { if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) { $ip = $ips[$i]; break; } } } return ($ip ? $ip : $_SERVER['REMOTE_ADDR']); } var o = new Object(); o.type='data'; o.content=msg ; var message = JSON.stringify(o); var char2 = valueStr.charAt(i+1); var num1 = hex.indexOf(char1); var valueInt = parseInt(value);
上一篇:LINUX(centos6)安装LIBCOAP[COAP-SERVER COAP-CLI]
下一篇:虚短和虚断分析运放电路