Solved

Java to VB6 equivalent syntax

Posted on 2004-08-26
7
895 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…
Suggested Courses
Course of the Month5 days, 17 hours left to enroll

627 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