473,765 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hashtable Contains() - byte arrays as keys -

Hi All,

I am having problem when i am using hashtable to keep an array of bytes
value as keys.

Take a look at the code snippet below

---------------------------------------------------

ASCIIEncoding asciiEncoder = new ASCIIEncoding() ;
byte[] bArray = asciiEncoder.Ge tBytes("Test");

Hashtable ht = new Hashtable();
ht.Add(bArray," Some value");

byte[] bArrayNew = asciiEncoder.Ge tBytes("Test");

Console.WriteLi ne("Result : "+ ht.Contains(bAr ray)); //true
Console.WriteLi ne("Result : "+ ht.Contains(bAr rayNew)); //false
Console.Read();

--------------------------------------------------

As seen here, by using the same object , the contains() will return true,
while using a new object with same 'value' returns false. If I am correct
the contains() command does not look at the values in an object. As for
primitive types like normal string, int and etc, it does not have any
problem.

So I was wondering if there is anyway i can keep byte arrays as keys in
hashtable and still find it in the time complexity of O(1). If i read all
the keys out and make a byte comparison it would be O(n) time complexity,
thus defeating the purpose of a hash table.

Is there any other command in hashtable or some functions i can overwrite?
Would prefer a much simpler way if possible.

Thanks in advance

Joseph Lee
Nov 16 '05 #1
7 6887
You are correct; any array or collection object would test for the object
reference, not the contents. A match of an arbitrary number of items in a
collection would be cost-prohibitive.

Why do you need to match a byte array? What are you trying to accomplish?
Maybe there's a different approach.

--Bob

"Joseph Lee" <jo*********@ho tmail.com> wrote in message
news:ed******** *****@TK2MSFTNG P12.phx.gbl...
Hi All,

I am having problem when i am using hashtable to keep an array of bytes
value as keys.

Nov 16 '05 #2
As Bob said, it's the reference that is being tested and not the contents. If
I remember right, searching in the lines of overriding Equals and GetHashCode
might help. Sorry, I can't recollect them now...
"Joseph Lee" wrote:
Hi All,

I am having problem when i am using hashtable to keep an array of bytes
value as keys.

Take a look at the code snippet below

---------------------------------------------------

ASCIIEncoding asciiEncoder = new ASCIIEncoding() ;
byte[] bArray = asciiEncoder.Ge tBytes("Test");

Hashtable ht = new Hashtable();
ht.Add(bArray," Some value");

byte[] bArrayNew = asciiEncoder.Ge tBytes("Test");

Console.WriteLi ne("Result : "+ ht.Contains(bAr ray)); //true
Console.WriteLi ne("Result : "+ ht.Contains(bAr rayNew)); //false
Console.Read();

--------------------------------------------------

As seen here, by using the same object , the contains() will return true,
while using a new object with same 'value' returns false. If I am correct
the contains() command does not look at the values in an object. As for
primitive types like normal string, int and etc, it does not have any
problem.

So I was wondering if there is anyway i can keep byte arrays as keys in
hashtable and still find it in the time complexity of O(1). If i read all
the keys out and make a byte comparison it would be O(n) time complexity,
thus defeating the purpose of a hash table.

Is there any other command in hashtable or some functions i can overwrite?
Would prefer a much simpler way if possible.

Thanks in advance

Joseph Lee

Nov 16 '05 #3
I have a list of encrypted keywords and all of them are in an array of byte.
So I am trying to keep those keyword as keys in a hashtable for fast lookup,
then i will be able retrieve some information which is in the value part of
the hashtable

Joey

"Bob Grommes" <bo*@bobgrommes .com> wrote in message
news:#V******** ******@tk2msftn gp13.phx.gbl...
You are correct; any array or collection object would test for the object
reference, not the contents. A match of an arbitrary number of items in a
collection would be cost-prohibitive.

Why do you need to match a byte array? What are you trying to accomplish?
Maybe there's a different approach.

--Bob

"Joseph Lee" <jo*********@ho tmail.com> wrote in message
news:ed******** *****@TK2MSFTNG P12.phx.gbl...
Hi All,

I am having problem when i am using hashtable to keep an array of bytes
value as keys.


Nov 16 '05 #4
ic, i will look into it. Thanks

Joey

"Rakesh Rajan" <Ra*********@di scussions.micro soft.com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
As Bob said, it's the reference that is being tested and not the contents. If I remember right, searching in the lines of overriding Equals and GetHashCode might help. Sorry, I can't recollect them now...
"Joseph Lee" wrote:
Hi All,

I am having problem when i am using hashtable to keep an array of bytes
value as keys.

Take a look at the code snippet below

---------------------------------------------------

ASCIIEncoding asciiEncoder = new ASCIIEncoding() ;
byte[] bArray = asciiEncoder.Ge tBytes("Test");

Hashtable ht = new Hashtable();
ht.Add(bArray," Some value");

byte[] bArrayNew = asciiEncoder.Ge tBytes("Test");

Console.WriteLi ne("Result : "+ ht.Contains(bAr ray)); //true
Console.WriteLi ne("Result : "+ ht.Contains(bAr rayNew)); //false
Console.Read();

