• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#转换函数

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32; //对注册表操作
using System.Collections; //使用Arraylist
using System.Security.Cryptography;//加密解密
using System.IO; //文件操作
using System.Runtime.InteropServices;//调用DLL DllImport
using System.Management; //获取硬件信息
using System.Net; //获取IP地址是用到
using System.Drawing; //image
using System.Net.NetworkInformation; //ping 用到
using System.Text.RegularExpressions; //正则
using System.Data;
using System.Data.SqlClient;
using Microsoft.VisualBasic; //简体转繁体时用到
using System.Web; //html UrlEncode


//注册表操作
public class GF_RegReadWrite
{

/// <summary>
/// 读取路径为keypath,键名为keyname的注册表键值,缺省返回def
/// </summary>
/// <param name="rootkey"></param>
/// <param name="keypath">路径</param>
/// <param name="keyname">键名</param>
/// <param name="rtn">默认为null</param>
/// <returns></returns>
static public bool GetRegVal(RegistryKey rootkey, string keypath, string keyname, out string rtn)
{
rtn
= "";
try
{
RegistryKey key
= rootkey.OpenSubKey(keypath);
rtn
= key.GetValue(keyname).ToString();
key.Close();
return true;
}
catch
{
return false;
}
}

/// <summary>
/// 设置路径为keypath,键名为keyname的注册表键值为keyval
/// </summary>
/// <param name="rootkey"></param>
/// <param name="keypath"></param>
/// <param name="keyname"></param>
/// <param name="keyval"></param>
/// <returns></returns>
static public bool SetRegVal(RegistryKey rootkey, string keypath, string keyname, string keyval)
{
try
{
RegistryKey key
= rootkey.OpenSubKey(keypath, true);
if (key == null)
key
= rootkey.CreateSubKey(keypath);
key.SetValue(keyname, (
object)keyval);
key.Close();
return true;
}
catch
{
return false;
}
}

/// 创建路径为keypath的键
private RegistryKey CreateRegKey(RegistryKey rootkey, string keypath)
{
try
{
return rootkey.CreateSubKey(keypath);
}
catch
{
return null;
}
}
/// 删除路径为keypath的子项
private bool DelRegSubKey(RegistryKey rootkey, string keypath)
{
try
{
rootkey.DeleteSubKey(keypath);
return true;
}
catch
{
return false;
}
}
/// 删除路径为keypath的子项及其附属子项
private bool DelRegSubKeyTree(RegistryKey rootkey, string keypath)
{
try
{
rootkey.DeleteSubKeyTree(keypath);
return true;
}
catch
{
return false;
}
}
/// 删除路径为keypath下键名为keyname的键值
private bool DelRegKeyVal(RegistryKey rootkey, string keypath, string keyname)
{
try
{
RegistryKey key
= rootkey.OpenSubKey(keypath, true);
key.DeleteValue(keyname);
return true;
}
catch
{
return false;
}
}
}
//类型转换
public class GF_Convert
{
/// <summary>
/// 字符串 转换 char数组
/// </summary>
/// <param name="in_str"></param>
/// <param name="in_len"></param>
/// <returns></returns>
public static char[] string2chararray(string in_str, int in_len)
{
char[] ch = new char[in_len];
in_str.ToCharArray().CopyTo(ch,
0);
return ch;
}

/// <summary>
/// char数组 转换 字符串
/// </summary>
/// <param name="in_str"></param>
/// <returns></returns>
public static string chararray2string(char[] in_str)
{
string out_str;
out_str
= new string(in_str);
int i = out_str.IndexOf('\0', 0);
if (i == -1)
i
= 16;
return out_str.Substring(0, i);
}

/// <summary>
/// byte数组 转换 字符串
/// </summary>
/// <param name="in_str"></param>
/// <returns></returns>
public static string bytearray2string(byte[] in_str)
{
string out_str;
out_str
= System.Text.Encoding.Default.GetString(in_str);
return out_str.Substring(0, out_str.IndexOf('\0', 0));

}

/// <summary>
/// 字符串 转换 byte数组 注意转换出来会使原来的bytearray长度变短
/// </summary>
/// <param name="in_str"></param>
/// <returns></returns>
public static byte[] string2bytearray(string in_str)
{
return System.Text.Encoding.Default.GetBytes(in_str);
}

/// <summary>
/// 字符串 转换 byte数组 长度为传如的长度
/// </summary>
/// <param name="in_str">传入字符串</param>
/// <param name="iLen">目标字节数组长度</param>
/// <returns></returns>
public static byte[] string2bytearray(string in_str, int iLen)
{
byte[] bytes = new byte[iLen];
byte[] bsources=System.Text.Encoding.Default.GetBytes(in_str);
Array.Copy(bsources, bytes, bsources.Length);


return bytes;
}

/// <summary>
/// 将字符串编码为Base64字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Base64Encode(string str)
{
byte[] barray;
barray
= Encoding.Default.GetBytes(str);
return Convert.ToBase64String(barray);
}

/// <summary>
/// 将Base64字符串解码为普通字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Base64Decode(string str)
{
byte[] barray;
try
{
barray
= Convert.FromBase64String(str);
return Encoding.Default.GetString(barray);
}
catch
{
return str;
}
}

/// <summary>
/// 图片 转换 byte数组
/// </summary>
/// <param name="pic"></param>
/// <param name="fmt"></param>
/// <returns></returns>
public static byte[] image_Image2Byte(Image pic, System.Drawing.Imaging.ImageFormat fmt)
{
MemoryStream mem
= new MemoryStream();
pic.Save(mem, fmt);
mem.Flush();
return mem.ToArray();
}
/// <summary>
/// byte数组 转换 图片
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static Image image_Byte2Image(byte[] bytes)
{
MemoryStream mem
= new MemoryStream(bytes, true);
mem.Read(bytes,
0, bytes.Length);
mem.Flush();
Image aa
= Image.FromStream(mem);
return aa;
}

/// <summary>
/// ip 转换 长整形
/// </summary>
/// <param name="strIP"></param>
/// <returns></returns>
public static long IP2Long(string strIP)
{

long[] ip = new long[4];

string[] s = strIP.Split('.');
ip[
0] = long.Parse(s[0]);
ip[
1] = long.Parse(s[1]);
ip[
2] = long.Parse(s[2]);
ip[
3] = long.Parse(s[3]);

return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
}

/// <summary>
/// 长整形 转换 IP
/// </summary>
/// <param name="longIP"></param>
/// <returns></returns>
public static string Long2IP(long longIP)
{


StringBuilder sb
= new StringBuilder("");
sb.Append(longIP
>> 24);
sb.Append(
".");

//将高8位置0,然后右移16为


sb.Append((longIP
& 0x00FFFFFF) >> 16);
sb.Append(
".");


sb.Append((longIP
& 0x0000FFFF) >> 8);
sb.Append(
".");

sb.Append((longIP
& 0x000000FF));


return sb.ToString();
}

/// <summary>
/// 将8位日期型整型数据转换为日期字符串数据
/// </summary>
/// <param name="date">整型日期</param>
/// <param name="chnType">是否以中文年月日输出</param>
/// <returns></returns>
public static string FormatDate(int date, bool chnType)
{
string dateStr = date.ToString();

if (date <= 0 || dateStr.Length != 8)
return dateStr;

if (chnType)
return dateStr.Substring(0, 4) + "" + dateStr.Substring(4, 2) + "" + dateStr.Substring(6) + "";

return dateStr.Substring(0, 4) + "-" + dateStr.Substring(4, 2) + "-" + dateStr.Substring(6);
}


/// <summary>
/// string型转换为bool型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的bool类型结果</returns>
public static bool StrToBool(object expression, bool defValue)
{
if (expression != null)
return StrToBool(expression, defValue);

return defValue;
}

/// <summary>
/// string型转换为bool型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
/// <returns>转换后的bool类型结果</returns>
public static bool StrToBool(string expression, bool defValue)
{
if (expression != null)
{
if (string.Compare(expression, "true", true) == 0)
return true;
else if (string.Compare(expression, "false", true) == 0)
return false;
}
return defValue;
}
/// <summary>
/// 将对象转换为Int32类型
/// </summary>
/// <param name="strValue">要转换的字符串</param>
/// <param name="defValue">缺省值</param>
///

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#开发“因为某项目未能生成,所以无法发布”发布时间:2022-07-14
下一篇:
【C#】[网络]网页源码读取(WebClient+StreamReader)发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap