Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java to VB6 equivalent syntax

Posted on 2004-08-26
7
Medium Priority
?
913 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

971 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