Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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
C#
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
Posted
Updated 10-Aug-15 21:24pm
v2
Comments
OriginalGriff 11-Aug-15 3:37am    
Are you sure you have read your homework correctly?
I'd guess that "0X0A" is actually a character value: '\0X0A' instead of a string...
GagKumar 11-Aug-15 4:00am    
yes ,its a Hex value which makes line feed, i want this byte array to be compiled in PLC and displays strings with line feed in the Visualization.

1 solution

C#
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:
C#
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

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

Hope this helps you
 
Share this answer
 
v3
Comments
GagKumar 11-Aug-15 4:00am    
Hi DamithSL,thanks for reply, am expected 0x0A as it is in between byte array,
below is what am expecting
72 101 108 108 111 32 0 X 0 A 32 87 111 114 108 100 44 32 72 111 119 32 0X0A 32 97 114 101 32 121 111 117 63
GagKumar 11-Aug-15 4:15am    
yes, i want this as it is because of some compile reasons, is it possible like this?
DamithSL 11-Aug-15 4:34am    
check my updated
GagKumar 11-Aug-15 7:42am    
hello, thanks for answer, almost near but this position Regex.Split(data, "0x0A").Select(w=>Encoding.Default.GetBytes(w));
'w' is taking whole string not splitting. 0X0A giving 48 88 48 65
GagKumar 14-Aug-15 11:42am    
small change i made Regex.Split(data, 0x0A").Select(w=>Encoding.Default.GetBytes(w)) , here i gave space (" 0X0A"), now its working, thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900