-4

I'm still confused whats better to use, datagridview or Be.HexEditor, I am pretty sure I'll be able to do it with datagridview, but I like be.hex more, especially speed, UI etc...

But I tried to understand the code of be.hex, and I can't figure out how does it work, and also is it possible to show values from bytes arrays to the HexBox(not only from opening .bin files). it also uses dynamicFileByteProvider, and there is no info on the net about this class.

dynamicFileByteProvider = new DynamicFileByteProvider(fileName);
dynamicFileByteProvider.Changed += new EventHandler(byteProvider_Changed);
dynamicFileByteProvider.LengthChanged += new EventHandler(byteProvider_LengthChanged);

my app will open 1024 bytes files max, and also it will read bytes from comm port

Tân
  • 1
  • 15
  • 56
  • 102
James_BK
  • 21
  • 1
  • 7
  • Please [edit] the question in such a way that there is a *clear question* with a *limited scope*. "How to create hex editor" to me is an enormous scope. I also can't see how it relates in any way to the very few lines of code that you posted. – Peter B Nov 21 '18 at 10:55

2 Answers2

1

I've tinkered a bit with this. What I did was

1) put an invisible picture box as control placeholder on the form, here named ph1

2) configure the HexBox control in Form_Load()

private HexBox hexBox;

private void Form1_Load(object sender, EventArgs e)
{
    hexBox = new HexBox()
    {
        Top = ph1.Top,
        Width = ph1.Width,
        Height = ph1.Height,
        Left = ph1.Left,
        Visible = true,
        UseFixedBytesPerLine = true,
        BytesPerLine = 16,
        ColumnInfoVisible = true,
        LineInfoVisible = true,
        StringViewVisible = true,
        VScrollBarVisible = true
    };
    this.Controls.Add(hexBox);
    this.Controls.Remove(ph1);
}

3) Load the actual file in DragDrop event

var filePath = ((string[])(e.Data.GetData(DataFormats.FileDrop)))[0];
var source = new FileByteProvider(filePath);
hexBox.ByteProvider = source;
hexBox.Refresh();

Example after drag/drop of a docx file onto the form:

enter image description here

Edit: if you wish to provide some self-generated array of bytes, it is as simple as this:

byte[] byteArr = {0xaa, 0x3f, 0x4b};
hexBox.ByteProvider = new DynamicByteProvider(byteArr);

Edit 2: To save the contents of the hex box: I am sure there is some better way to do this. What I found for now is to simply add a handler in the hex box definition block:

hexBox.CopiedHex += HexBox_CopiedHex;

Have some kind of "save" button with such a code:

private void button1_Click(object sender, EventArgs e)
{
    hexBox.SelectAll();
    hexBox.CopyHex();
    hexBox.SelectionLength = 0;
}

And such an event handler:

private void HexBox_CopiedHex(object sender, EventArgs e)
{
    var hex = Clipboard.GetText();
    var hexHex = hex.Split(' ');
    var hexArr = new byte[hexHex.Length];
    for (var i = 0; i < hexHex.Length; i++)
    {
        hexArr[i] = byte.Parse(hexHex[i], NumberStyles.AllowHexSpecifier);
    }
    File.WriteAllBytes(@"C:\00_Work\test.bin", hexArr);
}
LocEngineer
  • 2,847
  • 1
  • 16
  • 28
  • Thx for your anser, but what it actually does? – James_BK Nov 22 '18 at 04:30
  • i need to be able to put bytes array in the hexbox, not only from openning a bin file – James_BK Nov 22 '18 at 04:31
  • Did you try it? That's exactly what this does! I'll put a screenshot in my answer. You need to show a little more effort. – LocEngineer Nov 22 '18 at 09:42
  • I think now I know what you mean. I have adjusted my answer accordingly, see the edit. – LocEngineer Nov 22 '18 at 15:53
  • Also, how can i save file after i provided some self-generated array of bytes? Seems like it can save file only which was opened before. But how to create .bin file and write values on it from hexbox – James_BK Nov 23 '18 at 06:27
  • Go ahead and play! Try some things out! Try with `hexBox.ByteProvider.ApplyChanges();` and `hexBox.SelectAll();` followed by `CopyHex();` ... Whatever. Knock yourself out and especially: *find* out. Much more fun. :-) – LocEngineer Nov 23 '18 at 10:57
  • thanks a lot for your attention to the thread. I didnt get where i have to put hexBox.CopiedHex += HexBox_CopiedHex; But anyway i tried to convert the hex string which we copy with var hex = Clipboard.GetText(); then removed spaces with hex = hex.Replace(" ", string.Empty); then converted it to the bytes array byte[] testarray; testarray = StringToByteArray(test_string); – James_BK Nov 26 '18 at 10:35
  • Either something wrong in your `StringToByteArray` or using StreamWriter for a byte array, when you should use `File.WriteAllBytes`. See edited event handler. – LocEngineer Nov 26 '18 at 11:02
  • From where you installed the plugin? From nugget `Be.Windows.Forms.HexBox` ? https://www.nuget.org/packages/Be.Windows.Forms.HexBox#readme-body-tab – E235 May 14 '23 at 08:31
0

To show byte[] in hexbox use new DynamicByteProvider(byte[]

To get modified byte[] from hexbox use DynamicByteProvider.Bytes.ToArray()

dns_nx
  • 3,651
  • 4
  • 37
  • 66