function show(){
var str = prompt("请输入几个字符","")//接收字符串
var strAscii = new Array()//用于接收ASCII码
for(var i = 0 i <str.length i++ ){
strAscii[i] = str.charCodeAt(i)//只能把字符串中的字符一个一个的解码
}
var getAscii = ""//把这些ASCII码按顺序排列
for(var i = 0 i <strAscii.length i++ ){
getAscii += strAscii[i]
getAscii += " "
}
alert("这些字符的ASCII码依次是:"+getAscii)//输出结果给人看
}
js对一个字符串根据ASCII码从小到大排序(字典序)实现思路: 将带空格分隔的字符串split成数组,利用数组sort()排序方法,在该方法里面用chartCodeAt()方法比对元素间同位置的字符的ASCII码(同一位置字符的ASCII码相同则继续比对下一个字符)
function convert (str) {var matches = str.match(/\d{1,8}/g)
var len = matches.length
str = ''
for (var i = 0 i < len i++) {
str += String.fromCharCode(parseInt(matches[i], 2))
}
return str
}
var s = convert('011000010110001001100011')
alert(s)