1

Possible Duplicate:
.NET String to byte Array C#

I need to convert a string to a byte[]. If that were it the task would be simple. I need to write it in C#, but I need it to act like Java.

There are two problems I'm seeing:

Endianness: Java stores things internally as Big Endian, while .NET is Little Endian by default.

Signedness: C# bytes are unsigned. Java bytes are signed.

How can I take a UTF-8 string, and get a byte[] that will match what it would in Java.

To give more context, I have these two functions (one in java, one in c#), they create different hashes, I suspect it's due to the difference in byte representation in .net vs java. Java:

String key = "test";
String data = "test";
String algorithm = "HmacSHA1";
Charset charset = Charset.forName("utf-8");
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), algorithm);
Mac mac = Mac.getInstance(algorithm);
mac.init(signingKey);
return new String(Base64.encodeBase64(mac.doFinal(data.getBytes(charset))), charset);

C#:

string key = "ed2ee2e0-65c1-11de-8a39-0800200c9a66";
string data = "test";
byte[] aKey = Encoding.UTF8.GetBytes(key);
byte[] aData = Encoding.UTF8.GetBytes(data);
HMACSHA1 oEncode = new HMACSHA1(aKey);
byte[] aBytes = oEncode.ComputeHash(aData);
return Convert.ToBase64String(aBytes);
7
  • It doesn't matter whether they're signed or unsigned, that's just a matter of interpreting the same bits differently. It will affect division, multiplication and right shift (and nothing else), but you can just cast them to sbyte first. Commented Oct 5, 2011 at 16:03
  • @harold - so if I need to convert a string to byte[], and my java code and c# code need to come up with the same byte[], how does it not matter? won't the byte[] be different if you us signed vs unsigned bytes? Commented Oct 5, 2011 at 16:09
  • @Donut - I really do wish it was that simple. It's not for the reasons stated in the question - the same string comes out to two different byte[] in c# vs java. Commented Oct 5, 2011 at 16:11
  • 3
    Nope, they will be the same. They'll look different in the debugger, but the bits will be the same. If there's a difference, it won't be due to signedness. You can make them look the same by casting them to sbytes, and check whether they look the same. Commented Oct 5, 2011 at 16:12
  • aepheus, please provide an example of string that comes out with different arrays of bytes in Java and C# (not from your sample code that does whole lot of other things too). Otherwise will close. Commented Oct 5, 2011 at 16:45

3 Answers 3

0

Just use:

byte[] bytes = Encoding.UTF8.GetBytes("SomeString");

There are a few notes though:

  1. There is no such thing as "UTF-8" string in .NET: all strings are Unicode (UCS-2).
  2. There is no big endian/little endian byte order in UTF-8, the order of bytes is defined by format. Check RFC-2279 for details.

Why do you care if byte is signed or not?

Sign up to request clarification or add additional context in comments.

Comments

0

As was commented above, the behaviour is exactly the same for byte endianness. Signed vs. unsigned isn't really a factor here, either.

Just use this:

byte[] bytes = Encoding.UTF8.GetBytes("my string");

And you should be fine.

If you explain why you need something other than this case, I might be able to help further.

Comments

-1

It has been quite some time since I played in a heterogenous environment, but I am not convinced the signed versus unsigned should make a huge difference. If I am correct, the ordering of the array is the big issue.

There are two solutions (potential solutions?):

  1. Flip the array in .NET using Array.Reverse()
  2. use little endian in Java

Either should solve the ordering problem.

3 Comments

Reverse on a string would just do what it says - reverse the string. The author mistakenly thought bytes had endianness, which they don't.
The bytes do not themselves, when they represent a string they do.
I assume he thought multibyte characters had endianness rather than bytes themselves.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.