What it the preferred method to write a class to a file?

Charles D Hixson charleshixsn at earthlink.net
Tue Jul 25 11:00:02 PDT 2006


Derek wrote:
> On Mon, 24 Jul 2006 13:34:12 -0700, Charles D Hixson wrote:
> 
>> xs0 wrote:
>>>> P.S.:  Is there a standard library routine for converting
>>>> between strings of length 4 and uint-s?  If so I wasn't able
>>>> to find it.  If not, I wasn't able to determine that it
>>>> didn't exist.  (That would have made writing the sig more
>>>> efficient.)
>>> union {
>>>     uint asUInt;
>>>     char[4] asChars;
>>> }
>>>
>>> or something to that effect :)
>> For some reason when I tried that I was told that I was
>> using "illegal utf8 characters".  But Jarrett's approach
>> worked fine.
> 
> How about 
>  union {
>      uint asUInt;
>      ubyte[4] asChars;
>  }
> 
> Are you really using *characters* or *bytes*?
> 
> 
characters.  My current (working) versions are:

uint  char4ToUint(char[4] item)
{   return  *cast(uint *)item.ptr;  }
char[] uintToChar4(uint item)
{  char[]  itmStr = new char[4];
   *cast(uint *)itmStr.ptr  =  item;
   return  itmStr[0..4];
}

Note that even with the version the "illegal utf8
characters" sneaks in unless I return a range from the
string.  I have no idea as to why, I've just tried various
things until something worked.
(Note the heavy influence of Jarrett's code.)

The test I used was to run a string through both of them in
turn, and check that the output was the same as the input.
I didn't check all possible strings, so there may be corner
cases.  E.g., there may well be numbers that will generate
illegal utf-8 characters.  (I can't see how, but then I
don't know why this version works and several that appeared
to me to be essentially equivalent didn't work.)

I didn't save the version using a union, and I didn't try a
byte array.  (I don't know why...perhaps that seemed like a
way of dodging the error message without dodging the error?
Since currently a lot of what is happening feels like magic,
I tend to be a bit conservative when an unexplained error
message shows up.)



More information about the Digitalmars-d-learn mailing list