2009. május 12., kedd
Converting To and From Hexadecimal Numbers
Problem/Question/Abstract:
I have seen quite a few articles on how to convert a decimal number to hexa-decimal, yet none have used the most obviuos function. Delphi offers the solution already!
Answer:
The solution is quite simple.
If you have an Integer intX and you want to convert it into an hexadecimal number use IntToHex(intNumber, intDigits)
Expample 1:
strHex = IntToHex(intX, 2);
Delphi will create a hexadecimal string representing intX with at least two digits. Therefore if intX is 10, strHex will Hold 0A.
To convert back to decimal you can use either StrToInt or StrToIntDef. However, you must ensure that the string contains a leading dollar-sign ($).
Example 2.1 (Completting this article using Example 1):
intX = StrToInt('$' + strHex);
Delphi will take strHex (0A), you may have to provide the leading $ and will return the Decimal Value 10.
Example 2.2 (Completting this article using Example 1):
intX = StrToIntDef('$' + strHex, -1);
Delphi will take strHex (0A), you may have to provide the leading $ and will return the Decimal Value 10. If the function fails, it will return the pre-defined value -1.
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése