Binary form of strings
#4

pawn Code:
#include a_samp

main() {
    StringToBinary("Test");
}

StringToBinary(str[]) {
    new dest[cellmax];
   
    for(new i, l = strlen(str); i < l; i++) {
        new mask = 1 << (8 - 1),
            bits[8];

        for(new d; d < 8; d++)
        {
            if((str[i] & mask) == 0)
                bits[d] = '0';
            else
                bits[d] = '1';

            mask >>= 1;
        }
       
        strcat(dest, bits);
    }

    printf("Print as a string: %s", dest);
    printf("Print with strval: %i", strval(dest));
}
Output (which is correct):
Quote:

Print as a string: 01010100011001010111001101110100
Print with strval: 1362660180


The only thing that needs a fixed is the size of 'dest'. 500 won't do for strings above 62 characters. For example:
This one works: StringToBinary("6660000000000000000000000000000000 0000000000000000000000000777");
This one doesn't: StringToBinary("6660000000000000000000000000000000 00000000000000000000000000777");
Reply


Messages In This Thread
Binary form of strings - by Gammix - 07.09.2015, 06:17
Re: Binary form of strings - by Crayder - 07.09.2015, 07:24
Re: Binary form of strings - by Gammix - 07.09.2015, 07:33
Re: Binary form of strings - by Crayder - 07.09.2015, 07:42
Re: Binary form of strings - by Gammix - 07.09.2015, 08:03
Re: Binary form of strings - by Crayder - 07.09.2015, 08:12
Re: Binary form of strings - by Gammix - 07.09.2015, 08:18
Re: Binary form of strings - by Crayder - 07.09.2015, 16:24
Re: Binary form of strings - by maddinat0r - 07.09.2015, 18:36
Re: Binary form of strings - by Mauzen - 10.09.2015, 01:45

Forum Jump:


Users browsing this thread: 2 Guest(s)