Click here to Skip to main content
15,885,141 members
Articles / Programming Languages / C#

Network Stuff (easy socket v3)

Rate me:
Please Sign up or sign in to vote.
4.87/5 (52 votes)
2 Jun 2005 215.8K   6.1K   137  
A class with event handlers for TCP, UDP or ICMP sockets; includes ping, traceroute, whois, ARP, and IPHelper functions and raw packets forging/ capturing.
/*
Copyright (C) 2004 Jacquelin POTIER <jacquelin.potier@free.fr>
Dynamic aspect ratio code Copyright (C) 2004 Jacquelin POTIER <jacquelin.potier@free.fr>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
using System;

namespace easy_socket
{
    /// <summary>
    /// 
    /// </summary>
    public class hexa_convert
    {
        public static string hexa_to_string(string str_hexa_data,System.Text.Encoding encoding)
        {
            byte[] b=hexa_convert.hexa_to_byte(str_hexa_data);
            if (b==null)
                return "";
            return encoding.GetString(b,0,b.Length);
        }
        public static string hexa_to_string(string str_hexa_data)
        {
            return hexa_convert.hexa_to_string(str_hexa_data,System.Text.Encoding.ASCII);
        }
        
        public static byte[] hexa_to_byte(string s)
        {
            try
            {
                if (s.Length==0)
                    return null;
                // remove separators if any
                s=s.Replace(" ","");
                s=s.Replace("-","");
                s=s.Replace(".","");
                s=s.Replace(":","");
                // make string size a multiple of 2
                if ((s.Length%2!=0))
                {
                    s="0"+s;
                }
                int size=s.Length/2;
                byte[] b_ret=new byte[size];
                for(int cpt=0;cpt<size;cpt++)
                {
                    b_ret[cpt]=byte.Parse(s.Substring(2*cpt,2),
                        System.Globalization.NumberStyles.HexNumber);
                }
                return b_ret;
            }
            catch
            {
                return null;
            }
        }
        public static string byte_to_hexa(byte[] b,string separator)
        {
            string s=byte_to_hexa(b);
            return s.Replace("-",separator);
        }
        public static string byte_to_hexa(byte[] b)
        {
            if (b==null)
                return "";
            if (b.Length==0)
                return "";
            return System.BitConverter.ToString(b);
        }
        public static string string_to_hexa(string str_data)
        {
            return byte_to_hexa(System.Text.Encoding.Default.GetBytes(str_data));
        }
        public static string string_to_hexa(string str_data,string separator)
        {
            return byte_to_hexa(System.Text.Encoding.Default.GetBytes(str_data),separator);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
France France

Comments and Discussions