Click here to Skip to main content
13,095,218 members (67,554 online)
Rate this:
Please Sign up or sign in to vote.
See more:
hello everybody I have question I am trying to do a hexeditor and I can read bin file in hex mode but I cannot read it as ascii.
I am using this function
private static string hx2Asc(string hxStr)
            byte[] tmp;
            int j = 0;
            tmp = new byte[(hxStr.Length)/2];
            for(int i = 0; i< hxStr.Length - 2; i += 2)
                tmp[j] = (byte)Convert.ToChar(Int32.Parse(hxStr.Substring(i,2),NumberStyles.HexNumber));
            return Encoding.GetEncoding(1252).GetString(tmp);

and this to call this function
string str = rtb.Text.Replace(" ", "");
                str = str.Replace("\n", "");
                str = hx2Asc(str);
                textBox4.Text = str;

I can read just 24445098 LS this in textbox the rest of the characters is not coming.
Thank you for now
Posted 18-Jun-13 3:00am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The first thing you want to do if you want a hex editor is to stop using strings: use byte values at all times - strings contain unicode characters which are of variable length, and do not have to be 8 bits.
Read your file as bytes, store it as bytes and convert it to ASCII values for display only when you need to - which is pretty simple:
byte[] bytes = ...
string s = System.Text.Encoding.ASCII.GetString(bytes);
It may well be worth your copying the area of the byte array you are interested in into a new short array before you call this (as it converts the whole array to a string). Do note that the values in the string are the unicode interpretation of the byte as an ASCII value, not ASCII values themselves, so generating a new binary file from the string will not necessarily give you the same file as you started with - hence the "keep it as a byte array" comment above.
brknlpr 18-Jun-13 10:12am
firstly thank you for your response. However I tried it before and also tried it now again
but it didnt work again I am getting the same result just like I got before.

this is my whole reading bin file codes

private void açToolStripMenuItem_Click(object sender, EventArgs e)
rtb.Text = "";
string filepath = Path.GetDirectoryName(hexAc.FileName);
string filename = Path.GetFileName(hexAc.FileName);
//dosya uzantısı için 2 sini birleştiriyoruz
string file = filepath + "\\" + filename;
label1.Text = filepath;
label2.Text = filename;

FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);

FileInfo fInfo = new FileInfo(file);
int s = Convert.ToInt32(fInfo.Length);
byte[] bit = br.ReadBytes(s);

int count = 0;

for (int i = 0; i < bit.Length; i++)

rtb.Text += bit[i].ToString("X2") + " ";
if (count == 16)
rtb.Text += "\n";
count = 0;


string st = Encoding.ASCII.GetString(bit);
textBox4.Text = st;

MessageBox.Show("Dosya Açılamadı Lütfen yeniden deneyiniz");

is there anything that I miss ?
OriginalGriff 18-Jun-13 10:32am
I just tried your code, and it does exactly what I would expect. What kind of effect are you getting that is not what you expect?
brknlpr 18-Jun-13 10:36am
I am getting only this in my textbox as ascii -- 24445098 LS but file is not short as this
OriginalGriff 18-Jun-13 10:43am
Textboxes don't display everything - if a character isn't in the Uncode set, then it won't display at all.
Try this:
string st = Encoding.ASCII.GetString(bit);
st = st.Replace((char) 0, '?');
textBox3.Text = st;
and see what happens...
brknlpr 18-Jun-13 10:42am
when I run it in debug mode I can see in my string st this --> 24445098 LS\0\0\0\0\0\02\0G?e1I-00023280DF5VF6844007354S\0\0\0\0S001010339S001010339S001010339\0\0\0\0\0\0030725\0\0\0\0\0\0\0\0\0\0\0\005\0\a??\a??\a??\a??\a??\a\0??$?\b?b?)???^??n??Fl?\a??\a??\a??\a??\a??\a\0??$?\b?b?)???^??n??Fl?\a??\a\0\0\0\0??\0??\a??\a\0??$?\b?b?)???^??n??Fl?" but I it only writes 24445098 LS to the textbox
brknlpr 18-Jun-13 10:45am
thank you very much it worked :) could you please explain a little bit why we do this ?
OriginalGriff 18-Jun-13 10:59am
NULL is a funny character - it has the value zero and it is used in many, many systems to indicate "end of string". So many systems in fact that it has a special place even in .NET and Unicode despite .NET strings not using it for that purpose (they store an int32 length value instead, which is one reason why a string can't exceed 2GB in length)

A .NET TextBox uses a Windows CTextBox primitive to actually do it's displaying and that is written in C / C++ - both of which use NULL as "end of string", so when your textbox meets the first one it stops displaying any more info! This isn't a bug - I think it is documented somewhere so it's a "Feature" instead - it's a part of how things work. There is a good chance that you need to set up some kind of translation system to convert the "text" data before you display it, because there are several other characters that will have an effect on the text instead of displaying as a character: 0x09, 0x0a, 0x0d are all examples which will probably mess you up! (But there are more, it might be easier to list the ones you do what to see!)

Do note that this is not the same as the .NET null value - that is not a character at all!
OriginalGriff 18-Jun-13 11:02am
BTW: You do realise you can replace all the faffing about with Streams and FileInfos with a single line:
byte[] bit = File.ReadAllBytes(file);
brknlpr 18-Jun-13 11:16am
Thank you very very much again. you helped me a lot have a good day :)
OriginalGriff 18-Jun-13 12:53pm
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 18 Jun 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100