i have some python code which i want to use in an 100% php code. have you any ideas how i can convert the code ??? i have problems with converting the code especially the part get bit and set bit.
The bitmask is read out from a switch via snmp and the mask represent witch ports are in a defined vlan. Example:
snmpget 1.3.6.1.2.1.17.7.1.4.3.1.2. returns an HEX String like F100000000000000
Every hex digit represents 4 physically LAN ports of the switch. The first digit in this example is F which means in binary 1111. This means that every port from 1-4 is in the vlan.
If you want only port 1,2 and 4 the mask will be 1101 and is in hex D.
The goal of the code is that i want to know for example if port 15 is in the vlan or not (getbitatposition) and if not that i can change the bitmask at the position of the port (setbitatposition).
Port 15 for example is in char 4 from left:
F = Port 1-4
1 = Port 5-8
0 = Port 9-12
0 = Port 13-16 => HEX 0 => Binary 0000
To get port 15 in the vlan i must change the 3rd bit from 0000 to 0010 and convert it back to hex => 2.
The new mask will be for example F102000000000000
The python code:
def convertHexCharacterToInt(char):
if (char.upper() == "A"):
return 10
elif (char.upper() == "B"):
return 11
elif (char.upper() == "C"):
return 12
elif (char.upper() == "D"):
return 13
elif (char.upper() == "E"):
return 14
elif (char.upper() == "F"):
return 15
else:
return (int(char))
def convertIntToHexCharacter(integer):
if (integer < 0 or integer > 15):
return "-1"
if (integer < 10):
return str(integer)
elif (integer == 10):
return "A"
elif (integer == 11):
return "B"
elif (integer == 12):
return "C"
elif (integer == 13):
return "D"
elif (integer == 14):
return "E"
elif (integer == 15):
return "F"
def __getBitAtPosition(position, bitmap):
for x in range(0, len(bitmap)):
mask = 0x8
for y in range(0, 4):
if (((x * 4) + y + 1) == position):
return (convertHexCharacterToInt(bitmap[x]) & mask) != 0
mask = mask >> 1
return None
def __setBitAtPosition(position, bitmap, value):
if (__getBitAtPosition(position, bitmap) == value):
return bitmap
charPosition = (position + 3) / 4 - 1
bitPosition = int(math.fabs((position - (charPosition * 4)) - 4))
bitValue = 2 ** bitPosition
fourBitValue = convertHexCharacterToInt(bitmap[charPosition])
newValue = None
if (value):
newValue = fourBitValue + bitValue
else:
newValue = fourBitValue - bitValue
newBitmap = bitmap[:charPosition] + convertIntToHexCharacter(newValue) + bitmap[charPosition + 1:]
return newBitmap
This was my first try but the result is not the same:
private function _convertHexCharacterToInt($char){
if (strtoupper($char) == "A"){
return 10;}
elseif (strtoupper($char) == "B"){
return 11;}
elseif (strtoupper($char) == "C"){
return 12;}
elseif (strtoupper($char) == "D"){
return 13;}
elseif (strtoupper($char) == "E"){
return 14;}
elseif(strtoupper($char) == "F"){
return 15;}
else {
return $char;
}
}
private function _getBitAtPosition($pos,$bitmap){
foreach(range(0,strlen($bitmap)) as $x){
$mask=0x8;
foreach(range(0,3) as $y){
if((($x * 4) + $y + 1) == $pos){
if($this->_convertHexCharacterToInt(substr($bitmap,$x,1))&$mask != 0{
return $this->_convertHexCharacterToInt(substr($bitmap,$x,1))&$mask;
}
$mask = $mask >> 1
}
}
}
}