Solved

Java to VB6 equivalent syntax

Posted on 2004-08-26
7
826 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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