@@ -42,7 +42,7 @@ public function deserialize($header)
4242 return new SpanContext (
4343 strtolower ($ matches [1 ]),
4444 array_key_exists (2 , $ matches ) && !empty ($ matches [2 ])
45- ? dechex (( int )( $ matches [2 ]) )
45+ ? $ this -> decToHex ( $ matches [2 ])
4646 : null ,
4747 array_key_exists (3 , $ matches ) ? $ matches [3 ] == '1 ' : null ,
4848 true
@@ -61,11 +61,65 @@ public function serialize(SpanContext $context)
6161 {
6262 $ ret = '' . $ context ->traceId ();
6363 if ($ context ->spanId ()) {
64- $ ret .= '/ ' . hexdec ($ context ->spanId ());
64+ $ ret .= '/ ' . $ this -> hexToDec ($ context ->spanId ());
6565 }
6666 if ($ context ->enabled () !== null ) {
6767 $ ret .= ';o= ' . ($ context ->enabled () ? '1 ' : '0 ' );
6868 }
6969 return $ ret ;
7070 }
71+
72+ private function decToHex ($ numstring )
73+ {
74+ $ int = (int ) $ numstring ;
75+ if ($ this ->isBigNum ($ int )) {
76+ return $ this ->baseConvert ($ numstring , 10 , 16 );
77+ }
78+ return dechex ($ int );
79+ }
80+
81+ private function hexToDec ($ numstring )
82+ {
83+ $ dec = hexdec ($ numstring );
84+ if ($ this ->isBigNum ($ dec )) {
85+ return $ this ->baseConvert ($ numstring , 16 , 10 );
86+ }
87+ return $ dec ;
88+ }
89+
90+ private function isBigNum ($ number )
91+ {
92+ return $ number >= PHP_INT_MAX ;
93+ }
94+
95+ private function baseConvert ($ numstring , $ fromBase , $ toBase )
96+ {
97+ $ chars = "0123456789abcdefghijklmnopqrstuvwxyz " ;
98+ $ newstring = substr ($ chars , 0 , $ toBase );
99+
100+ $ length = strlen ($ numstring );
101+ $ result = '' ;
102+
103+ for ($ i = 0 ; $ i < $ length ; $ i ++) {
104+ $ number [$ i ] = strpos ($ chars , $ numstring {$ i });
105+ }
106+
107+ do {
108+ $ divide = 0 ;
109+ $ newlen = 0 ;
110+ for ($ i = 0 ; $ i < $ length ; $ i ++) {
111+ $ divide = $ divide * $ fromBase + $ number [$ i ];
112+ if ($ divide >= $ toBase ) {
113+ $ number [$ newlen ++] = (int )($ divide / $ toBase );
114+ $ divide = $ divide % $ toBase ;
115+ } elseif ($ newlen > 0 ) {
116+ $ number [$ newlen ++] = 0 ;
117+ }
118+ }
119+ $ length = $ newlen ;
120+ $ result = $ newstring {$ divide } . $ result ;
121+ } while ($ newlen != 0 );
122+
123+ return $ result ;
124+ }
71125}
0 commit comments