十六进制字符串转换成byte数组_物联网学习教程——增强字符串

之前有朋友问过小编一个问题,你对增强字符串这块了解吗?其实小编很多时候也只是稍微懂一点,就先不和大家分享了,如果有想了解的话,可以看看这个,是之前我收藏的!

一个自定义类,用于大规模的字符串连接,如拼接SQL语句。用流技术实现的,很好哦!!

c8af01c02cf5ffdcd1bfc741c6e977a0.png

using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace SuperString
{
///
/// 创 建 者: superhood
/// 内容描述: 增强字符串类
///
public class CSuperString
{
///
/// 功能简述: 运算符重载
///
/// 左参数
/// 右参数
/// 文本类
public static CSuperString operator + (string Left,CSuperString Right)
{
byte[] btyLeft = Encoding.Default.GetBytes(Left);//返回左参数数组
byte[] btyRight = new Byte[Right.iLength];//返回右参数数组
Right.iTextLength += Left.Length;//设置右参数文本长度
Right.iLength = btyLeft.Length + btyRight.Length;//设置右参数字节长度
Right.memStrm.Position = 0;//将右参数流位置置0
Right.memStrm.Read(btyRight,0,btyRight.Length);//将右参数数据读出

Right.memStrm.Position = 0;//将右参数流位置置0
Right.memStrm.Write(btyLeft,0,btyLeft.Length);//将字符串(字节数组)写入右参数
Right.memStrm.Write(btyRight,0,btyRight.Length);//将右参数原有信息写回(加在左参数字符串后)

return Right;//返回右参数
}

///
/// 功能简述: 运算符重载
///
/// 左参数
/// 右参数
/// 文本类
public static CSuperString operator + (CSuperString Left,string Right)
{
byte[] btyRight = Encoding.Default.GetBytes(Right);//将右参数(字符串)转换为字节数组
Left.memStrm.Position = Left.iLength;//设置左参数流的位置
Left.memStrm.Write(btyRight,0,btyRight.Length);//将右参数字符串写入流

Left.iTextLength += Right.Length;//设置左参数文本长度
Left.iLength += btyRight.Length;//设置左参数字节长度

return Left;//返回左参数
}
///
/// 功能简述: 运算符重载
///
/// 左参数
/// 右参数
/// 文本类
public static CSuperString operator + (CSuperString Left,CSuperString Right)
{
byte[] btyRight = new Byte[Right.iLength];//声明字节数组(右参数)
Right.memStrm.Position = 0;//将右参数流位置置0

Right.memStrm.Read(btyRight,0,btyRight.Length);//将右参数(字符串)转换为字节数组
Left.memStrm.Position = 0;//将左参数流位置置0
Left.memStrm.Write(btyRight,0,btyRight.Length);//将右参数字符串写入流
Left.iTextLength += Right.iTextLength;//设置左参数文本长度
Left.iLength += Right.iLength;//设置左参数字节长度

return Left;//返回左参数
}

///
/// 功能简述: 流中有效字节长度
///
private int iLength = 0;
///
/// 功能简述: 流中文本长度
///
private int iTextLength = 0;

///
/// 功能简述: 内存流
///
private MemoryStream memStrm;

///
/// 功能简述: 构造函数
///
public CSuperString()
{
memStrm = new MemoryStream();//初始化流
}

///
/// 功能简述: 构造函数
///
/// 默认长度(以字节为单位)
public CSuperString(int DefaultLength)
{
memStrm = new MemoryStream(DefaultLength);//初始化流
}

///
/// 功能简述: 属性,字节长度
///
public int Length
{
get
{
return iLength;
}
}

///
/// 功能简述: 属性,文本长度
///
public int TextLength
{
get
{
return iTextLength;
}
}

///
/// 功能简述: 属性,流长度
///
public int Capacity
{
get
{
return memStrm.Capacity;
}
set
{
if (value >= iLength)
memStrm.Capacity = value;
else
memStrm.Capacity = iLength;
}
}

///
/// 功能简述: 向类中添加字符串
///
///
public void AddString (string Date)
{
byte[] btyDate = Encoding.Default.GetBytes(Date);//字符串转换为字节数组
memStrm.Position = iLength;//设置流的位置
memStrm.Write(btyDate,0,btyDate.Length);//将字符串写入流

iTextLength += Date.Length;//设置文本长度

iLength += btyDate.Length;//设置字节长度
}

///
/// 功能简述: 返回文本
///
/// 返回字符串
public override string ToString()
{
memStrm.Position = 0;//设置流的位置
byte[] btyDate = new byte[iLength];//声明字节数组
memStrm.Read(btyDate,0,iLength);//将流内容读入数组
return Encoding.Default.GetString(btyDate);//将字节数组转换为字符串并返回
}

///
/// 功能简述: 将字符串写入文件
///
/// 文件名
public void WriteToFile(string FileName)
{
FileStream strm = new FileStream(FileName,FileMode.OpenOrCreate,FileAccess.Write);//初始化文件流
//判断流长度用来确定流中是否有冗余信息
if (memStrm.Length > iLength)
{//有
memStrm.Position = 0;//设置流的位置
byte[] btyDate = new byte[iLength];//声明字节数组
memStrm.Read(btyDate,0,iLength);//将流内容读入数组
strm.Write(btyDate,0,iLength);//将流内容写入文件
}
else
{//没有
memStrm.WriteTo(strm);//将流中文本写入文件
}

strm.Close();//关闭文件
}

///
/// 功能简述: 将字符串写入流
///
/// 流
public void WriteToStream(Stream strm)
{
//判断流长度用来确定流中是否有冗余信息
if (memStrm.Length > iLength)
{//有
memStrm.Position = 0;//设置流的位置
byte[] btyDate = new byte[iLength];//声明字节数组
memStrm.Read(btyDate,0,iLength);//将流内容读入数组
strm.Write(btyDate,0,iLength);//将数组内容写入另一流
}
else
{//没有
memStrm.WriteTo(strm);//将流中文本写入另一流
}
}

///
/// 功能简述: 清除流
///
public void Clear()
{
iLength = 0;//将流字节长度设为0
iTextLength = 0;//将流文本长度设为0
}
}
}

字符串转换

1如何将字串 String 转换成整数 int?

A. 有两个方法:

1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

注: 字串转成 Double, Float, Long 的方法大同小异.


2 如何将整数 int 转换成字串 String ?


A. 有三种方法:

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i);

3.) String s = "" + i;

注: Double, Float, Long 转成字串的方法大同小异.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值