Solved

a conversion from decimal to hex and vise versa

Posted on 2000-05-03
8
475 Views
Last Modified: 2008-03-17
I need 3 VBScript functions something like
- convertToHex(decnumber)
- convertToDecimal(hexnumber)
- shift right function

Now that may sound easy but I cannot arrive to make the conversion function to work for the values

4294967295 for convertToHex
FFFFFFFF for convertToDecimal

I get an overflow message error.
And the last function, the shift right, is also wrong by 1! Try it with shr(4294967295,4) to understand what I mean and check the result with MS calculator.



My functions:
'Return a long number in hexadecimal (i.e. hex(69) )
function hexa(n)
    tmpResult = hex(n)
    for i=len(tmpResult) to 7 step 1
        tmpResult = "0" + tmpResult
    next
    hexa = LCase(tmpResult)
end function

'Return a hexadecimal number in decimal (i.e. deci(&HFF) )
function deci(n)
    deci = clng(n)
end function

'function shift right by b
function shr(a,b)
    for i=1 to b step 1
        a = int(a/2)
    next
    shr = a
end function


Maybe you guys can end up with a solution.
0
Comment
Question by:yes4me
  • 3
  • 3
  • 2
8 Comments
 

Author Comment

by:yes4me
ID: 2775715
Adjusted points from 50 to 100
0
 
LVL 4

Expert Comment

by:mberumen
ID: 2775726
Are you aware that there is an hex function already in ASP?

the format hex(number)

so

x= hex(69) 'should assign a value of  45 to x
0
 
LVL 4

Expert Comment

by:mberumen
ID: 2775735
BTW running the hex function for gives me an overflow error also.  I suspect there's an upper limit to the hex function

 
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 4

Expert Comment

by:mberumen
ID: 2775741
And I have no idea what the shift right function should return.  what is it? if you dont mind the question

mberumen
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 2775963
You're forgetting that a long represents a SIGNED 32-bit number.  So the valid range for a long is:

   -2147483648 to 2147483647

Because it's signed:

  FFFFFFFF = -1 (not 4294967295)

If you want to deal with unsigned numbers you have to do a little bit of manipulation.

function hexa(n)
  temp = n
  if temp > 2147483647 then 'convert it to its corresponding negative value
     temp = temp - 4294967296
  end if
  hexa = hex(temp)
end function
     
function deci(n)
  temp = clng(n)
  if temp < 0 then temp =  4294967296 + temp 'convert it to an unsigned value
  deci = temp
end function

function shr(a,b)
    for i=1 to b step 1
        a = int(a / 2)
    next
    shr = a
end function

  response.write shr(4294967295,4) & "<BR>"
  response.write deci(&hFFFFFFFF) & "<BR>"
  response.write hexa(4294967295) & "<BR>"

And your shift function is working fine.

  0xFFFFFFFF (unsigned 4294967295) >> 4 = 0x0FFFFFFF (268435455)

Which is what it produces.

0
 

Author Comment

by:yes4me
ID: 2777608
To mberumen:
Shift right mean that a binary number like 0101110 will become 0010111

To clockwatcher:
Big thanks but I still don't understand how come my shift is working? I mean if I try using MS calculator and input FFFFFFFF divide it by 2 four times and click on decimal, I will get 268435456!

Any idea why?
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 100 total points
ID: 2779131
I have no idea what you're doing in calculator, but if you switch to hex mode type in FFFFFFFF and divide it by 2 (4 times) you get the following:

FFFFFFFF / 2 =
7FFFFFFF / 2 =
3FFFFFFF / 2 =
1FFFFFFF / 2 =
FFFFFFF =  268435455 (decimal)

Which is the exact same result that you get via your shift function.  I don't know what procedure you're following to get your result, but whatever it is you're doing you're getting an incorrect result.  I'm assuming that you're going through the procedure incorrectly and doing some kind of rounding somewhere (e.g., you may be switching back and forth between hex and decimal or something when you've got fractional parts).

It's intuitively obvious your calculator result is wrong.  If you right shift a binary number made up of all ones (like FFFFFFFF), you're going to end up with an odd number as the least significant bit will always be one.
0
 

Author Comment

by:yes4me
ID: 2781564
Big thanks go to clockwatcher
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net VB.net gather value of text box text change 4 49
Calculating value of records from a column - classic asp 7 79
Time/Date Query 11 40
XML Parsing Classic ASP 5 56
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

806 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