如何在C#中将字符串转换为字节? [英] How to convert the string to byte in c# ?

查看:97
本文介绍了如何在C#中将字符串转换为字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,C#专家,

我正在尝试使用此代码将字符串转换为字节,但这给我提供错误提示.

i am trying to convert string to byte using this code, but it gives error pls help me..

public 静态 byte [] StringToByte( string InString ){

public static byte[] StringToByte(string InString) {

字符串 [] ByteStrings;

string[] ByteStrings;

ByteStrings = InString.Split(" .ToCharArray());

ByteStrings = InString.Split(" ".ToCharArray());

byte [] ByteOut;

byte[] ByteOut;

ByteOut = byte [ByteStrings.Length-1];

ByteOut = new byte[ByteStrings.Length-1];

( int i = 0; i == ByteStrings.Length-1; i ++){

for (int i = 0;i==ByteStrings.Length-1;i++) {

ByteOut = Convert.ToByte(("0x" + ByteStrings ));

ByteOut = Convert.ToByte(("0x" + ByteStrings));

}

返回 ByteOut;

return ByteOut;

}

推荐答案

将字符串转换为字节数组的最简单方法是使用GetBytes()实例化的文本编码类的方法,请考虑以下示例:

The easiest way to convert a string into a byte array is to use the GetBytes() method of an instantiated text encoding class, consider this example:

   System.Text.ASCIIEncoding  encoding = new System.Text.ASCIIEncoding();
    Byte []个字节= Encoding.GetBytes(yourString);

    System.Text.ASCIIEncoding  encoding=new System.Text.ASCIIEncoding();
    Byte[] bytes = Encoding.GetBytes(yourString);


这篇关于如何在C#中将字符串转换为字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