<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: HEX code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 13:15:51 GMT</pubDate>
    <description>DZone Snippets: HEX code</description>
    <item>
      <title>Convert a decimal value to hex in Python</title>
      <link>http://snippets.dzone.com/posts/show/2270</link>
      <description>// Convert a dec value to hex &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;hexValue = "%X" % 256&lt;br /&gt;print hexValue  #100&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 13 Jul 2006 19:12:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2270</guid>
      <author>kodreaming ()</author>
    </item>
    <item>
      <title>c C++ convert hex to ascii</title>
      <link>http://snippets.dzone.com/posts/show/2073</link>
      <description>// Presented with hex such as 0x12345abc perhaps there is a spot in there&lt;br /&gt;// which represents an ascii char - such as 53 would be 'S'&lt;br /&gt;// Common when dealing with hardware-related data structures and wire&lt;br /&gt;// protocols&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;*	To convert 53 to the character 'S':&lt;br /&gt;*	char returnVal = hexToString('5', '3');&lt;br /&gt;*/&lt;br /&gt;char hexToAscii(char first, char second)&lt;br /&gt;{&lt;br /&gt;	char hex[5], *stop;&lt;br /&gt;	hex[0] = '0';&lt;br /&gt;	hex[1] = 'x';&lt;br /&gt;	hex[2] = first;&lt;br /&gt;	hex[3] = second;&lt;br /&gt;	hex[4] = 0;&lt;br /&gt;	return strtol(hex, &amp;stop, 16);&lt;br /&gt;}&lt;br /&gt;int main(int argc, char* argv[])&lt;br /&gt;{&lt;br /&gt;	printf("%c\n", hexToAscii('5', '3'));&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;produces this output:&lt;br /&gt;S</description>
      <pubDate>Thu, 18 May 2006 20:37:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2073</guid>
      <author>ocorpening (Owen Corpening)</author>
    </item>
    <item>
      <title>Csharp format integer as hex string</title>
      <link>http://snippets.dzone.com/posts/show/2072</link>
      <description>// Using the built in ability to print a byte as a hex value&lt;br /&gt;// this set of methods gives the ability to print shorts&lt;br /&gt;// and longs as hex using the spacing that is customary.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace testApp&lt;br /&gt;{&lt;br /&gt;	/// &lt;summary&gt;&lt;br /&gt;	/// Summary description for Class1.&lt;br /&gt;	/// &lt;/summary&gt;&lt;br /&gt;	class Class1&lt;br /&gt;	{&lt;br /&gt;		/// &lt;summary&gt;&lt;br /&gt;		/// The main entry point for the application.&lt;br /&gt;		/// &lt;/summary&gt;&lt;br /&gt;		[STAThread]&lt;br /&gt;		static void Main(string[] args)&lt;br /&gt;		{&lt;br /&gt;			Class1 t = new Class1();&lt;br /&gt;			long i = 12; // &lt;br /&gt;			Console.WriteLine(t.int32ToHexString(i));&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		private string int32ToHexString(long i)&lt;br /&gt;		{&lt;br /&gt;			byte[] int32Bytes;&lt;br /&gt;			int32Bytes = BitConverter.GetBytes(i);&lt;br /&gt;			return String.Format("{0}{1}{2}{3}",&lt;br /&gt;				padString(int32Bytes[0].ToString("X")),&lt;br /&gt;				padString(int32Bytes[1].ToString("X")),&lt;br /&gt;				padString(int32Bytes[2].ToString("X")),&lt;br /&gt;				padString(int32Bytes[3].ToString("X")));&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		private string int16ToHexString(short i)&lt;br /&gt;		{&lt;br /&gt;			byte[] int32Bytes;&lt;br /&gt;			int32Bytes = BitConverter.GetBytes(i);&lt;br /&gt;			return String.Format("{0}{1}",&lt;br /&gt;				padString(int32Bytes[0].ToString("X")),&lt;br /&gt;				padString(int32Bytes[1].ToString("X")));&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		private string byteToHexString(byte b)&lt;br /&gt;		{&lt;br /&gt;			return padString(String.Format("{0}", b.ToString("X")));&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		private string padString(string s)&lt;br /&gt;		{&lt;br /&gt;			while (s.Length &lt; 2) s = "0" + s;&lt;br /&gt;			while (s.Length &lt; 3) s = " " + s;&lt;br /&gt;			return s;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;produces this output:&lt;br /&gt; 0C 00 00 00</description>
      <pubDate>Thu, 18 May 2006 20:25:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2072</guid>
      <author>ocorpening (Owen Corpening)</author>
    </item>
    <item>
      <title>str_hex and hex_str</title>
      <link>http://snippets.dzone.com/posts/show/2039</link>
      <description>// Convert hex to string and vice versa.&lt;br /&gt;//&lt;br /&gt;// (Source: http://codedump.jonasjohn.de/)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function str_hex($string){&lt;br /&gt;    $hex='';&lt;br /&gt;    for ($i=0; $i &lt; strlen($string); $i++){&lt;br /&gt;        $hex .= dechex(ord($string[$i]));&lt;br /&gt;    }&lt;br /&gt;    return $hex;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;function hex_str($hex){&lt;br /&gt;    $string='';&lt;br /&gt;    for ($i=0; $i &lt; strlen($hex)-1; $i+=2){&lt;br /&gt;        $string .= chr(hexdec($hex[$i].$hex[$i+1]));&lt;br /&gt;    }&lt;br /&gt;    return $string;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// example:&lt;br /&gt;&lt;br /&gt;$hex = str_hex("test sentence...");&lt;br /&gt;// $hex contains 746573742073656e74656e63652e2e2e&lt;br /&gt;&lt;br /&gt;print hex_str($hex);&lt;br /&gt;// outputs: test sentence...&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 16 May 2006 01:41:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2039</guid>
      <author>jonasj (Jonas J.)</author>
    </item>
    <item>
      <title>HEX to RGB</title>
      <link>http://snippets.dzone.com/posts/show/214</link>
      <description>&lt;code&gt;&lt;br /&gt;function hexToRGB ( hex:Number ){&lt;br /&gt;   var returnObj:Object = new Object();   &lt;br /&gt;   var returnObj .r = hex &gt;&gt; 16;&lt;br /&gt;   var temp = hex ^ r &lt;&lt; 16;&lt;br /&gt;   var returnObj .g = temp &gt;&gt; 8;&lt;br /&gt;   var returnObj .b = temp ^ g &lt;&lt; 8;&lt;br /&gt;&lt;br /&gt;   return returnObj;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 22 Apr 2005 03:57:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/214</guid>
      <author>edolecki (eric e. dolecki)</author>
    </item>
    <item>
      <title>RGB to HEX</title>
      <link>http://snippets.dzone.com/posts/show/213</link>
      <description>&lt;code&gt;&lt;br /&gt;function RGBToHex (r, g, b ){&lt;br /&gt;    var hex = r &lt;&lt; 16 ^ g &lt;&lt; 8 ^ b;&lt;br /&gt;   return hex;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 22 Apr 2005 03:55:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/213</guid>
      <author>edolecki (eric e. dolecki)</author>
    </item>
  </channel>
</rss>
