Page 1 of 1 1
Topic Options
#54542 - 2001-03-15 12:14 AM Converting HEX to DECIMAL?
Anonymous
Unregistered


Does anyone know how to convert the following hexadecimal number (mac address) to a decimal?

$Mac = @Address

I've tried VAL("&$Mac") to no avail. It returns a value of 0.

Looking for a HexToDec macro,
James

Top
#54543 - 2001-03-15 08:27 AM Re: Converting HEX to DECIMAL?
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
Hi James,

I love these sort of convertions, here's the code:

code:

BREAK ON CLS

; Hex2Dex

$HEX=@ADDRESS
$DEC=0

if len($hex)<>(len($hex)/2)*2
? "Not a valid Hexadecimal number"
else
while len($hex)>0
$part=substr($hex,1,2)
$hex=substr($hex,3,len($hex))
$DEC=$DEC+val("&$part")
loop
? $DEC
endif


Now could you explain to us what you want to do with this information?

------------------
Greetz,

Roger the Young
------------------------
The code is out there...
------------------------

_________________________
The Code is out there

Top
#54544 - 2001-03-15 09:04 AM Re: Converting HEX to DECIMAL?
cj Offline
MM club member
*****

Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
Roger, your script returned 631 for a hex input of 0050BA8A3DA6. The real answer is 346727005606.

Kix cannot count above 0x7fffffff (which is 31 bits) or 2 147 483 647, so it cannot convert a 48bit MAC to decimal.

Even if you chop the MAC into two 24 bit pieces, KiX cannot multiply the top 24 bits by 16,777,216:

Top 24 bits:
val("&"+substr(@address, 1, 6))

bottom 24 bits:
val("&"+substr(@address, 7, 6))

decimal value=(top 24 bits*16777216)+bottom 24 bits

I too am curious as to why you want to convert the MAC to decimal...


cj

------------------
For more scripts goto my website and click the hammer and spanner icon.

chrismat@ozemail.com.au

Top
#54545 - 2001-03-15 09:40 AM Re: Converting HEX to DECIMAL?
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
You're right, CJ. My code above just adds every decimal value of each seperate hex-number to a total.

It was an example of how the conversion works. In my case the numbers wouldn't be uniqe any more. (Quess i've should have mentioned that in the first post )

I was waiting for james to tell me what he wanted to do before I elaborated some more.

I have been toying around with making calculations with big numbers as a string using the old arithmetic rules:

$nr1="123456789"
$nr2="234567890"

Take the last number of the second string and multiply the last number of the first string, place the last digit in a answer string and remeber the first digit to add to the calculation of the second calculation.

But... as much as I hate to s(h)ell out to a simple calculator, this was so bulky and slow I didn't have the heart to post it on this board


------------------
Greetz,

Roger the Young
------------------------
The code is out there...
------------------------

_________________________
The Code is out there

Top
#54546 - 2001-03-15 02:25 PM Re: Converting HEX to DECIMAL?
cj Offline
MM club member
*****

Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
This string calculator is a cool idea, what have you done so far?


cj

Top
#54547 - 2001-03-15 02:30 PM Re: Converting HEX to DECIMAL?
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
Well, all my scripts where on my laptop, and recently my laptop was stolen

Backups? nope.

It wasn't much, but I'll try to reconstruct the script from memory and post it a.s.a.p.

------------------
Greetz,

Roger the Young
------------------------
The code is out there...
------------------------

_________________________
The Code is out there

Top
#54548 - 2001-03-15 03:40 PM Re: Converting HEX to DECIMAL?
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Roger,

what calc program were you using?

Bryce

------------------
kix.isorg.net

Top
#54549 - 2001-03-15 06:13 PM Re: Converting HEX to DECIMAL?
Anonymous
Unregistered


Hello Everyone,

Thanks for all the input.

Here's the reason: I need to perform hardware inventory on several hundred desktop PCs. Our chosen program, TrackIt, requires the MAC address to be stored within AUTOEXEC.BAT in decimal format. Very unorthodox but a requirement which I have to meet nevertheless (we confirmed this with a call to tech support).

In the words of the famous pastry princess, "Help me, Obi Wan Kenobi, you're my only hope!"

I eagerly await a solution.

Top
#54550 - 2001-03-15 10:32 PM Re: Converting HEX to DECIMAL?
Anonymous
Unregistered


UPDATED REQUIREMENT:

I can work with simply taking the last three digits of the MAC address and conveting that into a binary number.

I'm currently struggling with the following:

$Z=VAL("&"+substr(@address, 10, 3))

The value always yields a 0. I've tried a variety of start and stop numbers within the SUBSTR function, to no avail.

Any ideas?

Top
#54551 - 2001-03-17 12:23 AM Re: Converting HEX to DECIMAL?
Anonymous
Unregistered


James,
keep an eye on your email. A solution is on the way. This is one of those problems which is easier to solve with C (or other language).

