2
\$\begingroup\$

Another journey down the difficult path! I'd like to know if there's anything I haven't accounted for, if this can be significantly improved, etc.

(Just as a side-note, I know that some of the formatting is a bit off; some places are spaced out a bit to make it more visually appealing.)

using System;
namespace Data
{
 public class BinaryHelper
 {
 public static string ByteToHexString (byte[] bytes)
 {
 char[] c = new char[bytes.Length * 2];
 byte b;
 for (int i = 0; i < bytes.Length; i++) {
 b = ((byte)(bytes [i] >> 4));
 c [i * 2] = (char)(b > 9 ? b + 0x37 : b + 0x30);
 b = ((byte)(bytes [i] & 0xF));
 c [i * 2 + 1] = (char)(b > 9 ? b + 0x37 : b + 0x30);
 }
 return new string (c);
 }
 public static byte[] HexStringToByte (string hex)
 {
 int len = hex.Length;
 byte[] bytes = new byte[len / 2];
 for (int i = 0; len > i; i += 2) {
 bytes [i / 2] = Convert.ToByte (hex.Substring (i, 2), 16);
 }
 return bytes;
 }
 }
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Sep 23, 2016 at 18:48
\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

Ask yourself "how can I make the meaning and intention of the code more clear?"

Consider making small helper methods rather than duplicating code:

static byte HighNibble(byte b) => (byte)(b >> 4);
static byte LowNibble(byte b) => (byte)(b & 0xF);
static char NibbleToHex => ... etc.

And now instead of

 b = ((byte)(bytes [i] >> 4));
 c [i * 2] = (char)(b > 9 ? b + 0x37 : b + 0x30)
 b = ((byte)(bytes [i] & 0xF));
 c [i * 2 + 1] = (char)(b > 9 ? b + 0x37 : b + 0x30);

Which is overwhelming in the sheer amount of punctuation, we have:

 c [i * 2] = NibbleToHex(HighNibble(bytes[i])); 
 c [i * 2 + 1] = NibbleToHex(LowNibble(bytes[i]));

So much easier to read and understand.

answered Sep 23, 2016 at 20:35
\$\endgroup\$
0
2
\$\begingroup\$

I found the magic numbers in this line less than obvious:

c [i * 2] = (char)(b > 9 ? b + 0x37 : b + 0x30);

0x30 is clearly '0', however 0x37 is well... '7'. I actually find this more expressive, even though it's longer:

c[i * 2] = (char)(b > 9 ? b + 'A' - 10: b + '0');

If you're interested you can also find a rather more concise version of ByteToArrayString which makes use of BitConverter to perform the translation in this answer. It essentially becomes a one-liner:

return BitConverter.ToString(bytes).Replace("-","");
answered Sep 23, 2016 at 20:20
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.