0

I'm looking to convert this exact function from whatever language (Java I think) it is in to C#.

String hex_chr = "0123456789abcdef";
private String numToHex(int num) {
    String str = "";
    for (int j = 0; j <= 3; j++) {
        str += "" + hex_chr.charAt((num >> (j * 8 + 4)) & 15) + "" + hex_chr.charAt((num >> (j * 8)) & 15);
    }
    return str;
}
3
  • 3
    Have you tried something ? Commented Oct 25, 2013 at 22:41
  • It doesn't look like this method is complete - where is hex_chr defined, and what is it defined as? Where are you getting this function from? Commented Oct 25, 2013 at 22:41
  • Yes, I attempted to change hex_char.char into hex_char[code here] but that did not work. I also added the definition for hex_chr in the code. Commented Oct 25, 2013 at 22:42

2 Answers 2

4

If you were looking for an exact translation, you just need to deal with it as a char array. You can either use the ToCharArray() method or just index into the string directly.

String hex_chr = "0123456789abcdef";
private String numToHex(int num)
{
    String str = "";
    for (int j = 0; j <= 3; j++)
    {
        str += "" + hex_chr[(num >> (j * 8 + 4)) & 15] + "" + hex_chr[(num >> (j * 8)) & 15];
    }
    return str;
}

You may want to just use the .ToString("X") call that HighCore suggested, but keep in mind that this does not include the trailing zeroes present in the string that your code produces.

Likewise, to do that easier in Java the code should have just called Integer.toHexString().

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

Comments

1

The .Net Framework has a built-in function for that, there's no need to have that ugly, unmaintainable code in your project.

This example leverages a C# feature called Extension Methods

public static class IntExtensions
{
    public static string ToHex(this int value)
    {
       return value.ToString("X");
    }
}

Usage:

var myinteger = 10;
Console.WriteLine(myinteger.ToHex()); //Outputs "A"

-- or --

Console.WriteLine(255.ToHex()); //Outputs "FF"

8 Comments

While I too enjoy using C#, this isn't the place for a language debate. Your answer would be stronger without the Java deprecation/.Net superiority segment.
Looking at your answer before the prior edit, it was even more aggressively an attack on Java than it is now. Again, your answer would be better with just the facts. The two platforms are both very capable of solving this problem; Java just ends up being more verbose.
@HighCore, fine, your answer is not the place for a language "rant" then. Either way, the downvoters are correct that the editorizing is not going to be appreciated.
@HighCore Know where the appropriate venue for holding a discussion is. An answer to a question like this is most certainly the wrong venue. It's great that you can introduce a .Net problem-solving paradigm in the solution, but you can do so without smearing it in Java's face.
If you want to display it as a full byte, you should .ToString( "X2" )
|

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.