1
我有一個字符串形式的MAC地址。我有興趣參加在Mac串並增加/ 1個值減小,但同時保持十六進制的完整性,PHP從PHP中的字符串中增加/減少MAC地址
例如:00:A1:2C:3B:99:1F
-1:00: A1:2C:3B:99:1E 1:00:A1:2C:3B:99:20
我有一個字符串形式的MAC地址。我有興趣參加在Mac串並增加/ 1個值減小,但同時保持十六進制的完整性,PHP從PHP中的字符串中增加/減少MAC地址
例如:00:A1:2C:3B:99:1F
-1:00: A1:2C:3B:99:1E 1:00:A1:2C:3B:99:20
轉換十六進制到十進制和背面
$hexstring = "00:A1:2C:3B:99:1F";
$hex = preg_replace("/:/", '', $hexstring);
$dec = hexdec($hex);
$dec--; //or ++
$hex = dechex($dec);
$hexstring = implode(":", str_split($hex, 2));
測試
<?php
$hexval="00:A1:2C:3B:99:1F";
//First 10 MAC Address Increments
for($i=0;$i<10;$i++)
{
$dec=hexdec($hexval);
$dec++;
$hexval=dechex($dec);
$hexval=rtrim(strtoupper(chunk_split($hexval, 2, ':')),':');
echo "00:A1:{$hexval}<br>";
}
echo "<br><br>";
//First 10 MAC Address Decrements
$hexval="00:A1:2C:3B:99:1F";
for($i=0;$i<10;$i++)
{
$dec=hexdec($hexval);
$dec--;
$hexval=dechex($dec);
$hexval=rtrim(strtoupper(chunk_split($hexval, 2, ':')),':');
echo "00:A1:{$hexval}<br>";
}
OUTPUT:
00:A1:2C:3B:99:20
00:A1:2C:3B:99:21
00:A1:2C:3B:99:22
00:A1:2C:3B:99:23
00:A1:2C:3B:99:24
00:A1:2C:3B:99:25
00:A1:2C:3B:99:26
00:A1:2C:3B:99:27
00:A1:2C:3B:99:28
00:A1:2C:3B:99:29
00:A1:2C:3B:99:1E
00:A1:2C:3B:99:1D
00:A1:2C:3B:99:1C
00:A1:2C:3B:99:1B
00:A1:2C:3B:99:1A
00:A1:2C:3B:99:19
00:A1:2C:3B:99:18
00:A1:2C:3B:99:17
00:A1:2C:3B:99:16
00:A1:2C:3B:99:15
結果出來爲FF:FF:FF:FF:FF:FF ... =( – Rob