none
Hex value RRS feed

  • Question

  • How to translate C # in Hex value:

    private void esnJqbamQ(object sender, EventArgs e)
    {
       this.timer_0.Stop();
       this.timer_0.Interval = 60000 * (10 + this.random_0.Next(5));
       this.timer_0.Start();
    }

    Wednesday, November 30, 2011 9:31 AM

Answers

  • I guess it's an assembly you opened. It's not the text of the program you're seeing, it's the compiled IL instructions.

    To get the bytes from a method body, get a handle to the method and get its IL as a byte array. Supposing the method is defined in the class named Program:

    MethodInfo method = typeof(Program).GetMethod("esnJqbamQ", BindingFlags.NonPublic | BindingFlags.Instance);
    StringBuilder builder = new StringBuilder();
    foreach (byte b in method.GetMethodBody().GetILAsByteArray())
    {
            builder.Append(b.ToString("x2"));
    }
    string hexstring = builder.ToString();
    

     

    • Marked as answer by Stenl1 Wednesday, November 30, 2011 6:09 PM
    Wednesday, November 30, 2011 1:25 PM

All replies

  • Hi,

    can you clarify the question please?

    What do you want to display in hex format?

    I understood that you want to display something in Hex Format but the code you displayed does not seem to have anything with that.

    You can use hex values in c# like:

    int i = 0x10; // i = 16

    And you can display an integer as hex value this way:

    string iString = i.ToString("X");  // 0x10 again

    With kind regards,

    Konrad

     

    Wednesday, November 30, 2011 9:37 AM
  • I mean, suppose there is a text document with this code:
    private void esnJqbamQ (object sender, EventArgs e)
    {
        this.timer_0.Stop ();
        this.timer_0.Interval = 60 000 * (10 + this.random_0.Next (5));
        this.timer_0.Start ();
    }
    I need to create a program so that a single text file c code, copied to another in the HEX value and expansion. Bin
    To the code from the file becomes Hex!

    When you open a HEX editor on:

     

    133005003700000000000000027B2B03
    00046FE900000A027B2B0300042060EA
    00001F0A027B320300041B6FF000000A
    585A6FED00000A027B2B0300046FEE00
    000A2A


     


    • Edited by Stenl1 Wednesday, November 30, 2011 11:10 AM
    Wednesday, November 30, 2011 11:02 AM
  • Sorry, I still do not understand you and need more clarification.

    In general, hex values are just one form of representation. All data can be displayed in any form. But it is not always possible to get the binary form from an graphical representation without further information.

    Example: 1

    This is just something displayed. But it could be:
    0x01 - just a byte with value 1
    0x00 0x01 - just a word ... (Could be continued ... even with little endian / big endian ... )
    It could be a double value - which would make it even more complex.
    It could be a string. So in ASCII format, it could be 0x30. But ASCII is just an encoding. It could be stored in UTF-7, UTF-8, UTF-16 or UTF32, too. (Or even in multiple other formats!)

    So back to files:

    When you have a file, then it is always a bunch of bytes. There might be some text in it e.g. the lines of code you posted but internally all this are just a couple of bytes. And each byte could be displayed in Hex Format. You can easily try it through an hex editor. ake any hex editor and open a text file with it.

    (I use Gizmo mainly to mount ISO Images but it also comes with a universal Editor. So after installing that tool, you can right click any file and then you get blocks of Hex-bytes!)

    So now you have to exactly define, what you want to do:
    - You read the text file (How do you read it?)
    - You do soemthing with the data you read (What exactly?)
    - You write something back ...

    So you can read the .c file with a Binary Reader and then use a Binary reader to write the read bytes to the .bin file. (Result is, that the content of the files is equal!)

    Or you can read the file as textfile. (The system will normaly take care of encodings. So you can read the file as text. The result can be a string in memory (which is stored UTF-16 encoded). This string can be encoded as you want (e.g. ASCII) and from that encoding, you can get the Bytes and then write them to the .Bin file. Result is again a text file but maybe in some different format. You have full control over what you write. So for example your .bin file could be a text file in which you write each byte not as byte but as a string. So if the first byte is 0x00, you could write "0x00 " or just "00 " into the file.

    But maybe you want to do something which is known as "compile". A compiler takes some input files and creates something new. So a c / c++ compiler is creating some object files (e.g. .o / .obj) or a c# compiler creates some MSIL modules / assemblies? Maybe this is, what you want.

    I hope I was able to show you, what kind of clarification I need to help you further. Maybe the information I wrote is of little help already.

    With kind regards,

    Konrad

    Wednesday, November 30, 2011 11:32 AM
  • You must first choose an encoding. Then convert the string into an array of bytes using that encoding. Then get the hexadecimal notation of each byte.
    public static string GetHex(string s, Encoding encoding)
    {
        StringBuilder builder = new StringBuilder();
        foreach (byte b in encoding.GetBytes(s))
        {
            builder.Append(b.ToString("x2"));
        }
        return builder.ToString();
    }
    

    • Proposed as answer by Konrad Neitzel Wednesday, November 30, 2011 12:35 PM
    Wednesday, November 30, 2011 12:19 PM
  • Try to get your function in your code:

     

    private void esnJqbamQ (object sender, EventArgs e)
    {
        this.timer_0.Stop ();
        this.timer_0.Interval = 60 000 * (10 + this.random_0.Next (5));
        this.timer_0.Start ();
    }

     

    This value is:

     

    133005003700000000000000027B2B03
    00046FE900000A027B2B0300042060EA
    00001F0A027B320300041B6FF000000A
    585A6FED00000A027B2B0300046FEE00
    000A2A

    I would be very grateful!!!

    When you open the Notepad++: 

     

     [URL=http://www.radikal.ru][IMG]http://i055.radikal.ru/1111/84/90e5cec606e1.jpg[/IMG][/URL]

    What is the code I do not know. It seems to ANSI

     





    • Edited by Stenl1 Wednesday, November 30, 2011 1:00 PM
    Wednesday, November 30, 2011 12:48 PM
  • I guess it's an assembly you opened. It's not the text of the program you're seeing, it's the compiled IL instructions.

    To get the bytes from a method body, get a handle to the method and get its IL as a byte array. Supposing the method is defined in the class named Program:

    MethodInfo method = typeof(Program).GetMethod("esnJqbamQ", BindingFlags.NonPublic | BindingFlags.Instance);
    StringBuilder builder = new StringBuilder();
    foreach (byte b in method.GetMethodBody().GetILAsByteArray())
    {
            builder.Append(b.ToString("x2"));
    }
    string hexstring = builder.ToString();
    

     

    • Marked as answer by Stenl1 Wednesday, November 30, 2011 6:09 PM
    Wednesday, November 30, 2011 1:25 PM
  • The point is. There is a program with no serviceable kodom.My its opening in WinHex search for the faulty piece of code in Hex redaktore.I so as not to write code as Hex value. Writing an ordinary person understands the code and compile it into the Hex value. To replace in WinHex. And we get normal working ability of the program.
    I hope I explained clearly.
    Wednesday, November 30, 2011 1:37 PM
  • If you really want to know why this guy wants to do it, haha... :)

    http://translate.google.com/translate?u=http%3A%2F%2Fexelab.ru%2Ff%2Findex.php%3Faction%3Dvthread%26forum%3D5%26topic%3D19312%26page%3D0&sl=ru&tl=en&hl=&ie=UTF-8


    Eyal (http://shilony.net), Regards.
    • Edited by Eyal SolnikModerator Wednesday, November 30, 2011 1:50 PM
    • Proposed as answer by Wyck Wednesday, November 30, 2011 3:12 PM
    Wednesday, November 30, 2011 1:49 PM
    Moderator