-1

This is my C# program: I have to decode Hex data into normal text in readable format. I prefer to use Hex class from apache library which i downloaded from http://commons.apache.org/codec/download_codec.cgi ,which is a jar file.

Jar files are not accepted by C# compilers. So after bit of googling, i came to know that i have to convert the commons-codec jar file into MSIL first using jbimp.exe from Visual Studio and then import it. I use Visual Studio 2010. But i couldnt find jbimp.exe utility to convert this jar to MSIL. I am unable to find JBimp utility on my system.

Please help me how and where can i get jbimp utility? and also how must i specify the MSIL code as package/namespace to C# file?

My C# code:

using org.apache.commons.codec.binary.Hex;

class mainw
{
    private static byte[] secret = new byte[] 
        {0x33, 0x34, 0x36, 0x32, 0x33, 0x36, 0x36, 0x36, 0x33, 0x36, 
         0x36, 0x32, 0x33, 0x36, 0x36, 0x36, 0x33, 0x37, 0x33, 0x33, 
         0x33, 0x36, 0x33, 0x32, 0x33, 0x37, 0x33, 0x35, 0x33, 0x36, 
         0x36, 0x33, 0x33, 0x36, 0x36, 0x33, 0x33, 0x36, 0x33, 0x35};

    public static void main(string[] args) 
    {
        Hex hex = new Hex();
        byte[] secretDecoded1 = hex.decode(secret);
        byte[] secretDecoded2 = hex.decode(secretDecoded1);
        System.out.println("The secret is: "+new String(secretDecoded2));
    }

}

2 Answers2

3

It's complete overkill to convert a Java library to IL just for hex manipulation. It would very rarely be a good idea to convert a Java library to IL at all, to be honest - there's almost always a "native" .NET equivalent library available, following .NET idioms etc.

It's not really clear what you mean to start with, to be honest - if you're starting with a byte array and want to convert that into text, you'd normally use something like

String text = Encoding.UTF8.GetString(bytes);

... using whichever encoding is appropriate.

To parse hex data (which is naturally text, not bytes) into its binary equivalent (which is a byte array) you could use something like the code I posted in this Stack Overflow answer.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

I am not 100% sure I understand the question correctly, but here is the code I use to convert Hex data to readable text in C# for Microsoft CRM attachments:

using (FileStream fileStream = new FileStream("c:\path\to\file\filename.ext", 
FileMode.OpenOrCreate))
{
    byte[] fileContent = Convert.FromBase64String("Your HEX String HERE");
    fileStream.Write(fileContent, 0, fileContent.Length);
}

Since this is standard C# code you could utilize it to write to a string variable instead of a file if that's what you are trying to do.

Pete

DigiOz Multimedia
  • 1,156
  • 1
  • 12
  • 28