--------------------------------------------------

As seen here, by using the same object , the contains() will return true, while using a new object with same 'value' returns false. If I am correct the contains() command does not look at the values in an object. As for
primitive types like normal string, int and etc, it does not have any
problem.

So I was wondering if there is anyway i can keep byte arrays as keys in
hashtable and still find it in the time complexity of O(1). If i read all the keys out and make a byte comparison it would be O(n) time complexity, thus defeating the purpose of a hash table.

Is there any other command in hashtable or some functions i can overwrite? Would prefer a much simpler way if possible.

Thanks in advance

Joseph Lee

Nov 16 '05 #5
Joseph Lee wrote:
ic, i will look into it. Thanks
You can create your Hashtable with custom Hashcode provider and comparer.

Look at the Hashtable( IHashCodeProvid er, IComparer) constructor.

With a Hashtable created with that constructor you can provide your own
methods for comparing the byte arrays instead of using the default
methods provided by the Array class.

Joey

"Rakesh Rajan" <Ra*********@di scussions.micro soft.com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
As Bob said, it's the reference that is being tested and not the contents.


If
I remember right, searching in the lines of overriding Equals and


GetHashCode
might help. Sorry, I can't recollect them now...
"Joseph Lee" wrote:

Hi All,

I am having problem when i am using hashtable to keep an array of bytes
value as keys.

Take a look at the code snippet below

---------------------------------------------------

ASCIIEncodin g asciiEncoder = new ASCIIEncoding() ;
byte[] bArray = asciiEncoder.Ge tBytes("Test");

Hashtable ht = new Hashtable();
ht.Add(bArra y,"Some value");

byte[] bArrayNew = asciiEncoder.Ge tBytes("Test");

Console.Writ eLine("Result : "+ ht.Contains(bAr ray)); //true
Console.Writ eLine("Result : "+ ht.Contains(bAr rayNew)); //false
Console.Read ();

--------------------------------------------------

As seen here, by using the same object , the contains() will return
true,
while using a new object with same 'value' returns false. If I am
correct
the contains() command does not look at the values in an object. As for
primitive types like normal string, int and etc, it does not have any
problem.

So I was wondering if there is anyway i can keep byte arrays as keys in
hashtable and still find it in the time complexity of O(1). If i read
all
the keys out and make a byte comparison it would be O(n) time
complexity,
thus defeating the purpose of a hash table.

Is there any other command in hashtable or some functions i can
overwrite?
Would prefer a much simpler way if possible.

Thanks in advance

Joseph Lee


--
mikeb
Nov 16 '05 #6
But that would again mean he will have to iterate and compare each entries
right? Wonder whether there are any other efficient implementations ?
"mikeb" wrote:
Joseph Lee wrote:
ic, i will look into it. Thanks


You can create your Hashtable with custom Hashcode provider and comparer.

Look at the Hashtable( IHashCodeProvid er, IComparer) constructor.

With a Hashtable created with that constructor you can provide your own
methods for comparing the byte arrays instead of using the default
methods provided by the Array class.

Joey

"Rakesh Rajan" <Ra*********@di scussions.micro soft.com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
As Bob said, it's the reference that is being tested and not the contents.


If
I remember right, searching in the lines of overriding Equals and


GetHashCode
might help. Sorry, I can't recollect them now...
"Joseph Lee" wrote:
Hi All,

I am having problem when i am using hashtable to keep an array of bytes
value as keys.

Take a look at the code snippet below

---------------------------------------------------

ASCIIEncodin g asciiEncoder = new ASCIIEncoding() ;
byte[] bArray = asciiEncoder.Ge tBytes("Test");

Hashtable ht = new Hashtable();
ht.Add(bArra y,"Some value");

byte[] bArrayNew = asciiEncoder.Ge tBytes("Test");

Console.Writ eLine("Result : "+ ht.Contains(bAr ray)); //true
Console.Writ eLine("Result : "+ ht.Contains(bAr rayNew)); //false
Console.Read ();

--------------------------------------------------

As seen here, by using the same object , the contains() will return


true,
while using a new object with same 'value' returns false. If I am


correct
the contains() command does not look at the values in an object. As for
primitive types like normal string, int and etc, it does not have any
problem.

So I was wondering if there is anyway i can keep byte arrays as keys in
hashtable and still find it in the time complexity of O(1). If i read


all
the keys out and make a byte comparison it would be O(n) time


complexity,
thus defeating the purpose of a hash table.

Is there any other command in hashtable or some functions i can


overwrite?
Would prefer a much simpler way if possible.

Thanks in advance

Joseph Lee


--
mikeb

Nov 16 '05 #7
Rakesh Rajan wrote:
But that would again mean he will have to iterate and compare each entries
right? Wonder whether there are any other efficient implementations ?
Assuming I understand his problem (that he needs the byte arrays to
perform a 'deep' compare of the contents of the arrays), then yes -
these interface implementations would need to generate a hash value that
was dependent on the contents of the byte array and to compare the
entries of the arrays.

The check for equality could certainly have some short cuts so that a
member-by-member comparison might not always be needed:

