11 Replies - 13401 Views - Last Post: 22 February 2010 - 03:41 AM Rate Topic: -----

#1 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

How to write hexadecimal file in c#?

Posted 21 February 2010 - 02:27 PM

Hello everybody, i hope I'm writing in the right place. As in the title of topic, i want to know how to write a file in hexadecimal form. Can you give me a simple example. I want it as simple as possible, I'm still a beginner in this language.
Thank you
Posted Image
Is This A Good Question/Topic? 0
  • +

Replies To: How to write hexadecimal file in c#?

#2 EvLSnoopY  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 93
  • Joined: 24-November 09

Re: How to write hexadecimal file in c#?

Posted 21 February 2010 - 04:44 PM

I don't know if this is going to help you or not but here's my take on this. Consider the following:
string hex = "2AF3";
Console.WriteLine(Convert.ToInt32(hex, 16))); // Prints 10995 out to the console.


The preceeding code converted the hex value "2AF3" to it's decimal counter part with the help of the Convert Class.
The hexadecimal number 2AF3 is equal, in decimal (with a base of 16), to (2 163) + (10 162) + (15 16) + 3, or 10,995.
From what I have read on the net is that this method only supports bases of 2, 8, 10, and 16.
Was This Post Helpful? 0
  • +
  • -

#3 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: How to write hexadecimal file in c#?

Posted 21 February 2010 - 05:07 PM

Thank you very much Mr EvLSnoopY , but that's not what i meant. I mean using stream (using System .IO). I really don't know if this possible, i want to write normal data in the code to be written to the file, and the code write the code in hexadecimal form. I know this possible with binary, but i don't know about hexadecimal.
Thank you , and still waiting for more answers.
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How to write hexadecimal file in c#?

Posted 21 February 2010 - 05:18 PM

Have a look at BinaryWriter.Write Method

Do you mean you want to write hexadecimal ASCII strings?

This post has been edited by n8wxs: 21 February 2010 - 05:19 PM

Was This Post Helpful? 0
  • +
  • -

#5 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: How to write hexadecimal file in c#?

Posted 21 February 2010 - 05:30 PM

i hope this can declare my question, i have this code here:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace usingfile
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamWriter sw = new StreamWriter(@"C:\FirstFile.text");
            sw.WriteLine("Hello everbody , welcome to absba forum");
            sw.WriteLine("This forum concerns about computer");
           
            String forum = " Absba";
            for (int number = 1; number <= 3; ++number )
            {
                sw.WriteLine("You are in :{0}",forum);
                
                    forum = "Almoshageb forum";
                if (number ==2)
                    forum = "Programming department";
                
            }
            sw.Close();
        }
    }
}  


this code give me this result:
Posted Image


can i have this result in hexadecimal form ?? this me question .
is there any method or class in c# let me do that with streams.

still waiting, and thank you Mr n8wxs , i'll check the link , i guess it's useful from me anyway.
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How to write hexadecimal file in c#?

Posted 21 February 2010 - 06:08 PM

Well, try this: :)

class Program
{
    static void Main(string[] args)
    {
        StreamWriter sw = new StreamWriter(@"FirstFile.text");

        sw.WriteLine(toHex("Hello everbody , welcome to absba forum"));
        sw.WriteLine(toHex("This forum concerns about computer"));

        String forum = " Absba";

        for (int number = 1; number <= 3; ++number)
        {
            sw.WriteLine(toHex(String.Format("You are in :{0}", forum)));

            forum = "Almoshageb forum";
            if (number == 2)
                forum = "Programming department";
        }
        
        sw.Close();
    }

    private static string toHex(string arg)
    {
        StringBuilder sb = new StringBuilder();

        char[] inp = arg.ToCharArray();

        for (int i = 0; i < inp.Length; i++)
        {
            sb.Append(((int)inp[i]).ToString("X"));

        }

        return sb.ToString();
    }
}



This produces:

Quote

48656C6C6F2065766572626F6479202C2077656C636F6D6520746F20616273626120666F72756D
5468697320666F72756D20636F6E6365726E732061626F757420636F6D7075746572
596F752061726520696E203A204162736261
596F752061726520696E203A416C6D6F73686167656220666F72756D
596F752061726520696E203A50726F6772616D6D696E67206465706172746D656E74

Was This Post Helpful? 1
  • +
  • -

#7 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: How to write hexadecimal file in c#?

Posted 21 February 2010 - 06:33 PM

waaaw, that was gooood :bigsmile: . Thank you very much Mr n8wxs, though i still have to understand some sentence >.< , but it's my job andyway.
i want to ask you about this code , i found it in the net, it's supposed to be true, but when i debug it in vs2005, it gives me this error.
Util doesn't exist in the current context.
using System;
using System.IO;

/**
* Application for printing file to console as hexadecimal.
*/
public class PrintHex {
    
