将字符串中的特定单词转换为字节数组 [英] convert particular word in a string to byte array

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

问题描述

  string  data =   Hello 0X0A World,你是怎么做的0x0A?; 
byte [] value = null ;



i希望找到0x0A,除了0x0A,其他字应该转换为byte [],任何人都可以帮我。

i尝试

  if (dats.IndexOf(  0x0A,StringComparison.OrdinalIgnoreCase)>  =  0 

{
value = Encoding.Default.GetBytes(data);
// 此处它转换所有字符串
}



我在期待Hello World,你好吗?应该转换为bytearray []并且在0x0A之间不应该转换保持相同..请帮助我

解决方案

  string  data =   Hello 0x0A World,你是怎么做的0x0A?; 
byte [] value = null ;
data = data.Replace( 0x0A );
value = Encoding.Default.GetBytes(data);
// 如果您需要忽略大小写
// value = Regex.Replace(data,0x0A,,RegexOptions.IgnoreCase);
string newstring = Encoding.Default.GetString( value ); // Hello World,你好吗?



更新:

  byte  [] GetByteArray( string 数据)
{

var items = Regex.Split(data, 0x0A)。选择(w => Encoding.Default.GetBytes(w));
列表< byte> final = new List< byte>();
foreach var item in 项目)
{
if (final.Any()){
final.Add(0x0A);
}
final.AddRange(item);
}
return final.ToArray();
}





打电话为



  string  data =   Hello 0x0A World,怎么是0x0A?; 
byte [] result = GetByteArray(data);



希望这可以帮助你


string data="Hello 0X0A World , How 0x0A are you?";
byte[] value=null;


i want to find 0x0A , except 0x0A , other words should be convert as byte[], can any one help me please.
i tried

if(dats.IndexOf("0x0A", StringComparison.OrdinalIgnoreCase) >= 0)

{
value=Encoding.Default.GetBytes(data);
 //here it converts all the string
}


am expecting Hello World ,How are you ? should be converted into bytearray[] and in between 0x0A should not convert stay same .. Please help me

解决方案

string data="Hello 0x0A World , How 0x0A are you?";
byte[] value=null;
data=data.Replace("0x0A","");
value=Encoding.Default.GetBytes(data);
//if you need ignore case
//value =Regex.Replace(data, "0x0A", "", RegexOptions.IgnoreCase);
string newstring =Encoding.Default.GetString(value); //Hello  World , How  are you?


Update:

byte[] GetByteArray(string data)
{

    var items = Regex.Split(data, "0x0A").Select(w=>Encoding.Default.GetBytes(w));
    List<byte> final = new List<byte>();
    foreach(var item in items)
    {
       if(final.Any()){
           final.Add( 0x0A );
          }
        final.AddRange(item);
    }
    return final.ToArray();
}



call as

string data="Hello 0x0A World , How 0x0A are you?";
byte[] result = GetByteArray(data);


Hope this helps you


这篇关于将字符串中的特定单词转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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