yes4me
asked on
a conversion from decimal to hex and vise versa
I need 3 VBScript functions something like
- convertToHex(decnumber)
- convertToDecimal(hexnumber )
- shift right function
Now that may sound easy but I cannot arrive to make the conversion function to work for the values
4294967295 for convertToHex
FFFFFFFF for convertToDecimal
I get an overflow message error.
And the last function, the shift right, is also wrong by 1! Try it with shr(4294967295,4) to understand what I mean and check the result with MS calculator.
My functions:
'Return a long number in hexadecimal (i.e. hex(69) )
function hexa(n)
tmpResult = hex(n)
for i=len(tmpResult) to 7 step 1
tmpResult = "0" + tmpResult
next
hexa = LCase(tmpResult)
end function
'Return a hexadecimal number in decimal (i.e. deci(&HFF) )
function deci(n)
deci = clng(n)
end function
'function shift right by b
function shr(a,b)
for i=1 to b step 1
a = int(a/2)
next
shr = a
end function
Maybe you guys can end up with a solution.
- convertToHex(decnumber)
- convertToDecimal(hexnumber
- shift right function
Now that may sound easy but I cannot arrive to make the conversion function to work for the values
4294967295 for convertToHex
FFFFFFFF for convertToDecimal
I get an overflow message error.
And the last function, the shift right, is also wrong by 1! Try it with shr(4294967295,4) to understand what I mean and check the result with MS calculator.
My functions:
'Return a long number in hexadecimal (i.e. hex(69) )
function hexa(n)
tmpResult = hex(n)
for i=len(tmpResult) to 7 step 1
tmpResult = "0" + tmpResult
next
hexa = LCase(tmpResult)
end function
'Return a hexadecimal number in decimal (i.e. deci(&HFF) )
function deci(n)
deci = clng(n)
end function
'function shift right by b
function shr(a,b)
for i=1 to b step 1
a = int(a/2)
next
shr = a
end function
Maybe you guys can end up with a solution.
Are you aware that there is an hex function already in ASP?
the format hex(number)
so
x= hex(69) 'should assign a value of 45 to x
the format hex(number)
so
x= hex(69) 'should assign a value of 45 to x
BTW running the hex function for gives me an overflow error also. I suspect there's an upper limit to the hex function
And I have no idea what the shift right function should return. what is it? if you dont mind the question
mberumen
mberumen
You're forgetting that a long represents a SIGNED 32-bit number. So the valid range for a long is:
-2147483648 to 2147483647
Because it's signed:
FFFFFFFF = -1 (not 4294967295)
If you want to deal with unsigned numbers you have to do a little bit of manipulation.
function hexa(n)
temp = n
if temp > 2147483647 then 'convert it to its corresponding negative value
temp = temp - 4294967296
end if
hexa = hex(temp)
end function
function deci(n)
temp = clng(n)
if temp < 0 then temp = 4294967296 + temp 'convert it to an unsigned value
deci = temp
end function
function shr(a,b)
for i=1 to b step 1
a = int(a / 2)
next
shr = a
end function
response.write shr(4294967295,4) & "<BR>"
response.write deci(&hFFFFFFFF) & "<BR>"
response.write hexa(4294967295) & "<BR>"
And your shift function is working fine.
0xFFFFFFFF (unsigned 4294967295) >> 4 = 0x0FFFFFFF (268435455)
Which is what it produces.
-2147483648 to 2147483647
Because it's signed:
FFFFFFFF = -1 (not 4294967295)
If you want to deal with unsigned numbers you have to do a little bit of manipulation.
function hexa(n)
temp = n
if temp > 2147483647 then 'convert it to its corresponding negative value
temp = temp - 4294967296
end if
hexa = hex(temp)
end function
function deci(n)
temp = clng(n)
if temp < 0 then temp = 4294967296 + temp 'convert it to an unsigned value
deci = temp
end function
function shr(a,b)
for i=1 to b step 1
a = int(a / 2)
next
shr = a
end function
response.write shr(4294967295,4) & "<BR>"
response.write deci(&hFFFFFFFF) & "<BR>"
response.write hexa(4294967295) & "<BR>"
And your shift function is working fine.
0xFFFFFFFF (unsigned 4294967295) >> 4 = 0x0FFFFFFF (268435455)
Which is what it produces.
ASKER
To mberumen:
Shift right mean that a binary number like 0101110 will become 0010111
To clockwatcher:
Big thanks but I still don't understand how come my shift is working? I mean if I try using MS calculator and input FFFFFFFF divide it by 2 four times and click on decimal, I will get 268435456!
Any idea why?
Shift right mean that a binary number like 0101110 will become 0010111
To clockwatcher:
Big thanks but I still don't understand how come my shift is working? I mean if I try using MS calculator and input FFFFFFFF divide it by 2 four times and click on decimal, I will get 268435456!
Any idea why?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Big thanks go to clockwatcher
ASKER