Connecting Tech Pros Worldwide Help | Site Map

comparing byte arrays

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 9th, 2006, 08:45 AM
eoghan.kenny@gmail.com
Guest
 
Posts: n/a
Default comparing byte arrays

Hi,

I need to compare two timestamp columns in sql server and see which one
is greater. (i can tell if they are not equal buts not enough for this
requirement).

A timestamp value is unique in a database and always increments. A
non-null timestamp is type Binary(8) in sql server and I pass it to a
byte array in C#. To compare the two byte arrays I use
BitConverter.ToUInt64, as this converts an 8 byte array to a 64 bit
integer. I can then compare the two integers to see which is greater.

Is this method of comparing byte arrays correct ?

Thanks.


  #2  
Old March 9th, 2006, 09:35 AM
Truong Hong Thi
Guest
 
Posts: n/a
Default Re: comparing byte arrays

The SqlBinary class provides many methods/operators (like equality
operator and Equals method) which you can use.
SqlDataReader.GetSqlBinary() return SqlBinary.

  #3  
Old March 9th, 2006, 11:45 AM
ek1
Guest
 
Posts: n/a
Default Re: comparing byte arrays

thats a good tip about the sqlbinary type, i hadn't thought of that.
However it doesn't seem to help me determine which timestamp or byte
array is the greater, only if they are or are not equal.

one other way i have seen this done is to compare the timestamps using
SQL, but I want to to this in my C# program.

  #4  
Old March 9th, 2006, 12:15 PM
Truong Hong Thi
Guest
 
Posts: n/a
Default Re: comparing byte arrays

>However it doesn't seem to help me determine which timestamp or byte[color=blue]
>array is the greater[/color]
It does, in fact:
if (binary1 > binary2) {}
You can even do like this:
if (binary1 >= binary2) {}
Same for ==, <, <=. SqlBinary has overloads all of these operators.
Just see its member in MSDN.

Of course, you can also use binary.CompareTo(binary2)

  #5  
Old March 9th, 2006, 02:05 PM
ek1
Guest
 
Posts: n/a
Default Re: comparing byte arrays

yes you're correct, thanks for your help !

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.