Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Compare short strings as numbers Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Compare 8 bytes in two swoops 08-Dec-04
Category
Others
Language
Delphi All Versions
Views
368
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
Nemitz, Vernon
Reference URL:
			1   
2   // Requires Int64 type.  Earliest versions of Delphi may lack it.
3   
4   type
5     a8ch = array[1..8] of char;
6     PA8C = ^a8ch;  // pointer type, to above array of characters
7     PI64 = ^Int64;  // pointer type, for Int64 numbers
8   
9   
10  // converts short strings (8 chars max) to a number holding ASCII bytes
11  function StrToAsc(s: string): Int64;
12  begin
13    SetLength(s, 8); // increase or decrease to length of 8
14    Result := PI64(Pointer(PA8C(@(s[1]))))^;
15  end;
16  // Intermediate use of generic Pointer yields no compiler warnings
17  // The compiler translates the characters in the string
18  // first into the array-of-chars type, and then into the numeric type.
19  // The 8 character-bytes is essentially an array of ASCII codes, and
20  // since they ARE a group of adjacent memory cells holding numbers, 
21  // they can be directly interpreted as the 8 bytes of an Int64 in one
22  // swoop.  So, no time-wasting byte-at-a-time copying loop is needed!
23  
24  
25  
26  // WITH THE PRECEDING PREPARATION, HERE IS THE COMPARER FUNCTION:
27  // Result: positive: s1 is larger
28  //         negative: s2 is larger
29  //         zero: first 8 characters of strings are the same
30  function CmpShortS(s1, s2: string): Int64;
31  begin
32    Result := StrToAsc(s1) - StrToAsc(s2);
33  end;
34  // The two swoops are the two lower-level function calls :)
35  // Yes, the Result is a C-language kind of value.  Useful... 
36  
37  
38  
39  //(included for sake of completeness)
40  // interprets a 64-bit number as a sequence of characters
41  function AscToStr(a: Int64): string;
42  begin
43    SetLength(Result, 8);
44    Result := string(PA8C(Pointer(@a))^);
45  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC