www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Byte Order Swapping Function

reply Andrew Wiley <wiley.andrew.j gmail.com> writes:
--000e0cd48e04e979fd04a801c309
Content-Type: text/plain; charset=ISO-8859-1

Hey, does anyone else thing a function like this belongs in Phobos, and if
so, where do you think it should go?

T ntoh(T)(T val) if (__traits(isArithmetic, T)) {
version(BigEndian) {
 return val;
}
else version (LittleEndian) {
 ubyte[] arr = (cast(ubyte*)&val)[0 .. T.sizeof];
ubyte temp;
for(int i = 0; i < T.sizeof/2; i++) {
 temp = arr[i];
arr[i] = arr[T.sizeof - i - 1];
arr[T.sizeof - i - 1] = temp;
 }
return val;
}
 else static assert(0, "Are you sure you're using a computer?");
 }

I was looking for something along these lines in the docs today and couldn't
find it. It looks like there's a stream in std.stream to do this, but, well,
I think we've all been pretending std.stream doesn't exist for a while now.

--000e0cd48e04e979fd04a801c309
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hey, does anyone else thing a function like this belongs in Phobos, and if =
so, where do you think it should go?<div><br></div><div><div>T ntoh(T)(T va=
l) if (__traits(isArithmetic, T)) {</div><div><span class=3D"Apple-tab-span=
" style=3D"white-space:pre">	</span>version(BigEndian) {</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>retu=
rn val;</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">=
	</span>}</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre=
">	</span>else version (LittleEndian) {</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>ubyt=
e[] arr =3D (cast(ubyte*)&amp;val)[0 .. T.sizeof];</div><div><span class=3D=
"Apple-tab-span" style=3D"white-space:pre">		</span>ubyte temp;</div><div><=
span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>for(int i =
=3D 0; i &lt; T.sizeof/2; i++) {</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">			</span>tem=
p =3D arr[i];</div><div><span class=3D"Apple-tab-span" style=3D"white-space=
:pre">			</span>arr[i] =3D arr[T.sizeof - i - 1];</div><div><span class=3D"=
Apple-tab-span" style=3D"white-space:pre">			</span>arr[T.sizeof - i - 1] =
=3D temp;</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>}</d=
iv><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>r=
eturn val;</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pr=
e">	</span>}</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>else =
static assert(0, &quot;Are you sure you&#39;re using a computer?&quot;);</d=
iv><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span></=
div>

<div>}</div></div><div><br></div><div>I was looking for something along the=
se lines in the docs today and couldn&#39;t find it. It looks like there&#3=
9;s a stream in std.stream to do this, but, well, I think we&#39;ve all bee=
n pretending std.stream doesn&#39;t exist for a while now.</div>


--000e0cd48e04e979fd04a801c309--
Jul 13 2011
parent Johannes Pfau <spam example.com> writes:
Andrew Wiley wrote:
Hey, does anyone else thing a function like this belongs in Phobos,
and if so, where do you think it should go?

T ntoh(T)(T val) if (__traits(isArithmetic, T)) {
version(BigEndian) {
 return val;
}
else version (LittleEndian) {
 ubyte[] arr = (cast(ubyte*)&val)[0 .. T.sizeof];
ubyte temp;
for(int i = 0; i < T.sizeof/2; i++) {
 temp = arr[i];
arr[i] = arr[T.sizeof - i - 1];
arr[T.sizeof - i - 1] = temp;
 }
return val;
}
 else static assert(0, "Are you sure you're using a computer?");
 }

I was looking for something along these lines in the docs today and
couldn't find it.

byte order and it only works for uint, though. -- Johannes Pfau
Jul 14 2011