c# - save string array in binary format -


string value1 , value1 ; int length1 , length2 ; system.collections.bitarray bitvalue1 = new system.collections.bitarray(length1); system.collections.bitarray bitvalue2 = new system.collections.bitarray(length2); 

i'm looking fastest way covert each string bitarray defined length each string (the string should trimmed if larger defined length , if strings size smaller remaining bits filled false) , put 2 strings , write in binary file .

edit : @dtb : simple example can value1 = "a" ,value2 = "b" , length1 =8 , length2 = 16 , result 010000010000000001000010 first 8 bits "a" , next 16 bits "b"

        //source string         string value1 = "t";         //length in bits         int length1 = 2;         //convert text array of ascii bytes         byte[] bytes = system.text.encoding.ascii.getbytes(value1);         //create temp bitarray bytes         system.collections.bitarray tempbits = new system.collections.bitarray(bytes);         //create output bitarray setting maximum length         system.collections.bitarray bitvalue1 = new system.collections.bitarray(length1);         //loop through temp array         for(int i=0;i<tempbits.length;i++)         {             //if we're outside of range of output array exit             if (i >= length1) break;             //otherwise copy value temp output             bitvalue1.set(i, tempbits.get(i));                         } 

and i'm going keep saying it, assumes ascii characters above ascii 127 (such é in résumé) freak out , return ascii 63 question mark.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -