Re: how to encode a float in base64?
On Mar 17, 4:18 am, Jack Klein <jackkl...@spamcop.net> wrote:
On Sun, 16 Mar 2008 18:28:41 -0700 (PDT), aaragon
<alejandro.ara...@gmail.com> wrote in comp.lang.c++:
[...]
You don't encode a float, or a double, or an int, or a text
stream in base64, you encode streams of 8-bit octets, which
happen to correspond to streams of bytes on most architecture.
Also note that you can only include streams with a length that
is evenly divisible by 3, so if you have a stream of bytes
that is not evenly divisible by 3, you add one or two bytes of
0 at the end before encoding.
So if you have verified, on your implementation, that
sizeof(float) is 4 and CHAR_BIT is 8, and that one float is
all that you want to encode, you should define an array of 6
characters, initialized to 0. Then you can memcpy() the float
into the beginning of the buffer. Then you can encode the 6
byte buffer.
Why not just skip the memcpy?
The important point about base64 (which you point out in the
first paragraph above) is that it isn't a primary format, but a
secondary one---it is used to convert a binary format into a
stream of ASCII characters, which can be transmitted over a link
which doesn't support binary transparently. You still have to
define the binary format: how you convert a float into a stream
of 8-bit octets. How you do this, of course, will depend on
what binary format you are using: XDR, for example requires four
octets, with the sign on bit 7 of the first octet, etc., etc.
Until you've defined this format, it's impossible to say how to
convert a float to base64. (More generally, the question
doesn't have an answer, because base64 itself doesn't encode
floats, etc., but only octet streams.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34