SUMMARY
The following Microsoft Knowledge Base article describes how to convert decimal integers to a hexadecimal string:
Q95717 How to Convert Decimal Numbers to Hexadecimal Numbers
However, the function in the article is limited in the size of the integer it can convert. In Visual FoxPro, the TRANSFORM() function can be used to convert decimal integers to hexadecimal strings. However, TRANSFORM() cannot handle negative decimal integers (for example, TRANSFORM(-2, "@0")).
This article demonstrates how to use the DecToHex function to convert any decimal integer (up to DWORD in size) to a hexadecimal string, regardless of the sign of the integer.
MORE INFORMATION
The following code demonstrates how to use the DecToHex() function to convert decimal integers to a string containing the equivalent hexadecimal number.
*-----------------------------------------------------------------
*- Function: DecToHex
*- Summary: Converts decimal integers to hex strings, whether
*- signed or unsigned.
*- Parameters: liDecNumber - decimal number to convert.
*-----------------------------------------------------------------
FUNCTION DecToHex
PARAMETERS liDecNumber
lsHexNumber = ""
IF liDecNumber > 0 && It's not negative, so do a straight TRANSFORM.
lsHexNumber = TRANSFORM(liDecNumber, "@0")
ELSE
*-- The number is negative, so we'll have to do a little more work,
*-- since it's not as straight forward as converting and adding a
*-- minus sign.
*-- Find the length of the resulting hex string.
lsHexNumber = TRANSFORM(ABS(liDecNumber), "@0")
IF SUBSTR(lsHexNumber, 3, 1) = "0" && The number has filled the
&& eight places of DWORD.
liLength = LEN(SUBSTR(lsHexNumber, NotAt("0", lsHexNumber, 2)))
ELSE
*-- Subtract to account for "0x".
liLength = LEN(TRANSFORM(ABS(liDecNumber), "@0")) - 2
ENDIF
*-----------------------------------------------------------------
*- Function: NotAt
*- Summary: From within a passed string, finds the first
*- occurrence of a character that is not the character
*- specified. In other words, this function works
*- opposite of the manner that AT() does.
*- Parameters: lsNotString - What we don't want. The function
*- finds the first character that is
*- not lsNotString.
*- lsSearchString - The string in which to search.
*- liOccurence - Indicates that NotAt should find
*- the liOccurence of a character
*- that is not lsNotString.
*-----------------------------------------------------------------
FUNCTION NotAt()
PARAMETERS lsNotString, lsSearchString, liOccurrence
llFound = .F. && Flag indicates if we've found a character that
&& is NOT lsNotString
llEndOfString = .F. && Flag to indicate that we've reached the end
&& of the string.
lnCounter = 0 && Tracks position during the search.
lnLength = LEN(lsSearchString)
liOccurCount = 0 && Tracks how many occurrences have been found.
*-- Loop until the desired character is found or the end of
*-- the string is reached.
DO WHILE NOT llFound AND NOT llEndofString
lnCounter = lnCounter + 1
lsCompare = SUBSTR(lsSearchString, lnCounter, 1)
IF lsCompare <> lsNotString
liOccurCount = liOccurCount + 1
*-- Have we found the occurrence we want?
IF liOccurCount = liOccurrence
llFound = .T.
ENDIF
ENDIF
IF lnCounter = lnLength
llEndOfString = .T.
ENDIF
ENDDO
RETURN lnCounter
*-- Code ends here.
REFERENCES
For additional information, please see the following articles in the Microsoft Knowledge Base:
Q95717 How to Convert Decimal Numbers to Hexadecimal Numbers
Q155389 How To Convert Values to Hexadecimal Format with Xbase
(c) Microsoft Corporation 1998. All Rights Reserved. Contributions by Mike A. Stewart, Microsoft Corporation.
be bakhshid awali Fox bood cancel zadama wali kar az kar ghozashte bood ;)
Val Function
Returns the numbers contained in a string as a numeric value of appropriate type.
Syntax
Val(string)
The required string argument is any valid string expression.
Remarks
The Val function stops reading the string at the first character it can't recognize as part of a number. Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized. However, the function recognizes the radix prefixes &O (for octal) and &H (for hexadecimal). Blanks, tabs, and linefeed characters are stripped from the argument.
The following returns the value 1615198:
Val(" 1615 198th Street N.E.")
In the code below, Val returns the decimal value -1 for the hexadecimal value shown:
Val("&HFFFF")
Note The Val function recognizes only the period (.) as a valid decimal separator. When different decimal separators are used, as in international applications, use CDbl instead to convert a string to a number.
motavajeh nashodam akhe val ke inkaro nemikone mikham 1 keygen besazam vase in mikham
man 1 chizi neveshtam vali kamel nis
tabdile mabna dige aval hexro migire age A bood arayaro mikone 10...
bad masalan age bood
A1
1*16^0+10*16^1 mikone
vali eroor mide:D
Private Sub Command1_Click()
Dim a(20) As String
For i = Len(Text1) - 1 To 0 Step -1
'For i = 0 To Len(Text1)
a(i) = Mid(Text1, i + 1, 1)
MsgBox ("a(" & i & ")= " & a(i))
Next i
For i = 0 To Len(Text1) - 1
Select Case Mid(Text1, i + 1, 1)
Case "A"
a(i) = 10
Case "B"
a(i) = 11
Case "C"
a(i) = 12
Case "D"
a(i) = 13
Case "E"
a(i) = 14
Case "F"
a(i) = 15
End Select
Next i
For i = Len(Text1) - 1 To 0 Step -1
'MsgBox a(i)
dec = dec + (a(i + 1) * (16 ^ i))
MsgBox ("a(" & i & ")= " & a(i))
MsgBox dec
Next i
Private Sub Command1_Click()
Dim a(20) As String
For i = 0 To Len(Text1) - 1
a(i) = Mid(Text1, i + 1, 1)
Next i
For i = 0 To Len(Text1) - 1
Select Case Mid(Text1, i + 1, 1)
Case "A"
a(i) = 10
Case "B"
a(i) = 11
Case "C"
a(i) = 12
Case "D"
a(i) = 13
Case "E"
a(i) = 14
Case "F"
a(i) = 15
End Select
Next i
For i = 0 To Len(Text1) - 1
dec = dec + (a(i) * 16 ^ (Len(Text1) - i - 1))
Next i
Text2 = dec
End Sub
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer,y az integer,z as integer
X = Val(textbox1.text)
Y = Val(textbox2.text)
z = X + y
label1.caption = Str$(z)
End Sub
End Class
Comment