Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert from hexadecimal to Decimal 2

Status
Not open for further replies.

akodey

MIS
Jan 9, 2003
2
KE
i am looking to convert hexadecimal value fields into decimal value fields. is there a formula for this
 
Hey Akodey,

There isn't any Crystal function which allows for conversion between decimal and hex.

Unless you're prepared to write your own dll for this, the most obvious way for you to achieve this would be to convert the number on your database, prior to reporting on it. I don't know what database you're using, but CAST is your man, if you're on a compliant database. If you're on Oracle, try utl_raw.cast_to_number. (I know for a fact that this works as utl_raw.cast_to_varchar2 but I've not tried it as number.)

All the best,

Naith
 
Now I think about it, I think there is a UFL you can download for this, called uflmath.exe, or something. I think this includes a HexToNumber function, or vice versa, or both.

Have a look on and the same URL /library.

Good luck,

Naith
 
If, like me, you don't like using UFLs (after all they must be installed on each machine that uses the report), you might like to use this:

<code>
// Convert hex value to decimal

StringVar HexValue:= &quot;FF&quot;;

NumberVar DecValue:=0; // Hold the result
NumberVar Factor:=1; // Counter for the hex string, used to determine the factor to use

NumberVar i:=0; // Loop counter
NumberVar tmpVal; // Temporary variable

for i := len(HexValue) to 1 step -1
do
(
// Working from right to left, determine the dec value of each hex character
if asc(uppercase(HexValue)) in 65 to 70 then
tmpVal := asc(uppercase(HexValue)) - 55
else
tmpVal := val (HexValue);

// Add the decimal value to the total
DecValue := DecValue + (Factor * tmpVal);

// Keep track of which hex character we are working on
if Factor = 1 then
Factor := 16
else
Factor := factor + 16;
);

// Show the result
DecValue
</code>
Steve Phillips, Crystal Consultant
 
Sorry, I should know not to use i as a loop counter in this forum by now!! Try this:

// Convert hex value to decimal

StringVar HexValue:= &quot;FF&quot;;

NumberVar DecValue:=0; // Hold the result
NumberVar Factor:=1; // Counter for the hex string, used to determine the factor to use

NumberVar j:=0; // Loop counter
NumberVar tmpVal; // Temporary variable

for j := len(HexValue) to 1 step -1
do
(
// Working from right to left, determine the dec value of each hex character
if asc(uppercase(HexValue[j])) in 65 to 70 then
tmpVal := asc(uppercase(HexValue)) - 55
else
tmpVal := val (HexValue[j]);

// Add the decimal value to the total
DecValue := DecValue + (Factor * tmpVal);

// Keep track of which hex character we are working on
if Factor = 1 then
Factor := 16
else
Factor := factor + 16;
);

// Show the result
DecValue
Steve Phillips, Crystal Consultant
 
Oops. You did it again, Steve. [wink]

Steve means:
Code:
// Convert hex value to decimal

StringVar HexValue:= &quot;FF&quot;;

NumberVar DecValue:=0;  // Hold the result
NumberVar Factor:=1;    // Counter for the hex string, used to determine the factor to use

NumberVar j:=0;         // Loop counter
NumberVar tmpVal;       // Temporary variable

for j := len(HexValue) to 1 step -1
do
(
    // Working from right to left, determine the dec value of each hex character
    if asc(uppercase(HexValue[j])) in 65 to 70 then
       tmpVal := asc(uppercase(HexValue[j])) - 55
    else
       tmpVal := val (HexValue[j]);

    // Add the decimal value to the total
    DecValue := DecValue + (Factor * tmpVal);

    // Keep track of which hex character we are working on
    if Factor = 1 then 
        Factor := 16
    else
        Factor := factor + 16;
);

// Show the result
DecValue
Very nice formula, Steve. Star material if ever I saw one. By the by, if you stick your code in [ignore]
Code:
 [/ignore]tags, you never need worry about italic scripts.

Naith
 
Sorry people, I've made a complete hash of this one. Will post amended, and working version very soon!! Steve Phillips, Crystal Consultant
 
Thank Naith, Finished version...

Code:
// Convert hex value to decimal

StringVar HexValue:= &quot;1000&quot;;

NumberVar DecValue:=0;  // Hold the result
NumberVar Factor:=1;    // Counter for the hex string, used to determine the factor to use

NumberVar j:=0;         // Loop counter
NumberVar tmpVal;       // Temporary variable

for j := len(HexValue) to 1 step -1
do
(
    // Working from right to left, determine the dec value of each hex character
    if asc(uppercase(HexValue[j])) in 65 to 70 then
       tmpVal := asc(uppercase(HexValue[j])) - 55
    else
       tmpVal := val (HexValue[j]);

    // Add the decimal value to the total
    DecValue := DecValue + (Factor * tmpVal);

    // Keep track of which hex character we are working on
    Factor := Factor * 16
);

// Show the result
DecValue
Steve Phillips, Crystal Consultant
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top