To everyone,
There seems to be a reasonable number of times when it would be convenient to have some place where we could make files available to each other. Anyone have any suggestions? My own web sites are not suitable for this kind of thing but perhaps someone does have somehwere we could use as a depository of useful stuff to go with kixtart.

Top
#54552 - 2001-03-17 12:42 AM Re: Converting HEX to DECIMAL?
Anonymous
Unregistered


Hmmm... seems the filename I selected was a mistake. some Antivirus software somewhere along the mail route rejected it because of the name. At least it sent me a report instead of just trashing it quietly. I'll try again with a different name.
Top
#54553 - 2001-03-16 02:14 PM Re: Converting HEX to DECIMAL?
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
Ok, you need two files:

First 'calc.kix'

code:

BREAK ON CLS

if len($val1)<len($val2)
$str1=$val1 $str2=$val2
else
$str2=$val1 $str1=$val2
endif

$ans="" $carry=0
gosub fill
gosub $do

exit

:multiply
$ans="" $subans=""
$fixpos=$pos
while substr($str1,$pos,1)<>" " and $pos>0
$zeros=0 while $pos+$zeros<len($str1) $subans="0"+$subans $zeros=$zeros+1 loop
$pos2=$fixpos
while substr($str2,$pos2,1)<>" " and $pos2>0 or $carry>0
if $pos2>0 $a=val(substr($str1,$pos,1))*(substr($str2,$pos2,1)) endif
$a=$a+$carry
if $a>9 $carry=val(substr("$a",1,1)) $a=val(substr("$a",2,1))
else $carry=0
endif
$subans="$a"+$subans $a=0
$pos2=$pos2-1
loop
$subans="|"+$subans
$pos=$pos-1
loop

$str1save=$str1 $str2save=$str2
$str1="" $str2=""
while len($subans)>0
$str1=$ans $ans=""
$subans=substr($subans,2,len($subans))
if instr($subans,"|")
$str2=substr($subans,1,instr($subans,"|")-1)
$subans=substr($subans,instr($subans,"|"),len($subans))
else
$str2=$subans
$subans=""
endif
gosub fill
gosub add
loop

$str1=$str1save $str2=$str2save
$kixans=val($str1)*val($str2)
return

:add
$carry=0
while $pos>0 or $carry>0
if $pos>0 $a=val(substr($str1,$pos,1))+val(substr($str2,$pos,1)) endif
$a=$a+$carry
if $a>9 $carry=1 $a=$a-10
else $carry=0
endif
$ans="$a"+$ans $pos=$pos-1 $a=0
loop
$kixans=val($str1)+val($str2)
return

:fill
if len($str1)>len($str2)
while len($str2)<len($str1) $str2=" "+$str2 loop
else
while len($str1)<len($str2) $str1=" "+$str1 loop
endif
$pos=len($str1)
return


Second, 'main.kix'

code:

BREAK ON CLS

$top=val("&"+substr(@ADDRESS, 1, 6))

$bottom=val("&"+substr(@ADDRESS, 7, 6)) $bottom="$bottom"

$val1="$top" $val2="16777216"
$do="multiply"
call "calc.kix"

$val1=$ans $val2="$bottom"
$do="add"
call "calc.kix"

? $ans


Thanx to CJ's explanation about the top 'n bottom part of the hex-nr.

Hope this helps.

------------------
Greetz,

Roger the Young
------------------------
The code is out there...
------------------------

_________________________
The Code is out there

Top
#54554 - 2001-03-16 04:29 PM Re: Converting HEX to DECIMAL?
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
You can use my ftp site to hold files if yall want.

ftp.isorg.net

use the /pub folder

Bryce

Top
#54555 - 2001-03-16 05:03 PM Re: Converting HEX to DECIMAL?
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
Hi Bryce,

Congrats on your 1K+ post. That FTP-server is really nice. I've posted one of my projects that I've never really finished, maybe someone out there would like to add the finishing touches.

I had many more of those projects, but my laptop was stolen, so I'm afraid they're lost .

I was working on a two-player version of Hangman and I was gonna try to use an FTP-site too make it possible to play over the Net, If I'll ever restart on that project, would you mind if I misuse your server for that purpose? (just for testing ofcourse)

------------------
Greetz,

Roger the Young
------------------------
The code is out there...
------------------------

[This message has been edited by DrillSergeant (edited 16 March 2001).]

_________________________
The Code is out there

Top
#54556 - 2001-03-16 10:00 PM Re: Converting HEX to DECIMAL?
Anonymous
Unregistered


THANKS FUSE!! IT WORKED!

I took the program you e-mailed me (our anti-virus software running on our mail server moved it in quarantine)and it worked like a charm!

Here's how I used it:

SHELL "%COMSPEC% /C @LServer\netlogon\macaddress.exe @Address"
OPEN (1,"c:\MacAddre.txt",2)
WRITEPROFILESTRING ("C:\AUTOEXEC.BAT", "TrackIt Mac Address in Decimal Format", "TIA", READLINE(1))

