<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Ocorpening's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 19:09:36 GMT</pubDate>
    <description>DZone Snippets: Ocorpening's Code Snippets</description>
    <item>
      <title>C convert buffer to binary string</title>
      <link>http://snippets.dzone.com/posts/show/2076</link>
      <description>// Sometimes one wants to see data in binary&lt;br /&gt;// This method converts an arbitrary buffer to 1's and 0's&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;#include &lt;memory.h&gt;&lt;br /&gt;#include &lt;string.h&gt;&lt;br /&gt;&lt;br /&gt;char *getBufferAsBinaryString(void *in)&lt;br /&gt;{&lt;br /&gt;	int pos=0;&lt;br /&gt;	char result;&lt;br /&gt;	char bitstring[256];&lt;br /&gt;	memset(bitstring, 0, 256);&lt;br /&gt;	unsigned int *input= (unsigned int *)in;&lt;br /&gt;	for(int i=31;i&gt;=0;i--)&lt;br /&gt;	{&lt;br /&gt;		if (((*input &gt;&gt; i) &amp; 1)) result = '1';&lt;br /&gt;		else result = '0';&lt;br /&gt;		bitstring[pos] = result;&lt;br /&gt;		if ((i&gt;0) &amp;&amp; ((i)%4)==0)&lt;br /&gt;		{&lt;br /&gt;			pos++;&lt;br /&gt;			bitstring[pos] = ' ';&lt;br /&gt;		}&lt;br /&gt;		pos++;&lt;br /&gt;	}&lt;br /&gt;	return bitstring;&lt;br /&gt;}&lt;br /&gt;int main(int argc, char* argv[])&lt;br /&gt;{&lt;br /&gt;	int i=53003;&lt;br /&gt;	char buffer[1024];&lt;br /&gt;	char *s=getBufferAsBinaryString(&amp;i);&lt;br /&gt;	strcpy(buffer, s);&lt;br /&gt;	printf("%s\n", buffer);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;produces this output:&lt;br /&gt;0000 0000 0000 0000 1100 1111 0000 1011</description>
      <pubDate>Thu, 18 May 2006 20:54:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2076</guid>
      <author>ocorpening (Owen Corpening)</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>Csharp formatting an integer as a binary string representation</title>
      <link>http://snippets.dzone.com/posts/show/2071</link>
      <description>// description of your code here&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.ConvertByteToBinaryString(i));&lt;br /&gt;		}&lt;br /&gt;		/// &lt;summary&gt;&lt;br /&gt;		/// Format integer as a binary string with spaces between every 4 bits&lt;br /&gt;		/// &lt;/summary&gt;&lt;br /&gt;		/// &lt;param name="input"&gt;integer to format&lt;/param&gt;&lt;br /&gt;		/// &lt;returns&gt;string of the form "0000 0000 0000 0000 0000 0110 0001 1000"&lt;/returns&gt;&lt;br /&gt;		private string ConvertByteToBinaryString(long input)&lt;br /&gt;		{&lt;br /&gt;			string result = null;&lt;br /&gt;			string bitstring = null;&lt;br /&gt;			for(int i=31;i&gt;=0;i--)&lt;br /&gt;			{&lt;br /&gt;				if (((input &gt;&gt; i) &amp; 1) == 1) result = "1";&lt;br /&gt;				else result = "0";&lt;br /&gt;				bitstring += result;&lt;br /&gt;				if ((i&gt;0) &amp;&amp; ((i)%4)==0)&lt;br /&gt;				{&lt;br /&gt;					bitstring += " ";&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;			return bitstring;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;produces this output:&lt;br /&gt;0000 0000 0000 0000 0000 0000 0000 1100</description>
      <pubDate>Thu, 18 May 2006 20:20:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2071</guid>
      <author>ocorpening (Owen Corpening)</author>
    </item>
    <item>
      <title>Csharp bitfields</title>
      <link>http://snippets.dzone.com/posts/show/2070</link>
      <description>// C# does not provide the ability to set a field within a byte to a value. &lt;br /&gt;// It has the ability to set individual bits, but if you are accessing hardware &lt;br /&gt;// you may need to set fields within bytes - no way jose. Until now!&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// SetField is used to set a certain set of bits to the value specified.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="input"&gt;byte in which to set the field&lt;/param&gt;&lt;br /&gt;/// &lt;param name="value"&gt;value to set field to&lt;/param&gt;&lt;br /&gt;/// &lt;param name="location"&gt;bit number within byte where field begins&lt;/param&gt;&lt;br /&gt;/// &lt;param name="noBits"&gt;field size&lt;/param&gt;&lt;br /&gt;private void SetField(ref long input, long value, byte location, int noBits)&lt;br /&gt;{&lt;br /&gt;	int size = 1;&lt;br /&gt;	if ((location &gt; 31) || (location &lt; 0))&lt;br /&gt;		throw new Exception("location out of range: " + location);&lt;br /&gt;	if ((noBits &gt; 31) || (noBits &lt;= 0))&lt;br /&gt;		throw new Exception("noBits out of range: " + noBits);&lt;br /&gt;	if (noBits &gt; 1) size = size &lt;&lt; noBits;&lt;br /&gt;	if ((value &lt; 0) || (value &gt; size))&lt;br /&gt;		throw new Exception("value out of range: " + value);&lt;br /&gt;	value = value &lt;&lt; location;&lt;br /&gt;	if (value &lt; 0)&lt;br /&gt;		throw new Exception("value out of range after shifting to field location: " + value);&lt;br /&gt;	input = (input | value);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Test:&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 = 2; // &lt;br /&gt;			Console.WriteLine(t.ConvertByteToBinaryString(i));&lt;br /&gt;			t.SetField(ref i,5,8,4);&lt;br /&gt;			Console.WriteLine(t.ConvertByteToBinaryString(i));&lt;br /&gt;		}&lt;br /&gt;		/// &lt;summary&gt;&lt;br /&gt;		/// SetField is used to set a certain set of bits to the value specified.&lt;br /&gt;		/// &lt;/summary&gt;&lt;br /&gt;		/// &lt;param name="input"&gt;byte in which to set the field&lt;/param&gt;&lt;br /&gt;		/// &lt;param name="value"&gt;value to set field to&lt;/param&gt;&lt;br /&gt;		/// &lt;param name="location"&gt;bit number within byte where field begins&lt;/param&gt;&lt;br /&gt;		/// &lt;param name="noBits"&gt;field size&lt;/param&gt;&lt;br /&gt;		private void SetField(ref long input, long value, byte location, int noBits)&lt;br /&gt;		{&lt;br /&gt;			int size = 1;&lt;br /&gt;			if ((location &gt; 31) || (location &lt; 0))&lt;br /&gt;				throw new Exception("location out of range: " + location);&lt;br /&gt;			if ((noBits &gt; 31) || (noBits &lt;= 0))&lt;br /&gt;				throw new Exception("noBits out of range: " + noBits);&lt;br /&gt;			if (noBits &gt; 1) size = size &lt;&lt; noBits;&lt;br /&gt;			if ((value &lt; 0) || (value &gt; size))&lt;br /&gt;				throw new Exception("value out of range: " + value);&lt;br /&gt;			value = value &lt;&lt; location;&lt;br /&gt;			if (value &lt; 0)&lt;br /&gt;				throw new Exception("value out of range after shifting to field location: " + value);&lt;br /&gt;			input = (input | value);&lt;br /&gt;		}&lt;br /&gt;		/// &lt;summary&gt;&lt;br /&gt;		/// Format integer as a binary string with spaces between every 4 bits&lt;br /&gt;		/// &lt;/summary&gt;&lt;br /&gt;		/// &lt;param name="input"&gt;integer to format&lt;/param&gt;&lt;br /&gt;		/// &lt;returns&gt;string of the form "0000 0000 0000 0000 0000 0110 0001 1000"&lt;/returns&gt;&lt;br /&gt;		private string ConvertByteToBinaryString(long input)&lt;br /&gt;		{&lt;br /&gt;			string result = null;&lt;br /&gt;			string bitstring = null;&lt;br /&gt;			for(int i=31;i&gt;=0;i--)&lt;br /&gt;			{&lt;br /&gt;				if (((input &gt;&gt; i) &amp; 1) == 1) result = "1";&lt;br /&gt;				else result = "0";&lt;br /&gt;				bitstring += result;&lt;br /&gt;				if ((i&gt;0) &amp;&amp; ((i)%4)==0)&lt;br /&gt;				{&lt;br /&gt;					bitstring += " ";&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;			return bitstring;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Output from test:&lt;br /&gt;C:\&gt;testApp.exe&lt;br /&gt;0000 0000 0000 0000 0000 0000 0000 0010&lt;br /&gt;0000 0000 0000 0000 0000 0101 0000 0010&lt;br /&gt;C:\&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 18 May 2006 20:08:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2070</guid>
      <author>ocorpening (Owen Corpening)</author>
    </item>
  </channel>
</rss>
