Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for dec2hex.vbs

(view source code of dec2hex.vbs as plain text)

  1. ' Check command line parameters
  2. Select Case WScript.Arguments.Count
  3. 	Case 1
  4. 		' Check if parameter is a valid number
  5. 		On Error Resume Next
  6. 		lngDec = CLng( WScript.Arguments(0) )
  7. 		If Err Then Syntax
  8. 		On Error GoTo 0
  9. 		' Check if number is within range
  10. 		If lngDec > 2147483647 Then Syntax
  11. 	Case Else
  12. 		Syntax
  13. End Select
  14.  
  15. ' Convert number to string representing hexadecimal value
  16. strHex = CStr( Hex( lngDec ) )
  17. ' Add leading zeros if necessary
  18. intLen = Len( strHex )
  19. if intLen < 8 Then
  20. 	Do Until intLen = 8
  21. 		strHex = "0" & strHex
  22. 		intLen = Len( strHex )
  23. 	Loop
  24. End If
  25.  
  26. ' Display the result
  27. WScript.Echo vbCrLf & lngDec & " = 0x" & strHex
  28.  
  29. ' Done
  30. WScript.Quit(0)
  31.  
  32.  
  33. Sub Syntax
  34. 	strMsg = vbCrLf & "Dec2Hex.vbs,  Version 1.00" & vbCrLf _
  35. 	       & "Convert specified decimal number to 8 digit hexadecimal" _
  36. 	       & vbCrLf & vbCrLf _
  37. 	       & "Usage:  CSCRIPT  DEC2HEX.VBS  number" _
  38. 	       & vbCrLf & vbCrLf _
  39. 	       & "Where:  " & Chr(34) & "number" & Chr(34) _
  40. 	       & " is any decimal number ranging from 0..2147483647 (0x7FFFFFFF)" _
  41. 	       & vbCrLf & vbCrLf _
  42. 	       & "Written by Rob van der Woude" & vbCrLf _
  43. 	       & "http://www.robvanderwoude.com"
  44. 	WScript.Echo strMsg
  45. 	WScript.Quit(1)
  46. End Sub
  47.  

page last uploaded: 2017-04-06, 13:33