当前位置: 移动技术网 > IT编程>开发语言>c# > C#中Byte转换相关的函数

C#中Byte转换相关的函数

2019年07月18日  | 移动技术网IT编程  | 我要评论

 1、将一个对象转换为byte对象

public static byte getbyte(object o)
{
byte retint = 0;
if (o != null)
{
byte tmp;
if (byte.tryparse(o.tostring().trim(), out tmp))
{
retint = tmp;
}
}
return retint;
} 

2、将一个十六进制字符串转换为byte对象,字符串以0x开头

public static byte getbyteformhex(string hexvalue)
{
try
{
return convert.tobyte(hexvalue, 16);
}
catch 
{
return 0;
}
}

3、将单个字符转换为byte对象

public static byte getbyteformsinglestring(string value)
{
return getbyteformchar(convert.tochar(value));
}

4、将一个字符串转换为byte数组

public static byte[] getbytes(string values)
{
return system.text.encoding.default.getbytes(values);
}

以上内容是小编给大家介绍的c#中byte转换相关的函数,希望对大家有所帮助!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网