Friday, 15 June 2012

Convert Hex to String using VbScript -



Convert Hex to String using VbScript -

here script convert string hex code:

strstring = "test" strhex ="" i=1 len(strstring) strhex = strhex & " " & hex(asc(mid(strstring,i,1))) next strhex = right(strhex,len(strhex)-1) wscript.echo strhex

i want reverse action converts hex string, possible using vbscript?

vbscript uses "&h" mark numbers hexadecimals.

>> wscript.echo chr("&h" & "41") >> >>

demonstrates strategy in principle. demo code:

option explicit function s2a(s) redim a(len(s) - 1) dim = 0 ubound(a) a(i) = mid(s, + 1, 1) next s2a = end function function s2h(s) dim : = s2a(s) dim = 0 ubound(a) a(i) = right(00 & hex(asc(a(i))), 2) next s2h = join(a) end function function h2s(h) dim : = split(h) dim = 0 ubound(a) a(i) = chr("&h" & a(i)) next h2s = join(a, "") end function dim s : s = "test" wscript.echo 0, s wscript.echo 1, s2h(s) wscript.echo 2, h2s(s2h(s))

output:

0 test 1 74 65 73 74 2 test

update wrt comment/unicode:

use ascw/chrw (vb ref) deal utf 16.

option explicit function s2a(s) redim a(len(s) - 1) dim = 0 ubound(a) a(i) = mid(s, + 1, 1) next s2a = end function function s2h(s) dim : = s2a(s) dim = 0 ubound(a) a(i) = right("0000" & hex(ascw(a(i))), 4) next s2h = join(a) end function function h2s(h) dim : = split(h) dim = 0 ubound(a) a(i) = chrw("&h" & a(i)) next h2s = join(a, "") end function dim s : s = "abcä" & chrw("&h" & "d98a") wscript.echo 0, s wscript.echo 1, s2h(s) wscript.echo 2, h2s(s2h(s))

vbscript

No comments:

Post a Comment