Baanboard.com

Go Back   Baanboard.com > Forum > Baan SIGs > Code & Utilities

User login

Frontpage Sponsor

Main

Poll
For ERP LN feature pack upgrade, what method of install are you using?
Installation Wizard into existing VRC
35%
Installation Wizard into new VRC
42%
Manual into existing VRC
3%
Manual into new VRC
19%
Total votes: 31

Baanboard at LinkedIn


Reference Content

Reply
 
Thread Tools Display Modes
  #1  
Old 8th December 2008, 16:13
george7a's Avatar
george7a george7a is offline
Guru
 
Join Date: May 2004
Location: Nazareth
Posts: 1,490
george7a is on a distinguished road
Baan: IVc, 5.0 b, 5.0 c, LN 6.1 - DB: MS SQL, Oracle - OS: Windows 2000, 2003 & UNIX
Hexadecimal, Decimal & Binary Conversion Functions

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
Code:
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)
}
__________________
_
George Abdo
NAZDAQ - Your Documents, Your Way.
Reply With Quote
  #2  
Old 8th December 2008, 16:15
george7a's Avatar
george7a george7a is offline
Guru
 
Join Date: May 2004
Location: Nazareth
Posts: 1,490
george7a is on a distinguished road
Baan: IVc, 5.0 b, 5.0 c, LN 6.1 - DB: MS SQL, Oracle - OS: Windows 2000, 2003 & UNIX
I already posted before Hex.to.Bin. It takes a hexadecimal and returns a binary.
Code:
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)

}
__________________
_
George Abdo
NAZDAQ - Your Documents, Your Way.
Reply With Quote
  #3  
Old 8th December 2008, 16:16
george7a's Avatar
george7a george7a is offline
Guru
 
Join Date: May 2004
Location: Nazareth
Posts: 1,490
george7a is on a distinguished road
Baan: IVc, 5.0 b, 5.0 c, LN 6.1 - DB: MS SQL, Oracle - OS: Windows 2000, 2003 & UNIX
Another small function Dec.to.Bin. It takes a decimal number and returns a binary.

Code:
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)
}
__________________
_
George Abdo
NAZDAQ - Your Documents, Your Way.
Reply With Quote
  #4  
Old 24th January 2009, 14:05
Hitesh Shah's Avatar
Hitesh Shah Hitesh Shah is offline
Guru
 
Join Date: Nov 2001
Location: Mumbai,India
Posts: 1,855
Hitesh Shah is on a distinguished road
Baan: triton,Baan IVc4 , ERP Ln - DB: Oracle/Bisam/SQL 2000/SQL 2005 - OS: Sun Solaris/Windows 2003
dec2hex and dec2oct

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 .
Reply With Quote
  #5  
Old 18th July 2011, 16:37
joepte joepte is offline
Member
 
Join Date: Jan 2006
Posts: 60
joepte is on a distinguished road
Baan: 4cIV - DB: Oracle 10g - OS: UNIX
Hi, I am working on a hex to dec coversion function but have run into a problem - has anyone got a working one?
Regards
Reply With Quote
  #6  
Old 18th July 2011, 23:58
~Vamsi's Avatar
~Vamsi ~Vamsi is offline
Guru
 
Join Date: Aug 2001
Location: San Diego CA, USA
Posts: 590
~Vamsi will become famous soon enough
Baan: ~*~ - DB: ~*~ - OS: ~*~
Quote:
Originally Posted by joepte View Post
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.
__________________
~Vamsi
Vamsi Potluru
Baan XL Yet another tool to format Baan output in Excel from Baan Board
This one just happens to be free :)

Play the Google game and help Baanboard get better rankings. Do your part. Click here to find how.
Reply With Quote
  #7  
Old 19th July 2011, 10:03
george7a's Avatar
george7a george7a is offline
Guru
 
Join Date: May 2004
Location: Nazareth
Posts: 1,490
george7a is on a distinguished road
Baan: IVc, 5.0 b, 5.0 c, LN 6.1 - DB: MS SQL, Oracle - OS: Windows 2000, 2003 & UNIX
Thumbs up Hex to Dec

Quote:
Originally Posted by joepte View Post
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:

Code:
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)

}
__________________
_
George Abdo
NAZDAQ - Your Documents, Your Way.
Reply With Quote
  #8  
Old 18th August 2011, 05:41
joepte joepte is offline
Member
 
Join Date: Jan 2006
Posts: 60
joepte is on a distinguished road
Baan: 4cIV - DB: Oracle 10g - OS: UNIX
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)
Reply With Quote
Sponsored Links
  #9  
Old 18th August 2011, 09:46
günther günther is offline
Guru
 
Join Date: Jan 2002
Location: Ehingen, Germany
Posts: 570
günther is on a distinguished road
Baan: IVc4 - DB: Informix - OS: HP-UX
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
Reply With Quote
  #10  
Old 18th August 2011, 16:04
joepte joepte is offline
Member
 
Join Date: Jan 2006
Posts: 60
joepte is on a distinguished road
Baan: 4cIV - DB: Oracle 10g - OS: UNIX
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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hexadecimal to Binary manojsharma Tools Development 3 20th December 2006 20:51
EDI - Item Code Conversion In shaboo Distribution, Transportation & Warehousing 2 12th April 2006 14:16
Cost Price in PO with 3 decimal places YOGESH GUPTA Distribution, Transportation & Warehousing 2 8th October 2004 12:41
Urgent help req -Customized Item Conversion factor noorulhaq Manufacturing & Supply Chain 0 7th June 2003 07:32


All times are GMT +2. The time now is 05:48.


©2001-2017 - Baanboard.com - Baanforums.com