E
E
Evgeny Petryaev2020-08-26 21:41:20
C++ / C#
Evgeny Petryaev, 2020-08-26 21:41:20

How to pass unsigned char* bytes in signed char* array?

1. Briefly read uchar[] into char[]
2. Pass by send(char[]) method

FILE* sFile = 0;        // Дескриптор файла
    unsigned char tgaHeader[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    unsigned char header[6];
    unsigned char tempColors = 0;
    fopen_s(&sFile,"Screen.tga", "wb");
    if (!sFile) {
    }
    header[0] = ScreenWidth % 256;
    header[1] = ScreenWidth / 256;
    header[2] = ScreenHeight % 256;
    header[3] = ScreenHeight / 256;
    header[4] = BMI.bmiHeader.biBitCount;
    header[5] = 0;
    fwrite(tgaHeader, sizeof(tgaHeader), 1, sFile);
    n = send(newsockfd[current], (char*)tgaHeader, sizeof(tgaHeader), 0);
    fwrite(header, sizeof(header), 1, sFile);
    n = send(newsockfd[current], (char*)header, sizeof(header), 0);
    fwrite(ImageBuffer, BMI.bmiHeader.biSizeImage, 1, sFile);
    char* s = new char[100];
    _ultoa_s(BMI.bmiHeader.biSizeImage+17, s,100,10);
    for (int i = 0; i < 100; i++)
        ((type*)args)[current].lalk->buffer[i] = s[i];
    n = send(newsockfd[current], ((type*)args)[current].lalk->buffer, buflen, 0);
    n = send(newsockfd[current], (char*)ImageBuffer, BMI.bmiHeader.biSizeImage, 0);
    fclose(sFile);

On the other side
1. I accept the recv(char[]) method into the char[]
variable 2. I write char[] as uchar[]
And it doesn't work (the data is distorted). How to fix it?
FILE* f;
        fopen_s(&f, "Screen.tga", "wb");
        if (!f) {
        }
        unsigned char tgaHeader[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        unsigned char header[6];
        n = recv(sockfd, (char*)tgaHeader, sizeof(tgaHeader), 0);
        fwrite((unsigned char*)tgaHeader, sizeof(tgaHeader), 1, f);
        n = recv(sockfd, (char*)header, sizeof(header), 0);
        fwrite((unsigned char*)header, sizeof(header), 1, f);
        // Записываем данные изображения:
        bzero(buffer, buflen);
        n = recv(sockfd, buffer, buflen, 0);
        unsigned long j = (unsigned long)atol(buffer);

        std::cout << buffer << std::endl;
        std::cout<<'\n' << "j=" << j << std::endl;
        bzero(buffer, buflen);
        n = recv(sockfd, buffer, j, 0);
        fwrite((unsigned char*)buffer,j, 1, f);
        bzero(buffer, buflen);
        fclose(f);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2020-08-26
@jcmvbkbc

does not work (data is distorted). How to fix it?

It is necessary to understand exactly how and what kind of data is distorted.
I suspect it is not being sent or received correctly.
What is "I write char[] as uchar[]" - it's not clear at all, show the code.

W
WinPooh32, 2020-08-27
@WinPooh32

1.Shortly read uchar[] to char[]

You can't do that, no wonder the data is skewed.
Although both uchar and char occupy the same number of bytes (1 pc in this case), but char has 1 bit under the sign, considering uchar as a regular char, you will get garbage if the char values ​​go beyond the bounds, i.e. numbers greater than 127 will be invalid.
How to fix it?

Use only uchar or char everywhere.
In general, with all your machinations, everything should be correct, the cant is somewhere in these places: "I read briefly ...", "I send by the send(char[]) method", "I accept by the method", "I write char[] as uchar []"
Vanga is on vacation and has nothing to say without a code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question