Find Values between two prefixes and swap one for the other

Asked

Viewed 125 times

0

I’m developing an application where you have to find a definite value and literally walk 19 houses.

130000007363645C7363645F
6178747265653030311300000073
6363645F6178747265653030315F

  • Search for value from: 13000000
  • Floor of: 13000000 until: 7363645C736363645F6178746565303031 (Or until you find another value 13)

Thus remaining:

130000007363645C7363645F
6178747265303031
1300000073
6363645F6178747265653030315F

  • Look for another value: 13000000

Thus remaining:

13000000736363645C736363645F
61787472653030311300000073
6363645F6178747265653030315F

  • Walk of new value : 13000000 up to the other value: 736363645F6178747265653030315F

Thus remaining:

13000000736363645C736363645F
6178747265653030311300000073
6363645F6178747265653030315F

Then exchange one value for another: 7363645C7363645F
617874726530303
for 736363645F6178747265653030315F

Making a loop repeating the same function throughout the file.

=======================@Edit

Thus:

13-00-00-00-73-63-63-64-5C-73-63-63-64-5F-61-78-74-72-65-65-30-30-31-13-00-00-00-73-63-63-64-5F-61-78-74-72-65-65-30-30-31-5F

Thus remaining:

13-00-00-00-73-63-63-64-5F-61-78-74-72-65-65-30-30-31-5F -13-00-00-00-73-63-63-64-5C-73-63-63-64-5F-61-78-74-72-65-65-30-30-31

NOTE: I changed the top value after the "13-00-00-00" down

============================@ I tried something similar to that:

byte[] file = File.ReadAllBytes(file);

string hexStrings = BitConverter.ToString(file);

hexStrings = hexStrings.Replace("-", "");

foreach (byte hexString in hexStrings)
{
}
  • Quite confused your question friend, I didn’t really understand what you need... you need to search a string in another?

  • I have to "walk 19 houses" from a string that is the "13-00-00-00" thus "73-63-63-64-5C-73-63-64-5F- 61-78-74-72-65-65-30-30-31"

  • @Emanuellucas no use repeating what he has already written, need to explain better, in other words, providing more details.

  • This value among the "13000000", what you want to do with it?

  • Do you have a line or not? You need to know what the pattern is. You have to find 13 or 13000000?

  • @Ciganomorrisonmendez, the values "13000000" I only want to use as a search base

  • @bigown have found the "13000000" as a search basis and so jump "19 houses"

  • Yes, but do you want to count how many "13000000" did you think? Or do you want to pick up the values between one "13000000" and another? What is the purpose of the exercise?

  • @Ciganomorrisonmendez I want to take the values between a "13000000" and another

  • 1

    This question is completely meaningless. The conversion to HEX is totally unnecessary. to extract the sccd\sccd_axtree001 etc the operation should be done directly in the original bytes. When so, explain the real problem, and not the way you are trying to do it, because you run the risk of getting answers that already start from wrong reasoning. Suggested reading for better use of the site: What is the XY Problem.

Show 5 more comments

2 answers

2


A simple way is by regular expression, using Regex:

using System.Text.RegularExpressions;

var padrao = @"13-00-00-00-(([0-9a-fA-F]{2}-){19})";
var re = new Regex(padrao, RegexOptions.IgnoreCase);
var matches = re.Matches(stringComHexadecimais);
foreach (Match match in matches)
     Console.WriteLine("   " + match.Value);

In this case, I made for you a regular expression with two groups: an internal one (to take the 19 values individually) and an external one, to take all the 19 values at once.

I made a Fiddle.

Notice that in Fiddle, the Group[1] is exactly what you’re looking for.


About reversing the String the simple way is by implementing this function (best response from here):

public static string Inverter(string s)
{
    char[] charArray = s.ToCharArray();
    Array.Reverse(charArray);
    return new string(charArray);
}

Therefore:

var grupo = match.Groups[1].ToString();
var stringInversa = Inverter(grupo);

Now I understand what you want. You want to locate the groups two by two, and then reverse them.

This changes the regular expression, because you need to necessarily find two groups:

var padrao = @"(13-00-00-00-(([0-9a-fA-F]{2}-){19})){2}";

Locate the larger group (matches[0].Groups[0]) and do another regular expression search on it. You will get two groups.

Just invert them and assemble another string.

  • Thank you very much. But you did not remove the rest of my doubt.

  • And what’s the rest of your doubt?

  • Find the other value "13-00-00-00" (quoted in topic) and find 736363645F6178747265653030315F and invert the two values between "13-00-00-00" and the other "13-00-00-00".

  • Thanks again. More right would be to reverse the position not the characters. Example thus: 13000000736363645C736363645F 6178747265653030311300000073 6363645F6178747265653030315F , thus: 13000000736363645C736363645F61787472656530303113000000736363645C736363645F617874726565303031 @I can’t improve the explanation here I put in the topic

  • 1

    Thank you you solved my doubt. Thanks again for the effort vlw <3

2

I tried to do something that seems to be what you want. You may have problems because the problem is not well defined. If you improve the definition, I can change it a little bit and make a more organized final version. I made a version with the strokes original of the question and without the traces as it was after editing.

var texto = @"13-00-00-00-73-63-63-64-5C-73-63-63-64-5F-61-78-74-72-65-65-30-30-31-13-00-00-00-73-63-63-64-5F-61-78-74-72-65-65-30-30-31-5F-";
var divisoes = texto.Split(new[] { "13-00-00-00" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var divisao in divisoes) {
    foreach (var casa in divisao.Split(new[] {'-'})) WriteLine(casa);
    WriteLine("--");
}
WriteLine("Sem traços");
texto = @"13000000736363645C736363645F61787472656530303113000000736363645F6178747265653030315F";
divisoes = texto.Split(new[] { "13000000" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var divisao in divisoes) {
    for (var i = 0; i <= Min(divisao.Length - 1, 37); i+=2) WriteLine(divisao.Substring(i, 2));
    WriteLine("--");
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Thank you for your reply. But it wasn’t what I was looking for

  • When you explain what you want, I improve. It was right to have closed the question, but I’ve been trying to save what is bad.

  • Sorry I didn’t mean to offend you, don’t take it personally, but I’m a beginner :( is similar to @Cigano Morrison Mendez

  • You didn’t offend me, but I can’t understand what you want. You said the two are wrong. So I voted to close the question as unclear, if you clarify, it can be reopened.

  • I tried to be as clear as possible, but you didn’t understand what I tried to explain. @Cigano was clearing my doubts

Browser other questions tagged

You are not signed in. Login or sign up in order to post.