Yearly Archives: 2017


Converting a (void*) buffer to a std::vector

On a project we were recently working on, some legacy C code was producing a (void*) voidBuffer accompanied by its size.
The rest of the project was in C++ and we needed to convert the (void*) voidBuffer to a std::vector<unsigned char> vector.

To do so, we used the following code:


//First cast the (void *) voidBuffer to an (unsigned char *) to implicitly get the element size (1 Byte each)
const unsigned char *charBuffer = (unsigned char *) voidBuffer;
//Then we create the vector (named vectorBuffer) by copying the contents of charBuffer to the vector
std::vector<unsigned char> vectorBuffer(charBuffer, charBuffer + length);

[download id=”4084″]


Remove all non digit characters from String

The following Java snippet removes all non-digit characters from a String.
Non-digit characters are any characters that are not in the following set [0, 1, 2, 3, 4 ,5 ,6 ,7 ,8, 9].


myString.replaceAll("\\D", "");

For a summary of regular-expression constructs and information on the character classes supported by Java pattern visit the following link.

The \\D pattern that we used in our code is a predefined character class for non-digit characters. It is equivalent to [^0-9]that negates the predefined character class for digit characters [0-9].