String to binary conversion

Colin via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 3 06:19:49 PDT 2014


On Wednesday, 3 September 2014 at 12:33:49 UTC, clearion wrote:
> I would like to write binary data to a file for an ancillary 
> hash
> table operation and then read it back using stream.rawRead(). 
> How
> would I go about converting a string to binary in D. I would
> prefer not to use any third party libraries if I can. Thank You

I think this:

void main(){
     string str = "Test string";
     auto file = File("output.txt", "w");
     auto bin = cast(ubyte[])str;
     foreach(b; bin){
         file.writef("%b", b);
     }
     file.close();
}

will do it?
This is untested though....


More information about the Digitalmars-d mailing list