Cplusplus Programmer said:
On Sun, 2012-03-25, Cplusplus Programmer wrote:
Hello all,
Suppose I need to store the binary notation of an integer value intoa
char and later write it to a binary file.
However I do not know beforehand the value. So I would like to reserve
say n (with n =3D5) bytes for that integer. It may sound odd, however
the type of file I am going to write represents an integer in a
variable number of bytes. In this way we can store any integer value
as large we want. So, I don't know the value I am going to store, but
I have an upper limit and thus I reserve 5 bytes for it. But now when
I know the value, I discover I only need 3 bytes. Now I have to add 2
pad bytes. Can someone explain to me how to add pad bytes ? Is this
binary notation 0000 a pad byte ? So if I had char[2] =3D 0; then
char[2] is a pad byte ?
You're confusing me. You talk about storing the integer in a char,
then it's 5 bytes, and finally 3 bytes. First the representation is a
variable number of bytes, but then you talk about adding pad bytes.
Step back for a moment and write down:
acceptable inputs:
(e.g. "any uint32_t" or "any int32_t")
output encoding:
(here you specify what a 5-, 4-, 3-, 2- and 1-octet encoding
looks like, and what each bit represents)
It's hard to give advice without understanding this.
/Jorgen
Sorry for causing confusing. I will try to keep it simple. What I want
is this. It does not matter what kind of value it is. Just suppose I
need to store it binary notation. I don't know how many bytes it will
take, but I do know what it maximum number of bytes it at most will
can take (Say 5 butes). Therefore I reserve the max number of bytes
for it in a char array (I suppose here that each char element in an
array takes 1 byte, correct me if I am wrong). Now, when I know the
actual value, I see that the value needs only 3 bytes. Now I want to
add two padding bytes. Now my question is what is a padding byte and
how shall I add this. Sorry, if it is still confusing, because this is
how I understand the problem I got. I need to add pad bytes to fill up
the rest of the bytes that does not represent the actual value I am
storing.
ccp
Use an 8-byte integer data type internally and save the low order 5 bytes..
e.g.
uint64_t value = 255 * 255 * 255 - 5; // unsigned magnitude less than 2^24.
// 'value' now already has the appropriate padding bytes.
// Write in little endian to output device
for (unsigned char i = 0; i < 5; i++, value >> 8) {
write(fd, value & 0xffull, 1);
}
// or, write in big endian (on a little-endian architecture like i836)
uint8_t *valp = ((uint32_t *)&value + 1);
for (unsigned char i=0; i < 5; i++, value << 8) {
write(fd, *(unsigned char *)valp, 1);
}
C++ purists will dislike the casts, and they may not be universally
applicable to all architecture types (e.g. a BCD architecture addressible
to the digit/nibble). For 32-bit x86 systems, 64-bit integer math
is implemented as either library calls or extended code sequences so may
be less efficient.
For 32-bit and 16-bit fields stored in external form or used for inter-host
network communcations, you may find the 'ntoh[ls]' and hton[ls] functions
useful. The bsd endian functions also provide 64-bit functions:
uint16_t htobe16(uint16_t host_16bits);
uint16_t htole16(uint16_t host_16bits);
uint16_t be16toh(uint16_t big_endian_16bits);
uint16_t le16toh(uint16_t little_endian_16bits);
uint32_t htobe32(uint32_t host_32bits);
uint32_t htole32(uint32_t host_32bits);
uint32_t be32toh(uint32_t big_endian_32bits);
uint32_t le32toh(uint32_t little_endian_32bits);
uint64_t htobe64(uint64_t host_64bits);
uint64_t htole64(uint64_t host_64bits);
uint64_t be64toh(uint64_t big_endian_64bits);
uint64_t le64toh(uint64_t little_endian_64bits);
The availability of the above mentioned functions on windows systems may
be limited, but windows likely has a (more verbose) equivalent.