2

Hello I have a code in Java I want to make it to code in C # but I'm having a problem:

This code (func):

DatatypeConverter.parseHexBinary (temp);

I'm looking for a replacement in C#

On the other hand I have the full code should do this in Java but also where I encountered a problem in this code:

public static byte [] hexStringToByteArray (String s)
{
    int len ​​= s.Length;
    byte [] data = new byte [len / 2];
    for (int i = 0; i <len; i + = 2)
    {
        data [i / 2] = (byte) ((Character.digit (s.charAt (i), 16) << 4)
                                     + Character.digit (s.charAt (i + 1), 16));
    }
    return data;
}

The problem is that the compiler does not recognize the

Character.digit (s.charAt (i)

Any help is appreciated. Thanks!

5
  • I think you used Google translate for that last line from hebrew. right? :P Commented Dec 25, 2016 at 11:25
  • genius!!!!!!!!! Commented Dec 25, 2016 at 11:28
  • I live in Modiin, btw. (I'm also trying to answer your question right now) Commented Dec 25, 2016 at 11:28
  • 1
    Beer Sheva :) Thanks! Commented Dec 25, 2016 at 11:32
  • did you work it out? Commented Dec 25, 2016 at 12:07

1 Answer 1

1

To convert a hex string to a byte array, you can use:

public static byte[] HexStringToByteArray(string s)
{
    int len = s.Length;
    byte[] data = new byte[len/ 2];
    for (int i = 0; i < len; i += 2)
    {
        data[i / 2] = Convert.ToByte(s.Substring(i, 2), 16);
    }
    return data;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.