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);
Community
  • 1
  • 1
aepheus
  • 6,747
  • 7
  • 32
  • 50
  • 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. – harold Oct 05 '11 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? – aepheus Oct 05 '11 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. – aepheus Oct 05 '11 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 `sbyte`s, and check whether they look the same. – harold Oct 05 '11 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. – Alexei Levenkov Oct 05 '11 at 16:45
  • I'm trying to put together a sample of input/output, I cannot put the one I am looking at for security reasons, and I don't have the Java version of this running on my machine, so I have to get it loaded. – aepheus Oct 05 '11 at 16:56
  • After implementing the java side, I found that they are indeed producing the same results. I was just following a poor example I guess. – aepheus Oct 05 '11 at 17:15

3 Answers3

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?

Sergey Sirotkin
  • 1,630
  • 11
  • 7
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.

Polynomial
  • 25,567
  • 8
  • 75
  • 106
-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.

Stefan
  • 13,716
  • 4
  • 52
  • 60
Gregory A Beamer
  • 16,342
  • 3
  • 23
  • 29