Is there an equivalent to C * (unsigned int *) (char) = 123 in Javascript?

I am dealing with some C source code that I am trying to convert to Javascript, I ended up in this link

char ddata[512];
*(unsigned int*)(ddata+0)=123;
*(unsigned int*)(ddata+4)=add-8;
memset(ddata+8,0,add-8);

I don’t know exactly what is going on here, I understand that they throw char on an unsigned int, but what is it ddata+0and what is being done here? Thanks.

+4
source share
2 answers

You can not say.

This is because the behavior when casting char*to unsigned*is equal to undefined , if the pointer did not start as unsigned*, that is not the case in your case.

ddata + 0equivalent to ddata.

ddata + 4equivalent &ddata[4], that is, the address of the 5th element of the array.

, , C unsigned . - ; , , , unsigned 4 , .

+2

(123) 4 4 char ddata. (add-8) 4 , , add-8 0.

javascript -, , , . , javascript, , .

, - , C.

, undefined , , ddata unsigned int cast *(unsigned int*)ddata = 123;, , int 4 , .

Redhat linux , , C-, , MacOS, Intel . Javascript, .

:

unsigned char ddata[512];
if (add <= 512) {
    ddata[0] = 123;
    ddata[1] = 0;
    ddata[2] = 0;
    ddata[3] = 0;
    ddata[4] = ((add-8) >> 0) & 255;
    ddata[5] = ((add-8) >> 8) & 255;
    ddata[6] = ((add-8) >> 16) & 255;
    ddata[7] = ((add-8) >> 24) & 255;
    memset(ddata + 8, 0, add - 8);
}
0

Source: https://habr.com/ru/post/1630027/


All Articles