Read data from unsigned charater * in c++

MNH ASP 101 Reputation points
2020-10-15T04:44:53.177+00:00

Hello Guys

How Can I read data from memory buffer ??

Example

unsigned char* totalResponse = NULL;

unsigned long totalResponseSize = 0;

const unsigned long RECVRESPONSE_TEMPBUFFERSIZE = 10 * 1024;
unsigned long readBufferSize = RECVRESPONSE_TEMPBUFFERSIZE;
// Temporary buffer that stores response comes from the printer
unsigned char readBuffer[RECVRESPONSE_TEMPBUFFERSIZE] = { 0 };

do {

        // sleep to get the printer ready to respond
       // Receive reponse from the printer
        readBufferSize = RECVRESPONSE_TEMPBUFFERSIZE;
        readResult = pu3Read(hSDK, readBuffer, &readBufferSize, &needContinue);
        l = readResult;
        if (CNMPU3_ERR_SUCCESS == readResult) {
            unsigned char* tmp = NULL;
            tmp = totalResponse;
            totalResponse = (unsigned char*)malloc(sizeof(char) * (readBufferSize + totalResponseSize));

            if (NULL == totalResponse)
                break;
            if (NULL != tmp)
            {
                memcpy_s(totalResponse, readBufferSize + totalResponseSize, tmp, totalResponseSize);
                free(tmp);
            }
            // Store
            memcpy_s(totalResponse + totalResponseSize, readBufferSize, readBuffer, readBufferSize);
            totalResponseSize += readBufferSize;
        }
        else if (CNMPU3_ERR_NOCONTENT == readResult) {
            Sleep(1000); // sleep to get the printer ready to respond
            continue;
        }
        else {
            break;
        }

    } while (needContinue == CNMPU3_TRUE);

so How to get a value from *totalresponse ??

Pls Sugest me

Thanks for Help !!!

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,856 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 119.9K Reputation points
    2020-10-15T05:19:49.453+00:00

    Using expressions like totalResponse[0], totalResponse[1], etc., or totalResponse[i] inside a loop you can get individual bytes.

    If the buffer contains a string, then std::string s((char * )totalResponse, totalResponseSize) will make the s variable containing the contents.

    It depends on what kind of data you want to read.

    (By the way, using realloc will simplify you code, excluding one or both of memcpy_s).

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.