Raised This Month: $51 Target: $400
 12% 

[ H3LP ] Converter bitsum to string?


Post New Thread Reply   
 
Thread Tools Display Modes
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 04-29-2017 , 21:24   Re: [ H3LP ] Converter bitsum to string?
Reply With Quote #11

Try this:
PHP Code:
#include <amxmodx>

enum PlayerFlags (<<= 1) {
    
PF_Alive 1,
    
PF_InWater,
    
PF_OnGround,
    
PF_Bot,
    
PF_Hltv
};

// This violates the DRY principle, but idk how to do this without repeating
// (I think there is no way to avoid this in Pawn,
// because macros and syntax aren't so powerful,
// I know how to do this in C++, but there is no way)
new const PlayerFlagsStrings[][] = {
    
"PF_Alive",
    
"PF_InWater",
    
"PF_OnGround",
    
"PF_Bot",
    
"PF_Hltv"
};

public 
plugin_init() {
    
server_print("%s"PlayerFlags_Serialize(PF_Hltv));
    
server_print("%s"PlayerFlags_Serialize(PF_Alive PF_OnGround));
    
server_print("%d"PlayerFlags_Deserialize("PF_Hltv"));
    
server_print("%d"PlayerFlags_Deserialize("PF_Alive, PF_OnGround"));
}

stock PlayerFlags_Serialize(PlayerFlags:value) {
    return 
BitFlagsEnum_Serialize(PlayerFlagsStringsvalue);
}

stock PlayerFlags:PlayerFlags_Deserialize(const value[]) {
    static 
Trie:valuesMap Invalid_Trie;
    if (
valuesMap == Invalid_Trie) {
        
valuesMap TrieCreate();
        for (new 
0sizeof(PlayerFlagsStrings); i++) {
            
TrieSetCell(valuesMapPlayerFlagsStrings[i], (<< i));
        }
    }
    
    return 
BitFlagsEnum_Deserialize(valuesMapvalue);
}

const 
MaxSerializedStringSize 256;
// TODO: error checking? optimizing?
stock BitFlagsEnum_Serialize(const valuesMap[][], any:value) {
    static 
serialized[MaxSerializedStringSize];
    
// foreach bit from lower to higher
    
for (new temp _:value,
            
bitNumber 0,
            
bool:wasWrittenSomething false,
            
serializedLength 0;
        
temp != 0;
        
temp >>= 1, ++bitNumber
    
) {
        if (
temp == 0) {
            continue;
        }
        
        if (
wasWrittenSomething) {
            
serializedLength += formatex(serialized[serializedLength],
                
charsmax(serialized) - serializedLength,
                
", %s",
                
valuesMap[bitNumber]
            );
        } else {
            
serializedLength += copy(serializedcharsmax(serialized), valuesMap[bitNumber]);
            
wasWrittenSomething true;
        }
    }
    
    return 
serialized;
}

// TODO: error checking? optimizing?
stock any:BitFlagsEnum_Deserialize(Trie:valuesMap, const value[]) {
    static 
remainPart[MaxSerializedStringSize];
    const 
MaxSerializedValueStringSize 64;
    static 
currentValue[MaxSerializedValueStringSize];
    
strtok(value,
        
currentValuecharsmax(currentValue),
        
remainPartcharsmax(remainPart),
        
',', .trimSpaces true
    
);
    new 
deserialized;
    
TrieGetCell(valuesMapcurrentValuedeserialized);
    
    while (
remainPart[0] != EOS) {
        
strtok(remainPart,
            
currentValuecharsmax(currentValue),
            
remainPartcharsmax(remainPart),
            
',', .trimSpaces true
        
);
        new 
temp;
        
TrieGetCell(valuesMapcurrentValuetemp);
        
deserialized |= temp;
    }
    
    return 
deserialized;

It would print:
Code:
PF_Hltv
PF_Alive, PF_OnGround
16
5
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
Old 04-30-2017, 09:31
CrazY.
This message has been deleted by CrazY..
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 05-22-2017 , 19:32   Re: [ H3LP ] Converter bitsum to string?
Reply With Quote #12

Hm, I don't have tested your code. But anyway the thread it's now solved.
__________________








CrazY. is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:51.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode