kwix
asked on
Java to VB6 equivalent syntax
I've been trying to convert this java code to VB6.
function intToHex(i)
{
var sHex = "0123456789ABCDEF";
h = "";
i = parseInt(i);
for(j = 0; j <= 3; j++) {
h += sHex.charAt((i >> (j * 8 + 4)) & 0x0F) +
sHex.charAt((i >> (j * 8)) & 0x0F);
}
return h.substring(0,2);
}
Im not really sure but I think the code inside the loop is searching for any matching character(s) that matches any of the enumerated characters in the "sHex" variable (like the built-in Instr VB function). I'm not sure what the "(i >> (j * 8 + 4)" does.--And it is a parameter of charAt. Does the parameter indicate a bit shift? or something else?
It would really help if someone can translate the java function above to a VB6 function. Thanks.
chris
function intToHex(i)
{
var sHex = "0123456789ABCDEF";
h = "";
i = parseInt(i);
for(j = 0; j <= 3; j++) {
h += sHex.charAt((i >> (j * 8 + 4)) & 0x0F) +
sHex.charAt((i >> (j * 8)) & 0x0F);
}
return h.substring(0,2);
}
Im not really sure but I think the code inside the loop is searching for any matching character(s) that matches any of the enumerated characters in the "sHex" variable (like the built-in Instr VB function). I'm not sure what the "(i >> (j * 8 + 4)" does.--And it is a parameter of charAt. Does the parameter indicate a bit shift? or something else?
It would really help if someone can translate the java function above to a VB6 function. Thanks.
chris
ASKER
I know how to use the built-in Hex conversion function in VB..but obviously, the code above does more besides Hex conversion. as "sHex" is present, i think it is a "pattern" string/character that the function will refer to when executing the code (correct me if im wrong). What confuses me the most is the code inside the loop (see below):
h += sHex.charAt((i >> (j * 8 + 4)) & 0x0F) +
sHex.charAt((i >> (j * 8)) & 0x0F);
If anyone can convert the java function to the CORRECT equivalent VB6 function, i'll give the points immediately, and maybe increase it further. Thanks.
Sorry kwix
on second inspection it seems to be doing a bit more
it seems to be reading an integer then converting it to its hex form but in reverse
so for the integer 1234
your function returns sHex as D2040000 and then extracts just the first two characters using the substring .. in this case D2
To properly replicate this you could use a function like this.
Private Function intToHex(i)
' Convert the integer to its hex equivalent
hexnum = Hex(i)
' If the hex value has only one character then add a leading 0
If Len(hexnum) = 1 Then hexnum = "0" & hexnum
' Return the Last two characters
intToHex = Right(hexnum, 2)
End Function
This converts the integer to hex but without reversing the order number so it reads the last two characters rather than the first two.
Regards
Scott
on second inspection it seems to be doing a bit more
it seems to be reading an integer then converting it to its hex form but in reverse
so for the integer 1234
your function returns sHex as D2040000 and then extracts just the first two characters using the substring .. in this case D2
To properly replicate this you could use a function like this.
Private Function intToHex(i)
' Convert the integer to its hex equivalent
hexnum = Hex(i)
' If the hex value has only one character then add a leading 0
If Len(hexnum) = 1 Then hexnum = "0" & hexnum
' Return the Last two characters
intToHex = Right(hexnum, 2)
End Function
This converts the integer to hex but without reversing the order number so it reads the last two characters rather than the first two.
Regards
Scott
ASKER
ok then.. so what is the purpose of var sHex = "0123456789ABCDEF"; in the java code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, I guess thats the clearest explanation i can have. :)
But VB6 does not have built-in bit shifting. Can you help create the exact function replica in VB6? ..same as the javascript function?
afterwards.. the points are yours.
But VB6 does not have built-in bit shifting. Can you help create the exact function replica in VB6? ..same as the javascript function?
afterwards.. the points are yours.
ASKER
ok. i got it. thanks. :)
The code above is converting an integer i in to its hex equivalent
in visual basic you can use Hex to get the hex value
for example:
i = 234234
h = Hex(i)
msgbox h
HTH
Scott