    public static void Main(String[] args) {
        if(args.Length!=2) {
            PrintUsage();
            return;
        }
        
        IEmitter emitter;
        if(args[0] == "-h") {
            emitter = new HexEmitter();
        } else if(args[0] == "-s") {
            emitter = new PerLineEmitter();
        } else {
            PrintUsage();
            return;
        }
        
        try {
            
            string path = args[1];
            
            if(!File.Exists(path)) {
                Console.WriteLine("File does not exist: {0}", path);
                return;
            }
            
            using(FileStream fs = File.OpenRead(path)) {
                while(true) {
                    int b = fs.ReadByte();
                    if(b == -1) {
                        break;
                    }
                    emitter.PrintByte(B)/>;
                }
                emitter.Flush();
            }
            
        } catch(Exception e) {
            Console.WriteLine(e);
        }
    }
    
    public static void PrintUsage() {
        Console.WriteLine("usage: PrintHex.exe -<switch> <filename>");
        Console.WriteLine("  -h  hex dump");
        Console.WriteLine("  -s  one byte per line");
    }
    
}

/**
* Interface for formatting byte output.
*/
public interface IEmitter {
    /**Called for every byte*/
    void PrintByte(int B)/>;
    /**Called after class is finished with*/
    void Flush();
}

/**
* Prints one byte per line as character, decimal and hex value.
*/
public class PerLineEmitter : IEmitter {
    public void PrintByte(int B)/> {
        char ch = Util.ToSafeAscii(B)/>;
        Console.WriteLine(ch+"\t"+b+"\t"+Util.ToHexString(B)/>);
    }
    public void Flush() {}
}

/**
* Prints multiple bytes per line followed by characters.
*/
public class HexEmitter : IEmitter {
    private const int NBUFF = 16;
    private int buffered = 0;
    private int[] bytes = new int[NBUFF];
    public void PrintByte(int B)/> {
        bytes[buffered] = b;
        buffered++;
        if(buffered == NBUFF) {
            Flush();
        }
    }
    public void Flush() {
        if(buffered<=0) {
            return;
        }
        
        for(int i=0; i<NBUFF; i++) {
            if(i >= buffered) {
                Console.Write("   ");
            } else {
                string hex = Util.ToHexString(bytes[i]);
                Console.Write(hex);
                Console.Write(" ");
            }
        }
        
        Console.Write("  ");
        
        for(int i=0; i<NBUFF; i++) {
            if(i >= buffered) {
                Console.Write(" ");
            } else {
                char ch = Util.ToSafeAscii(bytes[i]);
                Console.Write(ch);
            }
        }
        
        Console.WriteLine();
        
        buffered = 0;
    }
}

/**
* Utility methods.
*/
public class Util {
    
    /**
    * Converts a byte to a hexadecimal string value.
    */
    public static string ToHexString(int B)/> {
        const int mask1 = 0x0F;
        const int mask2 = 0xF0;
        
        string ret = "";
        
        int c1 = (b & mask1);
        int c2 = (b & mask2) >> 4;
        
        ret = ret + ToHexChar(c2) + ToHexChar(c1);
        return ret;
    }
    
    /**
    * Converts the given byte to a hex character.
    */
    private static char ToHexChar(int B)/> {
        const int ascii_zero = 48;
        const int ascii_a = 65;
        
        if(b>=0 && b<=9) {
            return (char) (b + ascii_zero);
        }
        if(b>=10 && b<=15) {
            return (char) (b + ascii_a - 10);
        }
        return '?';
    }
    
    /**
    * Converts the byte to a visible ASCII character or
    * underscore if it is not.
    */
    public static char ToSafeAscii(int B)/> {
        if(b>=32 && b<=126) {
            return (char) b;
        }
        return '_';
    }
    
}




the site says : C:\> PrintHex.exe -h PrintHex.cs
i didn't know what does this mean , and way I'm getting the error though it must be right.

of course I'm gonna take your code cause it looks easier to me, but i want to know>

Thank again Mr n8wxs.
Was This Post Helpful? 0
  • +
  • -

#8 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: How to write hexadecimal file in c#?

Posted 21 February 2010 - 06:33 PM

/////////////

This post has been edited by Bent al-Yemen: 22 February 2010 - 03:43 AM

Was This Post Helpful? 0
  • +
  • -

#9 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: How to write hexadecimal file in c#?

Posted 21 February 2010 - 07:35 PM

Sorry about the code you gave me Mr n8wxs
 sb.Append(((int)inp[i]).ToString("X"));



what does the letter "x" mean, where did it come from.
Was This Post Helpful? 0
  • +
  • -

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How to write hexadecimal file in c#?

Posted 21 February 2010 - 08:02 PM

Have a look at Standard Numeric Format Strings
Was This Post Helpful? 0
  • +
  • -

#11 EvLSnoopY  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 93
  • Joined: 24-November 09

Re: How to write hexadecimal file in c#?

Posted 21 February 2010 - 08:33 PM

This thread has been very informative. Thanks to every one who contributed!
Was This Post Helpful? 0
  • +
  • -

#12 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: How to write hexadecimal file in c#?

Posted 22 February 2010 - 03:41 AM

Thank you agina, i got it.
I like this forum very much.

This post has been edited by Bent al-Yemen: 22 February 2010 - 03:45 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1