Posted: 26th Jun 2007 14:50
Whats wrong with val(dword) it's producing the wrong answer?
Is it me or a bug in DBP?

Can other people test this code please an tell me if you get the same result.

REM Project: ValTest
REM Created: 26/06/2007 12:41:35
REM
REM ***** Main Source File *****
REM

shortVal as dword
longVal as dword

shortVal = val ("12345678")
longVal = val("1234567890")

print str$(shortVal) `result = 12345678
print str$(longVal) `result = 1234567936!!!
print str$(1234567890) `result = 1234567890

wait key

OMG i realy need accurate results for my encryption/decryption DLL to work
Posted: 26th Jun 2007 15:03
Yes ... no ... maybe.

The VAL command takes a string and returns a float - it just so happens that the second number is not representable in a float. That's why I included an INTVAL command in one of my utility plug-ins.
Posted: 26th Jun 2007 15:10
The VAL command takes a string and returns a float

The manual says it returns an int but I guess that maybe it's using a float in the background which would explain the results.

Location of untility plugin plx? it could turn out to be a life saver!

I had a search the the readme files for the DLLs on your website and I couldn't find one with the command INTVAL

I'm toying witht he idea of doing this..


valString$ = "1234567890"

l$ = left$(valString$,5)
r$ = right$(valString$,5)

l = val(l$)
r = val(r$)

lr = l * 100000 + r

print str$(lr)
wait key
end


as I know my dword values will always be 10 digits long
Posted: 26th Jun 2007 15:27
Location of untility plugin plx? it could turn out to be a life saver!

Just take a look at his signature
And btw, Ian, your plugins are simply amazing!
Posted: 26th Jun 2007 15:57
Thanks for the vote

@GatorHex,
I've just confirmed it from the resource string for VAL - it returns a float. The help is incorrect for this function.

[EDIT]Changing my sig to bold-text may help a little
Posted: 26th Jun 2007 15:59
Bhax & IanM, I said in my post (above) already "i read the plugin readme files and couldn't find which pluggin had the INTVAL command".

I can see my text, but maybe the forum is not showing my editted post? Wierd things happen on this forum that's for sure, like it says somone has replied but you cannot see their post

I suppose i could download them all and hope for the best
Posted: 26th Jun 2007 16:10
Oh, it's in plug-in number 7.
Posted: 26th Jun 2007 16:50
Hehe..
If you think thats weird, try this example I knocked up to help fix your problem....
+ Code Snippet
short as dword
long as dword

short = myval("12345678")
long  = myval("1234567890")

Print Str$(Short)
Print Str$(Long)

Wait key
End

Function MyVal(A$)
   diRes as Double Integer
   diMul as Double Integer
   If Len(A$)>0
      diMul = 1
      diRes = 0
      For C=Len(A$) to 1  step -1
         diRes = diRes +( Val(Mid$(A$,C)) * diMul)
         diMul = diMul * 10
      Next
      ExitFunction diRes
   EndIf
EndFunction diRes

Posted: 26th Jun 2007 17:03
I was wrong my Vals don't stay 10 digit they sometimes go 9 digit.

But I think Hex stays 8 chars all the time.

So my next question is

hex$(dword) `works

but how do i do the reverse, and turn a hex string back into a dword value?

val(hex$) 'don't it returns zero

[UPDATE]
Just found this gona try it and see if it works
+ Code Snippet
function Hex2Dec(s$)
   for p = 0 to len(s$) - 1
      n = asc(upper$(mid$(s$, len(s$) - p))) - 48
      d = d + (n - (n > 9) * 7) * 16 ^ p
   next p
endfunction d


hmm, seems to be working after changing everything to dword size. I might store my encryption in hex then

Just worried about big/little endian issues. I'm right in think all windows PCs are the same endian?
Posted: 26th Jun 2007 20:26
Pretty much, yes - you only need to worry about endian issues if your data is to be decoded on another architecture.

[EDIT]WHile I think about it, you can guarantee that your numbers are 10 characters in size by using the PADLEFT$ function from my string utility plug-in (no 16):
+ Code Snippet
valString$ = "123456"

valString$ = padleft$( valString$, "0", 10 )  ` <-- Added line here
l$ = left$(valString$,5)
r$ = right$(valString$,5)

l = val(l$)
r = val(r$)

lr = l * 100000 + r

print str$(lr)
wait key
end
Posted: 28th Jun 2007 18:19
@IanM
That Padleft$ function looks similar to the String$ function used in other Basic languages.
+ Code Snippet
ValString$=String$(10-Len(ValString$),"0")+ValString$

it's a shame DB hasn't got a command like this built in...
Posted: 28th Jun 2007 19:53
Is it needed though? It's easy to duplicate its effects using padleft$.

It's trivial to implement, so if you can come up with a genuinely useful reason for me to include it in the next release of the utility plug-ins (this weekend!) then I'll add it.