当前位置:首页 > 通信资讯 > 正文

C#int转byte(c#字符转byte)

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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 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开头

?
1 2 3 4 5 6 7 8 9 10 11 public static byte GetByteFormHex(string hexValue) { try { return Convert.ToByte(hexValue, 16); } catch { return 0; } }

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

?
1 2 3 4 public static byte GetByteFormSingleString(string value) { return GetByteFormChar(Convert.ToChar(value)); }

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

?
1 2 3 4 public static byte[] GetBytes(string values) { return System.Text.Encoding.Default.GetBytes(values); }

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

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。