<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The ILNumerics Blog &#187; unsafe</title>
	<atom:link href="https://ilnumerics.net/blog/tag/unsafe/feed/" rel="self" type="application/rss+xml" />
	<link>https://ilnumerics.net/blog</link>
	<description>The Productivity Machine  &#124;  A fresh attempt for scientific computing  &#124;  http://ilnumerics.net</description>
	<lastBuildDate>Thu, 05 Dec 2024 09:09:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>Uncommon data conversion with ILArray</title>
		<link>https://ilnumerics.net/blog/uncommon-data-conversion-with-ilarray/</link>
		<comments>https://ilnumerics.net/blog/uncommon-data-conversion-with-ilarray/#comments</comments>
		<pubDate>Tue, 07 Oct 2014 08:42:14 +0000</pubDate>
		<dc:creator><![CDATA[haymo]]></dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[ILNumerics]]></category>
		<category><![CDATA[Usage]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[ILArray]]></category>
		<category><![CDATA[pointer arithmetic]]></category>
		<category><![CDATA[unsafe]]></category>
		<category><![CDATA[ushort]]></category>

		<guid isPermaLink="false">http://ilnumerics.net/blog/?p=635</guid>
		<description><![CDATA[<p>ILNumerics Computing Engine supports the most common numeric data types out of the box: double, float, complex, fcomplex, byte, short, int, long, ulong If you need to convert from, let&#8217;s say ushort to float, you will not find any prepared conversion function in ILMath. Luckily, it is very easy to write your own: Here comes &#8230; <a href="https://ilnumerics.net/blog/uncommon-data-conversion-with-ilarray/" class="more-link">Continue reading <span class="screen-reader-text">Uncommon data conversion with ILArray</span> <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/uncommon-data-conversion-with-ilarray/">Uncommon data conversion with ILArray</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>ILNumerics Computing Engine supports the most common numeric data types out of the box: double, float, complex, fcomplex, byte, short, int, long, ulong</p>
<p>If you need to convert from, let&#8217;s say ushort to float, you will not find any prepared conversion function in ILMath. Luckily, it is very easy to write your own:</p>
<p>Here comes a method which implements the conversion from ushort -&gt; float. A straight forward version first:</p>
<pre class="brush: csharp; title: ; notranslate">
        /// &lt;summary&gt;
        /// Convert ushort data to ILArray&amp;lt;float&gt;
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;A&quot;&gt;Input Array&lt;/param&gt;
        /// &lt;returns&gt;Array of the same size as A, single precision float elements&lt;/returns&gt;
        public static ILRetArray&lt;float&gt; UShort2Single(ILInArray&lt;ushort&gt; A) {
            using (ILScope.Enter(A)) {
                ILArray&lt;float&gt; ret = ILMath.zeros&lt;float&gt;(A.S);
                var retArr = ret.GetArrayForWrite();
                var AArr = A.GetArrayForRead();
                int c = 0;
                foreach (ushort a in A) {
                    retArr[c++] = a;
                }
                return ret;
            }
        }</pre>
<p><span id="more-635"></span>This method is used like that:</p>
<pre class="brush: csharp; title: ; notranslate">
            ushort[,] rawSensorData = new ushort[,] {{0,1,2},{3,4,5}};
            ILArray&lt;float&gt; converted = UShort2Single(rawSensorData);
            /*
             * &lt;Single&gt; [3,2]
             * [0]:          0          3
             * [1]:          1          4
             * [2]:          2          5
             */

            // continue working with 'converted' here...
</pre>
<p>The following method does the same but utilizes pointer arithmetic, hence it needs the /unsafe flag. Use this, if performance is critical and your data are sufficiently large:</p>
<pre class="brush: csharp; title: ; notranslate">
        /// &lt;summary&gt;
        /// Convert ushort data to ILArray&amp;lt;float&gt; (unsafe version)
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;A&quot;&gt;Input Array&lt;/param&gt;
        /// &lt;returns&gt;Array of the same size as A, single precision float elements&lt;/returns&gt;
        public unsafe static ILRetArray&lt;float&gt; UShort2SingleUnsafe(ILInArray&lt;ushort&gt; A) {
            using (ILScope.Enter(A)) {
                ILArray&lt;float&gt; ret = ILMath.zeros&lt;float&gt;(A.S);
                var retArr = ret.GetArrayForWrite();
                var AArr = A.GetArrayForRead();

                fixed (ushort* pAArr = AArr)
                fixed (float* pRetArr = retArr) {
                    ushort* pInWalk = pAArr;
                    ushort* pInEnd = pAArr + A.S.NumberOfElements;
                    float* pRetWalk = pRetArr;
                    while (pInWalk &lt; pInEnd) {
                        *(pRetWalk++) = /*implicit: (float)*/ (*(pInWalk++));
                    }
                }
                return ret;
            }
        }</pre>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/uncommon-data-conversion-with-ilarray/">Uncommon data conversion with ILArray</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://ilnumerics.net/blog/uncommon-data-conversion-with-ilarray/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
