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 a Decimal number to hex number. 1

Status
Not open for further replies.

priyanthan

Programmer
Jul 27, 2008
70
CA
Can someone tell me a way to Convert a Decimal number to hex number.
Working with DB2 and CR 8.5.

Thanks.
 
I believe it is a matter of parsing and evaluating your decimals.

Here are a couple of links with the steps necessary:

Below is a very rough simplified example of how the formula would look...you would need to add logic to check for 0 as the numerator, as that signals the end of the process. Also, logic would need to be added to convert values between 10-15 into hex A,B,C, etc) and to combine the values into a readable format:

numbervar tabledata := 1128;


numbervar step1 := tabledata/16;
numbervar one := (step1-TRUNCATE(step1))*16;


numbervar step2 := TRUNCATE(step1)/16;
numbervar two;
IF TRUNCATE(step2) <= 0 then two := (step2-TRUNCATE(step2))*16;


numbervar step3 := TRUNCATE(step2)/16;
numbervar three;
IF TRUNCATE(step3) <= 0 then three := (step3-TRUNCATE(step3))*16;


stringvar show3;
IF (step3-TRUNCATE(step3))*16 = 10 then show3 := "A"
else
(
IF (step3-TRUNCATE(step3))*16 = 11 then show3 := "B"
else
(
IF (step3-TRUNCATE(step3))*16 = 12 then show3 := "C"
else
(
IF (step3-TRUNCATE(step3))*16 = 13 then show3 := "D"
else
(
IF (step3-TRUNCATE(step3))*16 = 14 then show3 := "E"
else
(
IF (step3-TRUNCATE(step3))*16 = 15 then show3 := "F"
else totext((step3-TRUNCATE(step3))*16)
)))))
 

After all these years I finally ran across a use for the strreverse function!

fisherromacse's post walks you through the logic if you're interested, but this should do the trick:

Code:
whileprintingrecords;
numbervar v_decimal := {yournumericfield);
stringvar v_hex;
stringvar v_hex2;
numbervar v_digit;


v_digit := v_decimal/16;
v_hex := totext((v_digit - floor(v_digit)) * 16,"#",0);
select v_hex
case "10": v_hex := "A"
case "11": v_hex := "B"
case "12": v_hex := "C"
case "13": v_hex := "D"
case "14": v_hex := "E"
case "15": v_hex := "F"
default: v_hex; 

while floor(v_digit) > 0
do
(
v_digit := floor(v_digit)/16;
v_hex2 := totext((v_digit - floor(v_digit)) * 16,"#",0);
select v_hex2
case "10": v_hex2 := "A"
case "11": v_hex2 := "B"
case "12": v_hex2 := "C"
case "13": v_hex2 := "D"
case "14": v_hex2 := "E"
case "15": v_hex2 := "F"
default: v_hex2; 

v_hex := v_hex + v_hex2);

strreverse(v_hex)

 

I realized later than the last variable assignment could be v_hex := v_hex2 + v_hex;


So I'm still looking for a use for strreverse...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top