From kde-devel Wed Oct 27 12:29:31 2004 From: =?iso-8859-2?q?R=FCdiger_Kn=F6rig?= Date: Wed, 27 Oct 2004 12:29:31 +0000 To: kde-devel Subject: Re: Byte order conversions Message-Id: <200410271429.31772.ruediger () knoerig ! de> X-MARC-Message: https://marc.info/?l=kde-devel&m=109888158307486 MIME-Version: 1 Content-Type: multipart/mixed; boundary="--Boundary-00=_rS5fBp42tH7SQzL" --Boundary-00=_rS5fBp42tH7SQzL Content-Type: text/plain; charset="iso-8859-2" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Am Mittwoch, 27. Oktober 2004 14:42 schrieb Szombathelyi Gy=F6rgy: > Hello! > > Is there anyone who knows convenient byte-order conversion functions in > QT/kdelibs? If not, then what about adding some functions like > {be2me,le2me,me2be,me2le}_{16,32,64) (prefixed with the usual 'K')?. As > I browse through the code, every piece of code which requires this > functionality implements its own solution, usually with ugly macros and > #ifdef WORDS_BIGENDIAN conditionals. > > If it's ok, I'll volunteer for implementing (read: steal from somewhere > > :) ) these. > > Bye, > Gy=F6rgy > > >> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to > >> unsubscribe << The attachment contains a template class which will do the job. For automat= ic=20 conversion of custom datatypes the autoconf macros AC_C_BIGENDIAN(),=20 AC_CHECK_SIZE(short) and AC_CHECK_SIZE(int) have to be executed. If these macros exist you can convert from host endianess via: ConvertEndianess::convert(doubleval,Endian::BigEndian); or change the endianess via ConvertEndianess::swapEndianess(doubleval); --Boundary-00=_rS5fBp42tH7SQzL Content-Type: text/x-chdr; charset="iso-8859-2"; name="ConvertEndianess.h" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="ConvertEndianess.h" /*************************************************************************** * Copyright (C) 2003 by R=FCdiger Kn=F6rig = * * ruediger@knoerig.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * **************************************************************************= */ /** * @file ConvertEndianess.h * @brief This file provides a template class for endianess conversion. * @author Ruediger Knoerig */ #ifndef _ENDIANESSCONVERTER_TEMPLATE_CLASS_RUDI_2003_ #define _ENDIANESSCONVERTER_TEMPLATE_CLASS_RUDI_2003_ namespace Endian { /** endianess definition */ enum Endianess { LittleEndian=3D0, /**< little endian byte order (least significant byte = first) */ BigEndian=3D1, /**< big endian byte order (most significant byte first) = */ IgnoreEndian=3D2 /**< ignore endianess, don't do any conversion */ }; } /** * @brief utility class providing template byte order swapping. * @author Ruediger Knoerig */ template class ByteSwapper { public: /** * method doing the byte order swap * @param input input with byte order ABCD (for length-4-input) * @return output input with byte order DCBA (for length-4-input) */ static const T swapBytes(T input); }; /** * implementation of swap_bytes for generic lengths */ template const T ByteSwapper::swapBytes(T inp= ut) { T output; char *src =3D reinterpret_cast(&input); char *dst =3D reinterpret_cast(&output)+length; do { *(--dst) =3D *(src++); }while(dst !=3D reinterpret_cast(&out= put)); return output; } /** * @brief specialization of ByteSwapper for length=3D1. * @author Ruediger Knoerig */ template class ByteSwapper { public: /** * method doing the byte order swap for length-1 inputs * @param input length-1-input * @return output input */ static const T swapBytes(T input) { return input; } }; /** * @brief specialization of ByteSwapper for length=3D2. * @author Ruediger Knoerig */ #if SIZEOF_SHORT=3D=3D2 template class ByteSwapper { public: /** * method doing the byte order swap * The algorithm has been taken from http://www.pyrogon.com/poshlib/posh_= 8c-source.html. * @param input length-2-input AB * @return output input with byte order BA */ static const T swapBytes(T input); }; template const T ByteSwapper::swapBytes(T input) { unsigned short ip =3D *reinterpret_cast(&input); unsigned short op =3D ip >> 8; op |=3D (ip << 8); return *reinterpret_cast(&op); } #endif #if SIZEOF_INT=3D=3D4 /** * @brief specialization of ByteSwapper for length=3D4. * @author Ruediger Knoerig */ template class ByteSwapper { public: /** * method doing the byte order swap * The algorithm has been taken from http://www.pyrogon.com/poshlib/posh_= 8c-source.html. * @param input length-4-input ABCD * @return output input with byte order DCBA */ static const T swapBytes(T input); }; template const T ByteSwapper::swapBytes(T input) { unsigned int ip =3D *reinterpret_cast(&input); unsigned int op =3D ( ip & 0xFF ) << 24; op |=3D ( ip & 0xFF00 ) << 8; op |=3D ( ip >> 8 ) & 0xFF00; op |=3D ( ip >> 24 ); return *reinterpret_cast(&op); } #endif /** * @brief Template class for generic endianess conversion. * @author Ruediger Knoerig */ template class ConvertEndianess : public ByteSwapper< T,sizeof(T)> { public: /** constructor */ ConvertEndianess() {} /** destructor */ virtual ~ConvertEndianess() {} /** * method for inverting the byte order of the input * @param input input data with byte sequence ABCD * @return output output data with byte sequence DCBA */ static const T swapEndianess(T input) { return swapBytes(input); } /** * @brief method for converting the byte order of input from/to host endi= aness. * The host byte order is checked by the macro WORDS_BIGENDIAN, which in = turn * is set by AC_C_BIGENDIAN(). * @param input value to convert * @param endianess desired endianess @see Endian::Endianess * @return input input in new byte order */ static const T convert(T input,Endian::Endianess endianess); }; template const T ConvertEndianess::convert(T input,Endian::Endi= aness endianess) { #ifdef WORDS_BIGENDIAN return ((endianess=3D=3DEndian::LittleEndian) ? swapBytes(input) : input); #else return ((endianess=3D=3DEndian::BigEndian) ? swapBytes(input) : input); #endif } #endif --Boundary-00=_rS5fBp42tH7SQzL Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline >> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe << --Boundary-00=_rS5fBp42tH7SQzL--