
how do you convert Enormus numbers to HEX?
I am trying to write a function that will take 2 strings & hash them together. Basically i take the ascii value of each letter of each string & mash them together to form a large number. <BR><BR>for example, WITTMAN would come out to 78657784847387. I tried calling the HEX() function but i keep getting OVERFLOW errors, is there any way around this? or is there an easier way to hash 2 strings together? <BR><BR>

RE: how do you convert Enormus numbers to HEX?
>I am trying to write a function that will take 2 strings & hash >them together. Basically i take the ascii value of each letter >of each string & mash them together to form a large number. <BR>><BR>>for example, WITTMAN would come out to 78657784847387. I tried >calling the HEX() function but i keep getting OVERFLOW errors, >is there any way around this? or is there an easier way to hash >2 strings together? <BR><BR>In other words, you would want code that would convert 78657784847387 to Hex? <BR><BR>Do you realize that 78657784847387 is larger than the largest<BR>possible integer? (Which is 2,147,483,647.) So what VBScript<BR>does is automatically start using floating point numbers.<BR><BR>Do you realize that floating point numbers have an intrinsic<BR>accuracy limitation of about 15 digits? So that number<BR>you are using as your example is about as large as you can get<BR>and still guarantee accuracy. If I used "WITTMANXX", I assume<BR>that your algorithm would get an answer of something like<BR>78187712158153791712. Except that the computer is only capable<BR>of rendering that as approximately 781877121581538E+05 as a<BR>floating point number. (Oops...can't even get the last <BR>displayable digit right. Darn. But right!)<BR><BR>So I *could* show you how to convert 78657784847387 into Hex,<BR>but I strongly suspect that your algorithm is faulty in the<BR>first place. You need to go back and learn the basic limitations<BR>of numeric processing on computers and, for efficiency, derive<BR>an algorithm that never exceeds 31 bits of accuracy (I'd say<BR>32 bits, but VBScript doesn't understand unsigned numbers).<BR>There are many, many such algorithms published in many, many<BR>Computer Science textbooks, and surely there are places on the<BR>Web that display them, as well.<BR><BR>If you have some way of guaranteeing that you will never<BR>exceed 14 digits, then ask again and I'll show you the way.<BR><BR><BR><BR>

RE: how do you convert Enormus numbers to HEX?
When i originally posted this i was trying to develop a 2 way hash i wanted to be able to get back to the original data after it had been converted. after i thought about it some more i realized that a one way hash would work just as well, instead of going back to the original data i could just compare the hashed values (very similar to the way passwords are stored) so i changed the algorithm to reflect this new (well new to me) thinking. now instead of taking appending the ascii value of one letter to another, 2359 would be 2 letters for example, i simply add them & get 82, which keeps the numbers much much smaller & are easily converted to hex that way. also its probably going to be a bit faster in the end as i dont have to worry about derving any original values, just compare hashed ones. Thank you for your help, you were correct I was definitely headed down the wrong path. <BR><BR>Here are the functions I am using to complete the hash,<BR>as a side note Is there any way to get back to your original values once you hash them? I realize that it would be hard, i'm just wondering if its possible?<BR><BR>just a thought?<BR><BR>Function Convert(ByVal Str)<BR><BR> dim iPos <BR> iPos = 1<BR> While iPos <= Len(Str)<BR> tmpVal = ASC(UCase(Mid(Str,iPos,1)))<BR> 'NumVal = cstr(tmpVal) <BR> iPos = iPos + 1<BR> tmp2 = tmpVal + tmp2<BR> Wend<BR> Convert = clng(tmp2) <BR><BR>End Function<BR><BR>Function hash(ByVal FirstWord, ByVal Secondword)<BR><BR> dim tmp1, tmp2, tmp3<BR> tmp1 = convert(FirstWord)<BR> tmp2 = convert(Secondword)<BR> tmp3 = hex(tmp1 * tmp2)<BR> hash = tmp3<BR><BR>End Function <BR><BR><BR>Scott S

Hashing algorithms (was hex conversion)
Well, you are starting to get there, but this still is a very very poor hashing algorithm.<BR><BR>Consider, for example, the hash values of these two strings:<BR> "AC"<BR> "BB"<BR>What do you think that a call to your "convert" function will return for each of those? Try it out if you can't do it in your head. Yep... 132. And 132. <BR><BR>I guess that answers your last question, doesn't it? If you saw 132 as the hash value, how could you know whether the original string was "AC" or "BB" (or "@D" or "!!!!" or "!B!" or any of dozens or more other possibilities).<BR><BR>A *good* hashing algorithm is one that makes it *unlikely* that two strings will hash to the same value. Note that "unlikely" is NOT the same as "impossible". Pretty much by definition there is only one hashing algorithm that guarantees unique values: The output is 100% the same as the input (which is thus a pretty useless algorithm).<BR><BR>Consider: If you only have 31 bits for your hash value, then there are a maximum of roughly 2 billion possible unique hash values. Even if you *ONLY* allowed the 26 upper case letters in the string to be hashed, there are EIGHT (8) billion possible combinations of 7letter strings! So unless you limited the strings to 6 characters, then you *must* have some strings that will get the same hash value. Period.<BR><BR>So I say again: Go find some good reference materials on hashing algorithms. Study them. Find one that suits your needs. Use it. One good place to look is in compiler books, since compilers need to hash your variable names for efficiency. I used to have a xerox of a page from one of those taped to my wall, but then I changed jobs and I dunno where it is. It was a particularly good one. <BR><BR>A *fair* one is the one used by the Java SDK libraries (it's in the method java.lang.String.hashcode if you know enough Java to look it up). Converted to VBScript, it would look like this:<BR><BR>Function hashcode( ByRef str )<BR> Dim hash, cnum<BR> hash = 0<BR> For cnum = 1 To Len(str)<BR> &n bsp; hash = (hash * 31) + Asc(Mid(str,cnum,1))<BR> &n bsp; If hash > 2147483647 Then hash = hash  2147483648<BR> Next<BR>&nbs p; hashcode = CLng(hash)<BR>End Function<BR><BR>The reason it's a fair algorithm is that multiplication by 31.<BR><BR>To elucidate: 32 is 2 to the 5th power, or 5 bits. If you multiplied by 32, you'd be shifting the current value to the left by 5 bits. Okay. But after you've shifted 6 times (that is, added 6 more characters), you've shifted all the information about the first character right off into space. So only the last<BR>6 characters in the string would actually participate in the hash value (oh, all right...to live up to my pedantic billing, it's the last 6 characters plus 1 cruddy bit from the character just prior). Yuck. By using 31 instead of 32, you are shifting the current value to the left by 5 bits but THEN subtracting off the current value. So, essentially, all characters still participate in the final hash value. And why 32? Or, rather, why 5 bits? Because if you are working with ordinary text, all the letters of the alphabet, whether upper or lower case, have unique values in the lower 5 bits. The upper bits do influence the final value, but not so much as the lower bits, which is what you want.<BR><BR>Incidentally, if you know that you *will* be working with only (or almost only) alphabetic charactersand if you want upper and lower case characters to give the same hash valuethen you could change this line:<BR> hash = (hash * 31) + Asc(Mid(str,cnum,1))<BR>to this:<BR> hash = (hash * 31) + (31 AND Asc(Mid(str,cnum,1)))<BR><BR>That would cause "123" to hash to the same as "QRS", though, so only use it if it is applicable.<BR><BR>***********<BR>Bill Wilkinson<BR><BR>p.s.: Forgot to mention that I haven't actually tried the VBS conversion from the Java code. If it doesn't work, let me know!<BR>
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

