Hash Code Help
I trying to port this pascal code to Spin
This doesn't work..
function ELFHash(const Str : String) : Int64;
var
i : integer;
x : Cardinal;
begin
Result := 0;
for i := 1 to Length(Str) do
begin
Result := (Result shl 4) + Ord(Str[noparse][[/noparse]i]);
x := Result and $F0000000;
if (x <> 0) then
begin
Result := Result xor (x shr 24);
end;
Result := Result and (not x);
end;
end;
This doesn't work..
''*************************************** ''* From Keyboard Demo v1.0 * ''* Author: Chip Gracey * ''* Copyright (c) 2006 Parallax, Inc. * ''* See end of file for terms of use. * ''***************************************
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
VAR byte j long x OBJ
term : "tv_terminal"
kb : "keyboard"
PUB start | i
'start the tv terminal
term.start(12)
term.str(string("HashCode Demo...",13))
'start the keyboard
kb.start(26, 27)
term.Hex(Hash(String("DIR")),8)
term.out(13)
term.Hex(Hash(String("TYPE")),8)
term.out(13)
term.Hex(Hash(String("MOUNT")),8)
term.out(13)
term.Hex(Hash(String("UNMOUNT")),8)
term.out(13)
PUB Hash(string_ptr) : Result
result := 0
repeat strsize(string_ptr)
Result := (Result << 4) + byte[noparse][[/noparse]string_ptr++]
x := Result and $F0_00_00_00
if (x <> 0)
Result := Result ^ (x >> 24)
Result := result & (not x)
{{
Here is a windows program
zip
202K

Comments
into that (bitwise AND)
any idea why it breaks with more than 6 characters?
''*************************************** ''* From Keyboard Demo v1.0 * ''* Author: Chip Gracey * ''* Copyright (c) 2006 Parallax, Inc. * ''* See end of file for terms of use. * ''*************************************** CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 VAR long x byte HashStr[noparse][[/noparse]8] OBJ term : "tv_terminal" kb : "keyboard" PUB start | i 'start the tv terminal term.start(12) term.str(string("HashCode Demo...",13)) 'start the keyboard kb.start(26, 27) repeat term.str(string("Enter a word >")) getstr term.str(string("Hash Code for ")) term.str(@HashStr) term.str(string(" is $")) term.Hex(Hash(@HashStr),8) term.out(13) PUB GetStr | i,ch i := 0 repeat until ch == 13 ch := kb.getkey HashStr[noparse][[/noparse]i++] := ch term.out(ch) HashStr[noparse][[/noparse]i-1] := 0 PUB Hash(string_ptr) : Result result := 0 repeat strsize(string_ptr) Result := (Result << 4) + byte[noparse][[/noparse]string_ptr++] x := Result & $F0_00_00_00 if (x <> 0) Result := Result ^ (x >> 24) Result := result & (not x) {{ ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ TERMS OF USE: MIT License │ ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │ │files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │ │modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│ │is furnished to do so, subject to the following conditions: │ │ │ │The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│ │ │ │THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE │ │WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR │ │COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ │ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ }}Post Edited (Bill Drummond) : 4/3/2009 10:08:26 AM GMT
Post Edited (kuroneko) : 4/3/2009 10:17:26 AM GMT
I edited the Windows program so everybody uses hex.
And I'm also not too comfortable with repeat strsize(). Is this evaluated only at the beginning of the loop or everytime the loop is run? If you put some debug tracing in there, how far does it get?
same result. That's true, but I changed it to Cardinal(unsigned 32 bit)·and it still works.
I need to Learn how ViewPort works... got too many projects going at same time.
Change...
...which is a boolean 'NOT' to...
...which is a bitwise 'NOT'.
If I key in : 1234567·· · I see $00000000 should be $045678A7
If I key in : 123456789 I see $000003B9 should be $0678AEE9
If I key in :·MOUNT····· I see $00524A34··- is good
If I key in : UNMOUNT· I see $00000000 should be $0A324A64
Download the File in my first post to run the Hash Code generator on a PC.
··