Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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));
                j++;
            }
            return Encoding.GetEncoding(1252).GetString(tmp);
        }



and this to call this function
C#
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

1 solution

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:
C#
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.
 
Share this answer
 
Comments
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)
{
hexAc.ShowDialog();
rtb.Text = "";
try
{
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") + " ";
count++;
if (count == 16)
{
rtb.Text += "\n";
count = 0;
}

}

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




}
catch(Exception){
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    
:laugh:
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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900