07.09.2015, 07:42
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));
}
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");