Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0 Network
hi
i want to get specific sub string from the string .. Explanation
if i have
string hex= " 7E 00 0F 10 01 00 13 A2 00 40 81 AB C4 FF FE 00 00 01 0B " ;
 
01 at end of string only from the string But ! i dont wanna make it with the normal substring function because in my case 01 may variate it can be 01 c5 or anything .. number of bites is not constant
in other way we can express it like
" 7E 00 0F 10 01 00 13 A2 00 40 81 AB C4 FF FE 00 00 (N) 0B "
While N is variable length ..but other packet parts is const length .
btw This is apacket and 01 or N is RF Data
Thanks
Posted 8-Jan-13 23:48pm
Edited 8-Jan-13 23:58pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use a Regex:
string hex1 = " 7E 00 0F 10 01 00 13 A2 00 40 81 AB C4 FF FE 00 00 01 0B ";
string hex2 = " 7E 00 0F 10 01 00 13 A2 00 40 81 AB C4 FF FE 00 00 01 FF FE 66 0B ";
Regex reg = new Regex(@"(?<=7E 00 0F 10 01 00 13 A2 00 40 81 AB C4 FF FE 00 00 )(.. )*(?=0B )");
Match m = reg.Match(hex1);
if (m.Success)
    {
    Console.WriteLine(m.Value);
    }
m = reg.Match(hex2);
if (m.Success)
    {
    Console.WriteLine(m.Value);
    }
Will produce:
01 
01 FF FE 66 
  Permalink  
Comments
promarkoo at 9-Jan-13 7:21am
   
data inside the string is not constant ... it is generated each calculation process . i was thinking if i can make like the first part of regex variable
like x=substring(0,20);
OriginalGriff at 9-Jan-13 7:36am
   
You can. Assuming your data starts with a 7E, you could discard the next 6 data pairs like this:
(?<=7E (.. ){6})(.. )*(?=0B )
And it would start capture with the A2 in your example.

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



Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100