Thanks again, FUSE! (I'm sorry our mail server gave you such a hard time -- I concur -- we need a centralized location for these files).

And thanks to everyone who responded to this thread!

Top
#54557 - 2001-03-17 12:20 AM Re: Converting HEX to DECIMAL?
Anonymous
Unregistered


Excellent code DrillSergeant, it is from the very best scripts ever posted .

Top
#54558 - 2001-03-18 08:33 AM Re: Converting HEX to DECIMAL?
cj Offline
MM club member
*****

Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
Roger, you beat me to it:

I wrote half of this on Fri and the rest on Sun:

BigHex2Dex.KiX

code:

break on
cls
dim $c[100]
dim $ans


;
; Big Hex2Dec Converter
;
; Version 1.1
;
; by cj
;


$hex=@address
gosub HEXCALC


"0x" $hex " = " $ans ?


;---------------------------------------------------------------------police line, do not cross----------
quit
:HEXCALC
;
; Inputs:
; $hex = string that contains a hexadecimal number
;
; Outputs:
; $ans = string that contains the decimal version of $hex
;
if len($hex)/2<len($hex)-len($hex)/2
$hex="0"+$hex ; add leading 0 if required
endif
if len($hex)<9 ; KiX can do this calc without help
$ans=val("&"+$hex)
"KiX can do this without any help from me..." ? return
endif
if len($hex)>12 ; too big for this script at this time...but maybe later
"$$hex too long, max length=12 at the moment..." ?
quit quit
endif
$num=val("&"+substr($hex, 1, len($hex)-6)) $num1="$num" $num2="16777216"
gosub multiply
$num1=$ans $num=val("&"+substr($hex, len($hex)-5, 6)) $num2="$num"
gosub add
return


:MULTIPLY
;
; Inputs:
; $num1, $num2 = strings that contain numbers
;
; Outputs:
; $ans = string that contains $num1 multiplied by $num2
;
$ans="" $s=0 $b=0
do
$n=$s $a=0
do
;; val(substr($num2, len($num2)-$b, 1)) " * " val(substr($num1, len($num1)-$a, 1)) " : " $n ?
$c[$n]=$c[$n]+val(substr($num2, len($num2)-$b, 1)) * val(substr($num1, len($num1)-$a, 1))
$a=$a+1
$n=$n+1
until $a=len($num1)
$s=$s+1 $b=$b+1
until $b=len($num2)
$s=$n ; stopping point for next loop
;; $n=0 do "$$c[" $n "] = " $c[$n] ? $n=$n+1 until $n=$s
$n=0
do
if $c[$n]>9
$h=$c[$n] $h="$h"
;; $h ?
$c[$n+1]=$c[$n+1]+val(substr($h, 1, len($h)-1))
$c[$n]=val(substr($h, len($h), 1))
endif
$n=$n+1
until $n=$s
$n=$s-1 do $ans=$ans+$c[$n] $c[$n]=0 $n=$n-1 until $n=-1
return


:ADD
;
; Inputs:
; $num1, $num2 = strings that contain numbers
;
; Outputs:
; $ans = string that contains $num1 added to $num2
;
while len($num1)>len($num2) $num2="0" + $num2 loop ; add leading zeros to smallest number
while len($num2)>len($num1) $num1="0" + $num1 loop
$ans=""
$s=len($num1)
$n=0 do $c[$n]=val(substr($num2, len($num2)-$n, 1)) + val(substr($num1, len($num1)-$n, 1)) $n=$n+1 until $n=$s
$n=0 do if $c[$n]>9 $h=$c[$n] $h="$h" $c[$n+1]=$c[$n+1]+val(substr($h, 1, len($h)-1)) $c[$n]=val(substr($h, len($h), 1)) endif $n=$n+1 until $n=$s
$n=$s-1 do $ans=$ans+$c[$n] $c[$n]=0 $n=$n-1 until $n=-1
return


Note that the :ADD and :MULTIPLY subs can handle very large numbers.

I can post more info on them , but a quick glance at Rogers code shows that it's prob the same.

cj

------------------
For more scripts goto my website and click the hammer and spanner icon.

chrismat@ozemail.com.au

[This message has been edited by cj (edited 19 March 2001).]

Top
#54559 - 2001-10-18 07:02 AM Re: Converting HEX to DECIMAL?
MCA Offline
KiX Supporter
*****

Registered: 2000-04-28
Posts: 5152
Loc: Netherlands, EU
Dear,

Another way see topic of kholm: UDF Hex2DEC and calculations of large integers
http://kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=3&t=000204
Greetings.

_________________________
email scripting@wanadoo.nl homepage scripting@wanadoo.nl | Links | Summary of Site Site KiXforms FAQ kixtart.org library collection mirror MCA | FAQ & UDF help file UDF kixtart.org library collection mirror MCA | mirror USA | mirror europe UDF scriptlogic library collection UDFs | mirror MCA

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1821 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.12 seconds in which 0.077 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org