Solved

Java to VB6 equivalent syntax

Posted on 2004-08-26
7
836 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:kwix
  • 4
  • 3
7 Comments
 
LVL 15

Expert Comment

by:Colosseo
ID: 11901712
Hi

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
0
 

Author Comment

by:kwix
ID: 11901814

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.

0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11901816
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:kwix
ID: 11901922
ok then.. so what is the purpose of  var sHex = "0123456789ABCDEF"; in the java code?
0
 
LVL 15

Accepted Solution

by:
Colosseo earned 300 total points
ID: 11902480
Hi kwix

sHex is needed to convert the integer into a hex

sHex contains the available hex values and is used in sHex.charAt() to return the correct hex value for the passed number

So for example sHex.charAt(4) returns 4 whereas sHex.charAt(12) returns C

The javascript example above when run for the value i = 12 will give

h = 0C000000 but return the substring of the first two characters, 0C

On the first pass through the for loop

(i >> (j * 8 + 4)) & 0x0F will equal 0 and (i >> (j * 8)) & 0x0F will equal 12

i >> (j * 8 + 4) means i converted to binary (1100) and then in this case shifted four bits to the right = 0
i >> (j * 8) & 0x0F  means i converted to binary (1100) and then shifted zero bits to the right = 12

these values are then each passed through the sHex.CharAt giving 0C

For the remainder of the passes through the loop 0 is returned always

What the javascript is doing is writing the hex out in reverse and getting the first two characters whereas the vb rights the sting out forwards and then reads the last two

If you need any more explanation(although I think this makes sense it might all be in my head :)  ) then just shout

Cheers

Scott
0
 

Author Comment

by:kwix
ID: 11902875
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.

0
 

Author Comment

by:kwix
ID: 11903775
ok. i got it. thanks. :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now