-3

strtol is very slow for my program which I want it to be very fast

so for example I have this string:

PCHAR ex = "\x55\xEC" etc...

They're hex, so If I want to write it without the "\x" to be like "55 EC" etc.. I have to use strtol, but strtol is EXTREMELY slow for my program I want the fastest one on my program..

I use strtol like (CHAR)strtol(ex, &ex, 16);

Mind helping me please?

Frought
  • 116
  • 9
  • 1
    What are you trying to do? Display your array without the 0x prefix? – Andreas DM Nov 05 '14 at 07:23
  • Does that compile for you? Passing `&ex` as the second parameter makes no sense when `ex` is an array. – Blastfurnace Nov 05 '14 at 07:24
  • Oh sorry It is `PCHAR ex = "\x55\xEC";` So what I want to do is: type the hex char without the \x so it would be: `PCHAR ex = "55 EC"` and after that using the alt of strtol changes it to `\x55\xEC` – Frought Nov 05 '14 at 09:45
  • If I now understand your question, I guess you could speed up your program by first converting the text pattern to binary and use the binary for pattern matching as you did before. – stefaanv Nov 05 '14 at 10:02
  • so i would convert the "55 EC" to binary and continue? but wouldn't it be still the same as it will convert the "55 EC" to binary and not convert the "55 EC" to "\x55\xEC"? – Frought Nov 05 '14 at 10:23
  • "\x55\xEC" is the binary representation of "55 EC", that's why I wrote "use the binary for pattern matching as you did before" – stefaanv Nov 05 '14 at 11:00
  • So, I still don't know how to convert it to binary xD – Frought Nov 05 '14 at 11:01
  • Just pass it to strtol, but do it only once and not each time while parsing your data `for (int i = 0; i < 4; ++i) { bin_p[i] = std::strtol(p, &p, 16); }` or using `std::istringstream` for a more c++ like solution. – stefaanv Nov 05 '14 at 12:03
  • I already said strtol is really slow, that's why I want to stop using it – Frought Nov 05 '14 at 12:50
  • strtol is not that slow. The program that you showed in the link is slow because you convert it over and over again. converting the pattern of around 20 hex values is too fast to notice if you only convert it once and use the result of that for pattern matching. Try timing a single conversion of a pattern. – stefaanv Nov 05 '14 at 12:58
  • check this new discussion http://stackoverflow.com/questions/26757747/findsignature-function I made my own implented strtol which is getByte but still having some issues you may help me on – Frought Nov 05 '14 at 13:00

2 Answers2

1

I would do something like this:

unsigned long ex[] {0x55, 0xEC};

  for (auto i : ex)
    std::cout << std::hex << i << " ";

Output is: 55 EC

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • You got me wrong, I want the input like "55 EC" and convert it to 0x55 and EC to 0xEC strtol is doing it correctly, but its really slow taking like 6 seconds or 5 to get it, while I want it to be really fast – Frought Nov 05 '14 at 09:10
  • 2
    @Frought: no, you are wrong: the input in your question is "\x55\xEC", not "55 EC". These strings are completely different and actually is a different question. `char ex[] {0x55, 0xEC, 0};` is the same as `char ex[] = "\x55\xEC";` – stefaanv Nov 05 '14 at 09:14
  • 1
    About strtol taking 5 to 6 seconds, you could provide us with a working example that shows this. That would help to spot the problem. – stefaanv Nov 05 '14 at 09:17
  • I'm doing that inside my pattern finder function aka signature finder: http://privatepaste.com/f19f997e61/5000 pasetkey is 5000 CC is a wildcard so the usage would be: `FindSignature("55 EC CC A1", baseAddress, size, mLength);` rather than: `FindSignature("\x55\xEC\xCC\A1", baseAddress, size, mLength);` Benchmarked my pattern and it took like 6 ~ 5 seconds with strtol, while without strtol it (using \x55etc..) took around 200ms – Frought Nov 05 '14 at 09:36
  • So you were not actually wrong, but very unclear. I guess most of us didn't understand that you want to change the input form "\x55..." to "55..." which doesn't help getting an answer. – stefaanv Nov 05 '14 at 09:52
  • Yeah, you're right. But I'm still finding anything faster or even some idea – Frought Nov 05 '14 at 10:07
0

What are you trying? strtol converts a byte string to a long integer; when the string has 0x as prefix it treats the string as hexadecimal number. It won't convert \x55 into 55. Three errors in assumption:

  • It will convert 0x55 into 55. No, it'll convert it into 85 (decimal)
  • It will convert multiple numbers in a string. No, it'll convert just one number in a string to a long or long long)
  • It will take a hex number of the format \x. No, this should be 0x for it to recognize it as a hex value

What you have is not a use case of strtol at all. You'll have to manually do the conversion.

All you need is to just a find-replace of all \x with space and trim the resulting string to shave off the leading space.

legends2k
  • 31,634
  • 25
  • 118
  • 222