george7a
8th December 2008, 17:13
Hi,
I wrote some conversion functions and I thought I will share them.
Dec.to.Hex takes a decimal number and returns hexadecimal in a string
function domain tcmcs.str300m Dec.to.Hex(long dec.num)
{
| Written By: George Abdo
domain tcmcs.str300m res.hex
if dec.num = 0 then return("0") endif
res.hex = ""
while dec.num <> 0
res.hex = Dec.to.Hex.digit(dec.num\16) & res.hex | mod
dec.num = dec.num /16 | div
endwhile
return (res.hex)
}
| This function is used only to convert one digit.
function domain tcmcs.str1 Dec.to.Hex.digit(long dec.num)
{
| Written By: George Abdo
string res.hex(1)
on case dec.num
case 10: res.hex = "A"
break
case 11: res.hex = "B"
break
case 12: res.hex = "C"
break
case 13: res.hex = "D"
break
case 14: res.hex = "E"
break
case 15: res.hex = "F"
break
default: res.hex = str$(dec.num)
break
endcase
return(res.hex)
}
george7a
8th December 2008, 17:15
I already posted before (http://baanboard.com/baanboard/showthread.php?p=105560) Hex.to.Bin. It takes a hexadecimal and returns a binary.
function string Hex.to.Bin(string in.hex(32))
{
| Written By: George Abdo
long i
string tmp.bin(4),out.bin(128)
for i=1 to len(in.hex)
on case in.hex(i;1)
case "0": tmp.bin = "0000"
break
case "1": tmp.bin = "0001"
break
case "2": tmp.bin = "0010"
break
case "3": tmp.bin = "0011"
break
case "4": tmp.bin = "0100"
break
case "5": tmp.bin = "0101"
break
case "6": tmp.bin = "0110"
break
case "7": tmp.bin = "0111"
break
case "8": tmp.bin = "1000"
break
case "9": tmp.bin = "1001"
break
case "A": tmp.bin = "1010"
break
case "B": tmp.bin = "1011"
break
case "C": tmp.bin = "1100"
break
case "D": tmp.bin = "1101"
break
case "E": tmp.bin = "1110"
break
case "F": tmp.bin = "1111"
break
default:
return ("Error")
endcase
out.bin=out.bin&tmp.bin
endfor
return(out.bin)
}
george7a
8th December 2008, 17:16
Another small function Dec.to.Bin. It takes a decimal number and returns a binary.
function domain tcmcs.str300m Dec.to.Bin(long dec.num)
{
| Written By: George Abdo
long res.div,res.mod
domain tcmcs.str300m res.bin
if dec.num = 0 then return("0") endif
res.bin = ""
while dec.num <> 0
res.bin = str$(dec.num \ 2) & res.bin | mod
dec.num = dec.num /2 | div
endwhile
return (res.bin)
}
Hitesh Shah
24th January 2009, 15:05
For converting decimal numbers to hex and octal numbers , sprintf$ with %x and %o argument can give the desired result . Probably in-built function may be faster .
For binary conversions , the functions written looks very much necessary .
joepte
18th July 2011, 17:37
Hi, I am working on a hex to dec coversion function but have run into a problem - has anyone got a working one?
Regards
~Vamsi
19th July 2011, 00:58
Hi, I am working on a hex to dec coversion function but have run into a problem - has anyone got a working one?
Regards
Please post your code to help find resolution.
george7a
19th July 2011, 11:03
Hi, I am working on a hex to dec coversion function but have run into a problem - has anyone got a working one?
Regards
Here you go:
function long Hex.to.Dec(string in.hex(32))
{
| Written By: George Abdo
long i,tmp.d,out.num
tmp.d = 0
out.num = 0
for i=1 to len(in.hex)
on case in.hex(i;1)
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9": tmp.d = lval(in.hex(i;1))
break
case "A": tmp.d = 10
break
case "B": tmp.d = 11
break
case "C": tmp.d = 12
break
case "D": tmp.d = 13
break
case "E": tmp.d = 14
break
case "F": tmp.d = 15
break
default:
return (-1) | Error
endcase
out.num=out.num + tmp.d*pow(16,len(in.hex) - i)
endfor
return(out.num)
}
joepte
18th August 2011, 06:41
Thanks George - that has helped greatly.
I have stumbled upon another related problem though:
When I try converting back using the sprintf command it seems to miss part of the hex number.
For example if the numer given in new.doub is
827569822769 representing C0AF015C31; after the sprintf the sprint.str is just AF015C31 a very different smaller number.
I thought it might be domain issues why it is skipping the "C0" but the string is 20 chars. should it be a diff domain. i have looked at a couple sprintf flags without success. Do you see anything here that might cause the chopping off of the first 2 chars?
Kind regards.
here is part of this code:
domain tcqiv1 new.doub
domain tcmcs.str20 sprint.str
sprint.str = sprintf$("%x", new.doub)
günther
18th August 2011, 10:46
I've checked your conversion, and on my system I get "7fffffff" which is -2147483647 or -MAXINT. Normally I would use %x only for integers, not for doubles. What would you expect for e.g. 0.5 in hex?
Günther
joepte
18th August 2011, 17:04
true - it does look strange on 0.5 but the numbers I tried to use were too large for ints or longs and contained no numbers after the decimal place. What I can do because the first part of the hex is constant I can take the eight chars returned by sprintf and add the constant to it.
Thanks for all the help