memstr
-
Hi, I have a binary string. I need to search certain characters. I used to use "memstr" (obtain fist substring from string) on Unix. Is there anything similar to that available from MFC? Thanks, Kevin
-
Hi, I have a binary string. I need to search certain characters. I used to use "memstr" (obtain fist substring from string) on Unix. Is there anything similar to that available from MFC? Thanks, Kevin
-
Hi, I have a binary string. I need to search certain characters. I used to use "memstr" (obtain fist substring from string) on Unix. Is there anything similar to that available from MFC? Thanks, Kevin
-
Hi, I have a binary string. I need to search certain characters. I used to use "memstr" (obtain fist substring from string) on Unix. Is there anything similar to that available from MFC? Thanks, Kevin
Use the CString to store the binary data & use Find (..) member fuction to find the charaters. :cool: Vikas Amin Embin Technology Bombay vikas.amin@embin.com
-
Hi, I have a binary string. I need to search certain characters. I used to use "memstr" (obtain fist substring from string) on Unix. Is there anything similar to that available from MFC? Thanks, Kevin
There is nothing exactly like
memstr(...)
in MFC or in the MS VC++ RTL. The closest things arestrstr(...)
and and functions likeCString::Find(...)
. However, these might not work as you expect, because you said "binary string", and that usually means thatNUL
characters can be expected in other locations than the end of the string. Other than taking an existing implementation and copying the source or writing your own implementation, a Q&D way would be to store the locations of all of the non-terminatingNUL
characters and convert them to some other character and then use a function likestrstr(...)
(with the strings modified as specified above). After locating your substrings, you can convert them back by replacing theNUL
s you replaced before. Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
Hi, I have a binary string. I need to search certain characters. I used to use "memstr" (obtain fist substring from string) on Unix. Is there anything similar to that available from MFC? Thanks, Kevin
As was mentioned before,
strstr
is the closest thing, but it is limited in that NULL characters terminate the buffers. So I have used this code:#include <string.h>
char * _memmem(const char *mem, size_t mem_size, const char *sub, size_t sub_size)
{
// returns a pointer of the first occurance of a sub-buffer in a memory buffer
// returns NULL if the sub-buffer was not found
// is not limited by embedded NULLs in either the memory buffer or the sub-buffer
//
// Parameters:
// mem - The memory buffer to scan
// mem_size - The size, in bytes, of the memory buffer
// sub - The sub buffer to find
// sub_size - The size, in bytes, of the sub bufferchar \*ret = NULL; char \*ptr = const\_cast<char \*>(mem); while (ptr && !ret) { ptr = reinterpret\_cast<char \*>(memchr(ptr, \*sub, mem\_size - (sub\_size - 1) - (int)(ptr - mem))); if (ptr) { if (!memcmp(ptr, sub, sub\_size)) ret = ptr; ++ptr; } } return ret;
}
[edit] cleaned up the code a little [/edit]
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it! -- modified at 18:12 Monday 21st November, 2005