Binary form of strings
#1

I have been trying this for a while and have failed to extract them values.

Just to those who don't know what i am doing: This is discussion for converting a string into an integer form (0 and 1 only).

Here is my code:
pawn Code:
stock StringToBinary(str[]) {
    new
        dest[500]
    ;
    for(new i = 0, l = strlen(str); i < l; i++) {
        new
            total = 0,
            ascii = str[i],
            binary[8]
        ;

        while (ascii > 0) {
            if ((ascii % 2) == 0) {
                binary[total] = 0;
            }
            else {
                binary[total] = 1;
            }

            ascii /= 2;

            total += 1;
        }

        total -= 1;
        while (total >= 0) {
            format(dest, sizeof(dest), "%s%i", dest, binary[total]);

            total -= 1;
        }
    }

    printf("Print as a string: %s", dest);
    printf("Print with strval: %i", strval(dest));
}
When i use it to for test:
pawn Code:
StringToBinary("Test");
The results are not good:
pawn Code:
Print as a string: 1010100110010111100111110100
Print with strval: 132202452
The problem is that strval somehow limits the characters or its not having high bytes rate. Where as the binary form is doing very well. As you can see the 1st result as a string, it prints all the characters but i can't use it for int vars or arrays.

For example i want to do something like this:
pawn Code:
new binnary = StringToBinary("Test");
So after putting the result into a var then i will extract it using my inverse function: BinaryToString.

Does anyone have a good String to Binary converter supporting PAWN or any solution?
Reply
#2

Perhaps it's a lot easier than that? Something like this:
pawn Code:
stock StringToBinary(str[]) {
    new
        dest[500]
    ;
    for(new i, l = strlen(str); i < l; i++) {
        new bits[8];
        for(new d; d < 8; d++)
            bits[d] = ((1 << d) & str[i]) != 0 ? 1 : 0;
       
        strcat(dest, bits);
    }
}

On a another note, shouldn't your first while loop be a do-while?
Reply
#3

Quote:
Originally Posted by Crayder
View Post
Perhaps it's a lot easier than that? Something like this:
pawn Code:
stock StringToBinary(str[]) {
    new
        dest[500]
    ;
    for(new i, l = strlen(str); i < l; i++) {
        new bits[8];
        for(new d; d < 8; d++)
            bits[d] = ((1 << d) & str[i]) != 0 ? 1 : 0;
       
        strcat(dest, bits);
    }
}

On a another note, shouldn't your first while loop be a do-while?
No valid characters are printed when i performed a similar test:
pawn Code:
StringToBinary("Test");
And actually the problem is not the method but extracting the numerals into a var, how can i directly put the values into a var or array without using strval, because strval works on very less bytes limit.
Reply
#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
#5

Yes, but you don't concentrate on the problem, i.e.:
Quote:

And actually the problem is not the method but extracting the numerals into a var, how can i directly put the values into a var or array without using strval, because strval works on very less bytes limit.

I can't directly do:
pawn Code:
new  binary = dest;
or the strval does give a low byte result which can't handle longer numbers:
pawn Code:
new binary = strval(dest); // shitty result
A method using high bytes rate for string to number conversion.
Reply
#6

Quote:
Originally Posted by Gammix
View Post
Yes, but you don't concentrate on the problem, i.e.:


I can't directly do:
pawn Code:
new  binary = dest;
or the strval does give a low byte result which can't handle longer numbers:
pawn Code:
new binary = strval(dest); // shitty result
A method using high bytes rate for string to number conversion.
Umm... If you don't want it as a string, shouldn't you just store it in a bool char array (i.e. "new bool:dest[charmax char];")?
Reply
#7

Quote:
Originally Posted by Crayder
View Post
Umm... If you don't want it as a string, shouldn't you just store it in a bool char array (i.e. "new bool:dest[cellmax char];")?
No.

I am doing such task for my dialogs include. People complain or precisely do't like large sized output files using just few lines of dialogs2.inc. So in order to resist the 3D array and for integrated permanently both the includes, i had the idea of binary from text.

Basically the idea can be implemented something like this, this 3d array:
pawn Code:
g_DialogModelLabels[MAX_PLAYERS][MAX_DIALOG_LISTITEMS][MAX_DIALOG_MODEL_LABEL_SIZE];
to
pawn Code:
g_DialogModelLabels[MAX_PLAYERS][MAX_DIALOG_LISTITEMS]
For example:
pawn Code:
g_DialogModelLabels[playerid][0] = "test";
would be
pawn Code:
g_DialogModelLabels[playerid][0] = 001001000100101100...;
(strings eat way more than integers)

> I wonder how much byte does db_get_field_int handles? I could use it to directly insert the integer value into the desired array or var.
Reply
#8

Still not sure what you want. If you still want to store the word "test" but in binary form you just need to put '0b' at the beginning of the binary, however, that isn't going to change the number of bytes taken by the array. If you are literally trying to take the binary in as an integer, that simply won't work because anything more than 1 character could easily go over the cellmax.
Reply
#9

You do realize that this is already implemented into PAWN as packed strings?
Code:
new string[5 char] = !"test";
printf("number base 10: %d", string[0]);
printf("number base 2: %b", string[0]);
will print
Code:
number base 10: 1952805748
number base 2: 1110100011001010111001101110100
Reply
#10

Quote:
Originally Posted by maddinat0r
View Post
You do realize that this is already implemented into PAWN as packed strings?
Code:
new string[5 char] = !"test";
printf("number base 10: %d", string[0]);
printf("number base 2: %b", string[0]);
will print
Code:
number base 10: 1952805748
number base 2: 1110100011001010111001101110100
This, but you need to access it with {} isntead of [] to get the actual char
Else just use the last 8 bits of each char field only, when using non-packed string, and eventually fill it up with zeroes in printf.

pawn Code:
new a_very_long_string[123456];

// I actually dont know if %b accepts zero-filling, but I guess it does
printf("%08b", a_very_long_string[0] & 0xFF);
I wonder what youd need that for. Or did you try it just out of interest?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)