可以照如下方式进行转换:
Js代码
var hex="0x29"//十六进制
var charValue = String.fromCharCode(hex)//生成Unicode字符
var charCode = charValue.charCodeAt(0)//获取指定字符的十进制表示.
var hexOri="0x"+charCode.toString(16)//将int值转换为十六进制
alert("hex:"+hex+"\ncharValue:"+charValue+"\ncharCode:"+charCode+"\nhexOri:"+hexOri)
很简单,每两个字符前面加一个%,然后用decodeURI转换就行了文件必须存成utf-8格式
<html><head>
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />
</head>
<body>
<div id="d1">
<script type="text/javascript">
var hi = '7465737420e6b58be8af95'
//var hj = '%74%65%73%74%20%e6%b5%8b%e8%af%95'
var hj = prePro(hi)
document.write(hi)
document.write('<br />')
document.write(hj)
document.write('<br />')
document.write(decodeURI(hj))
function prePro(data)
{
if (data.length % 2) return ''
var tmp=''
for(i=0i<data.lengthi+=2)
{
tmp += '%' + data.charAt(i) + data.charAt(i+1)
}
return tmp
}
</script>
</div>
</body>
</html>
调用的例子:
console.log(ColorLuminance(colorRGB(),0.5)) //先自动生成16进制颜色,在转成比原有颜色轻50%;
//16进制随机颜色
function colorRGB(){
return '#' + (function (h) {
return new Array(7 - h.length).join("0") + h
})((Math.random() * 0x1000000 << 0).toString(16))
}
ColorLuminance接受两个参数:
参数1: 十六进制颜色值,例如“#abc”或“#123456”(散列是可选的)
参数2: 亮度因数,即,-0.1是10%更暗,0.2是轻20%等。
function ColorLuminance(hex, lum) {
// validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '')
if (hex.length < 6) {
hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2]
}
lum = lum || 0
// convert to decimal and change luminosity
var rgb = "#", c, i
for (i = 0 i < 3 i++) {
c = parseInt(hex.substr(i*2,2), 16)
c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16)
rgb += ("00"+c).substr(c.length)
}
return rgb
}
相关资源:https://blog.csdn.net/dingshi7798/article/details/105829757