Click here to Skip to main content
14,635,966 members
Rate this:
Please Sign up or sign in to vote.
I have below code
byte[] bytestring= somefunction(); 
//somefunction() return a byte array..
 string st1 =  Encoding.UTF32.GetString(bytestring);
 byte[] bytestring2 = Encoding.UTF32.GetBytes(st1);
 bool st =  bytestring2.Equals(bytestring);

// RESULT IS st = false  
// Desired result st = true

I getting st = false, But I think it should be true...
what is the problem in ablove code I tried UTF8 , UnicodeEncoding .but result is same.
If bytestring2 has encoded value then how decode it( how get my old value)...

Thanks in Advance
Hemant Singh[^]
Updated 27-Dec-12 3:07am
Thomas Daniels 27-Dec-12 8:09am
What's the error?
Hemant Singh Rautela 27-Dec-12 9:00am
the boolean variable st value is false.
But i think it should be true....
Rate this:
Please Sign up or sign in to vote.

Solution 2


String.Equals don't have any overload on byte array, when you pass byte array to equals function that treated as object, so no compilation error occur but it's returns false.

Hemant Singh Rautela 27-Dec-12 12:53pm
It is not String.Equals

bytestring2 and bytestring both are byte array.

It is object.Equals(object) method.
Suvabrata Roy 27-Dec-12 23:56pm
Then Check your SomeFunction(), it returning properly or not
Rate this:
Please Sign up or sign in to vote.

Solution 1

You must exactly know what is going on under the hood in method
byte[] bytestring= somefunction(); 

For exampple what is it converted fron a specific text encoding to byte array,
and what i have seen, you just try to convert it bytes with UTF encoding , and check thei equality
Hemant Singh Rautela 27-Dec-12 12:56pm
somefunction() returns a byte array, it is implementd for RSA algorithm
Oleksandr Kulchytskyi 27-Dec-12 14:24pm
I saw that somefuc returns a bytes array , that is all what i know.
But what exact it does? which encodings , and so on??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100