snipt

Ctrl+h for KB shortcuts

PHP

Generate random hex color

1
2
3
4
5
<?php

$color = '#'.str_pad(dechex(rand(0,255)), 2, 0).str_pad(dechex(rand(0,255)), 2, 0).str_pad(dechex(rand(0,255)), 2, 0);

?>
https://snipt.net/embed/6cf0e2b075edfb2514c35c15e9f17dc4/
https://snipt.net/raw/6cf0e2b075edfb2514c35c15e9f17dc4/
6cf0e2b075edfb2514c35c15e9f17dc4
php
PHP
5
2016-09-30T01:58:43
True
False
False
/api/public/snipt/147047/
generate-random-hex-color
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="nv">$color</span> <span class="o">=</span> <span class="s1">&#39;#&#39;</span><span class="o">.</span><span class="nb">str_pad</span><span class="p">(</span><span class="nb">dechex</span><span class="p">(</span><span class="nb">rand</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">)),</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="nb">str_pad</span><span class="p">(</span><span class="nb">dechex</span><span class="p">(</span><span class="nb">rand</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">)),</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="nb">str_pad</span><span class="p">(</span><span class="nb">dechex</span><span class="p">(</span><span class="nb">rand</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">)),</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
color, hex, php

Python

return hexadecimal representation of integer

def dec2hex(n):
    return "%X" % n
https://snipt.net/embed/6ae348bffd5515cb0e73e3f94e5b6db5/
https://snipt.net/raw/6ae348bffd5515cb0e73e3f94e5b6db5/
6ae348bffd5515cb0e73e3f94e5b6db5
python
Python
2
2016-09-30T19:16:08
True
False
False
/api/public/snipt/28373/
return-hexadecimal-representation-of-integer
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">def</span> <span class="nf">dec2hex</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">return</span> <span class="s">&quot;</span><span class="si">%X</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">n</span> </span></pre></div> </td></tr></table>
base, hex, python

Python

Convert a hexstring to an ASCII-String of optional XOR.

1
2
3
4
5
6
7
def hexstring2ascii(hexstring, xor=0):
    """ Convert a hexstring to an ASCII-String. If you like to XOR it, give the
        xor value as an integer. If not, leave it blank """
    ascii = ""
    for i in xrange(0, len(hexstring)/2):
        ascii = ascii + chr(int(hexstring[2*i:2*i+2],16)^xor) 
    return ascii
https://snipt.net/embed/db58bb40b5d5be5f5b56f8460f92054a/
https://snipt.net/raw/db58bb40b5d5be5f5b56f8460f92054a/
db58bb40b5d5be5f5b56f8460f92054a
python
Python
7
2016-09-30T19:16:33
True
False
False
/api/public/snipt/27926/
convert-a-hexstring-to-an-ascii-string-of-optional-xor
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">def</span> <span class="nf">hexstring2ascii</span><span class="p">(</span><span class="n">hexstring</span><span class="p">,</span> <span class="n">xor</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span> </span><span id="L-2"><a name="L-2"></a> <span class="sd">&quot;&quot;&quot; Convert a hexstring to an ASCII-String. If you like to XOR it, give the</span> </span><span id="L-3"><a name="L-3"></a><span class="sd"> xor value as an integer. If not, leave it blank &quot;&quot;&quot;</span> </span><span id="L-4"><a name="L-4"></a> <span class="n">ascii</span> <span class="o">=</span> <span class="s">&quot;&quot;</span> </span><span id="L-5"><a name="L-5"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">hexstring</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">):</span> </span><span id="L-6"><a name="L-6"></a> <span class="n">ascii</span> <span class="o">=</span> <span class="n">ascii</span> <span class="o">+</span> <span class="nb">chr</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">hexstring</span><span class="p">[</span><span class="mi">2</span><span class="o">*</span><span class="n">i</span><span class="p">:</span><span class="mi">2</span><span class="o">*</span><span class="n">i</span><span class="o">+</span><span class="mi">2</span><span class="p">],</span><span class="mi">16</span><span class="p">)</span><span class="o">^</span><span class="n">xor</span><span class="p">)</span> </span><span id="L-7"><a name="L-7"></a> <span class="k">return</span> <span class="n">ascii</span> </span></pre></div> </td></tr></table>
ascii, cipher, encoding, hex, python
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.