1) if there's reference equality, the arrays are by definition equal
2) if the array lengths are different, then the arrays must not be
equal.

The hashcode might be cacheable if the byte arrays are not changed once
they are created. Indeed, once the byte arrays are added to the
hashtable, it would be a bug to modify them in such a way that the
hashcode (or equality test) would render different results.

There may be other techniques that can improve the efficiency
(particularly if unsafe code can be used). However, my main point was
that there's a way for the OP to get the behavior he needs without too
much work.

Performance is of no consequence if things don't even work correctly in
the first place.


"mikeb" wrote:

Joseph Lee wrote:
ic, i will look into it. Thanks


You can create your Hashtable with custom Hashcode provider and comparer.

Look at the Hashtable( IHashCodeProvid er, IComparer) constructor.

With a Hashtable created with that constructor you can provide your own
methods for comparing the byte arrays instead of using the default
methods provided by the Array class.

Joey

"Rakesh Rajan" <Ra*********@di scussions.micro soft.com> wrote in message
news:87***** *************** **************@ microsoft.com.. .
As Bob said, it's the reference that is being tested and not the contents.

If
I remember right, searching in the lines of overriding Equals and

GetHashCod e
might help. Sorry, I can't recollect them now...
"Joseph Lee" wrote:

>Hi All,
>
>I am having problem when i am using hashtable to keep an array of bytes
>value as keys.
>
>Take a look at the code snippet below
>
>---------------------------------------------------
>
>ASCIIEncod ing asciiEncoder = new ASCIIEncoding() ;
>byte[] bArray = asciiEncoder.Ge tBytes("Test");
>
>Hashtabl e ht = new Hashtable();
>ht.Add(bAr ray,"Some value");
>
>byte[] bArrayNew = asciiEncoder.Ge tBytes("Test");
>
>Console.Wr iteLine("Result : "+ ht.Contains(bAr ray)); //true
>Console.Wr iteLine("Result : "+ ht.Contains(bAr rayNew)); //false
>Console.Re ad();
>
>--------------------------------------------------
>
>As seen here, by using the same object , the contains() will return

true,
>while using a new object with same 'value' returns false. If I am

correct
>the contains() command does not look at the values in an object. As for
>primitiv e types like normal string, int and etc, it does not have any
>problem.
>
>So I was wondering if there is anyway i can keep byte arrays as keys in
>hashtabl e and still find it in the time complexity of O(1). If i read

all
>the keys out and make a byte comparison it would be O(n) time

complexity ,
>thus defeating the purpose of a hash table.
>
>Is there any other command in hashtable or some functions i can

overwrite?
>Would prefer a much simpler way if possible.
>
>Thanks in advance
>
>Joseph Lee
>
>
>


--
mikeb

--
mikeb
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
461
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
7
6747
by: War Eagle | last post by:
I have two byte arrays and a char (the letter S) I was to concatenate to one byte array. Here is what code I have. I basically want to send this in a one buffer (byte array?) through a socket. SWXXXXXXXXXYYYYZZZZZZZZZZZZZZZZZZZZZ Where S is the command for SEND and should just be the character S. Where W is a byte representing how long the filename (testfile.txt) is. In this case 12. Where XXXXXXX is converted from a string that...
8
4592
by: frekster | last post by:
Hi. I used to be able to do this easily in vb 6 via looping and preserving the source array data/size etc. How can I do this in vb.net? I've been trying for a while now and this should be an easy task but it just isn't clicking. For some reason, the preserve doesn't seem to be working.
2
11346
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been loaded Dim data64_1 As String = System.Convert.ToBase64String(result1, 0, result1.Length) Dim data64_2 As String = System.Convert.ToBase64String(result2, 0, result2.Length) Debug.WriteLine(IIf(data64_1 = data64_2, "Equals", "NOT Equals"))
6
2783
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1) using redim arrays to add to a normal byte array (2) using an ArrayList and finally (3) using a memorystream. These three classes are listed below the test sub called "TestBuildByteArray". It was interesting that using the memorystream was...
5
40102
by: Oleg Subachev | last post by:
Is there other way of comparing two byte arrays than iterating through the two and compare individual bytes ? Oleg Subachev
0
838
by: Rob | last post by:
Hi all, Ok - I've been toiling with some encyption - and I think I found why I was having a problem, it was because my encrypt and decrypt functions were using different keys....anyway...putting that to one side for the moment... Here's what I'm stuck with... Dim rijndaelManaged As rijndaelManaged
0
1904
by: JonJacobs | last post by:
When I add a series of byte arrays to an array list, then I read them back, all the arraylist byte array elements are identical to the last byte array entry. What is wrong? The following code will reproduce the problem. Thanks, Jon Jacobs Imports System.Text Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
3
4178
by: RobbSadler | last post by:
This post was meant to go at the end of another post named "How to Compare Two Byte Arrays" which was answered by Vadym Stetsyak, but it was closed. I used his code from that post to create this, and added two items discussed in the previous post: 1. different length arrays 2. buffer must be divisible by 4. I simply check the remaining bytes using the two byte pointers. public unsafe static bool CompareByteArrays(byte b1, byte b2) {...
0
10164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.