Serialization #5 - Serialization Writer Basics

preview_player
Показать описание


-------------------------------------------------------------------------------------------------

Рекомендации по теме
Комментарии
Автор

In C++, you could do this, too:

int l_integer = 16;
unsigned char* lp_bytes = reinterpret_cast<unsigned char*>(&l_integer);

In terms of efficiency, how would this compare to using the bit-shifting method used in the video?

dgdev
Автор

I don't understand why you don't use java's built-in ByteBuffer. It supports all the operations in this video as well as read except it calls it put and get. I guess you didn't know about it. It support both big and little endian through a call to order(). It can either have a backed array like in this video, or use direct memory for faster IO when writing to disk/network. I really recommend anyone to use ByteBuffer rather than reinventing your own.

Same example as in the video, but using a ByteBuffer backed by an array.

ByteBuffer buff = ByteBuffer.allocate(16);

short num = 10000;

buff.putShort(num);
buff.putShort(num);
buff.putShort(num);
buff.putShort(num);

printBytes(buff.array()); // Gets the actual array backing the byte buffer, not just a copy.

BlackMsh
Автор

Using & 0xff is pretty unnecessary if you just cast the short to a byte right after shifting, since in java that just truncates the first 8 bits, right? @18:00 is what I mean

dallasyoung
Автор

pretty late comment but why are you using byte for the array type, from what I googled, byte in java is an 8 bit signed type, how come you aren't using an unsigned byte?

demolitionist
Автор

but why do you put value & 0xff00 ? and not just value >>8

TheHHG
Автор

How should we do this in c++? We cant assume a size for int. Thanks in advance for your answer

palmenros