# TestByteSwap.cxx

This is a C++ example on how to use gdcm::ByteSwap

    /*=========================================================================

      Program: GDCM (Grassroots DICOM). A DICOM library
      Module:  $URL$

      Copyright (c) 2006-2009 Mathieu Malaterre
      All rights reserved.
      See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.

         This software is distributed WITHOUT ANY WARRANTY; without even
         the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
         PURPOSE.  See the above copyright notice for more information.

    =========================================================================*/
    #include "gdcmTypes.h"
    #include "gdcmSwapCode.h"
    #include "gdcmByteSwap.h"

    #include <string.h> // memcpy

    int myfunc()
    {
      char vl_str[4];
      const char raw[] = "\000\000\000\004";
      memcpy(vl_str, raw, 4);
      uint32_t vl;
      gdcm::ByteSwap<uint32_t>::SwapRangeFromSwapCodeIntoSystem((uint32_t*)(&vl_str), gdcm::SwapCode::BigEndian, 1);
      memcpy(&vl, vl_str, 4);
      if( vl != 0x00000004 )
        {
        std::cerr << std::hex << "vl: " << vl << std::endl;
        return 1;
        }

      gdcm::ByteSwap<uint32_t>::SwapFromSwapCodeIntoSystem(vl, gdcm::SwapCode::LittleEndian);
      if( vl != 0x00000004 )
        {
        std::cerr << std::hex << "vl: " << vl << std::endl;
        return 1;
        }

      gdcm::ByteSwap<uint32_t>::SwapFromSwapCodeIntoSystem(vl, gdcm::SwapCode::BigEndian);
      std::cout << std::hex << "vl: " << vl << std::endl;
      if( vl != 0x4000000 )
        {
        return 1;
        }

      return 0;
    }

    int TestByteSwap(int , char *[])
    {
      gdcm::SwapCode sc = gdcm::SwapCode::Unknown;
      if ( gdcm::ByteSwap<uint16_t>::SystemIsBigEndian() )
        {
        sc = gdcm::SwapCode::BigEndian;
        }
      else if ( gdcm::ByteSwap<uint16_t>::SystemIsLittleEndian() )
        {
        sc = gdcm::SwapCode::LittleEndian;
        }
      if( sc == gdcm::SwapCode::Unknown )
        {
        return 1;
        }

      std::cout << "sc: " << sc << std::endl;

      uint16_t t = 0x1234;
      gdcm::ByteSwap<uint16_t>::SwapFromSwapCodeIntoSystem(t, sc);
      if( sc == gdcm::SwapCode::BigEndian )
        {
        if( t != 0x3412 )
          {
          std::cerr << std::hex << "t: " << t << std::endl;
          return 1;
          }
        // ok test pass rest value to old one
        t = 0x1234;
        }
      else if ( sc == gdcm::SwapCode::LittleEndian )
        {
        if( t != 0x1234 )
          {
          std::cerr << std::hex << "t: " << t << std::endl;
          return 1;
          }
        }

      char n[2];
      memcpy(n, &t, 2 );
      gdcm::ByteSwap<uint16_t>::SwapRangeFromSwapCodeIntoSystem((uint16_t*)n, sc, 1);
      uint16_t tn = *((uint16_t*)n);
      if( sc == gdcm::SwapCode::BigEndian )
        {
        if( tn != 0x3412 )
          {
          std::cerr << std::hex << "tn: " << tn << std::endl;
          return 1;
          }
        // ok test pass rest value to old one
        t = 0x1234;
        }
      else if ( sc == gdcm::SwapCode::LittleEndian )
        {
        if( tn != 0x1234 )
          {
          std::cerr << std::hex << "tn: " << tn << std::endl;
          return 1;
          }
        }
      gdcm::ByteSwap<uint16_t>::SwapRangeFromSwapCodeIntoSystem((uint16_t*)n, gdcm::SwapCode::BigEndian, 1);
      tn = *((uint16_t*)n);
      if( sc == gdcm::SwapCode::LittleEndian )
        {
        if( tn != 0x3412 )
          {
          std::cerr << std::hex << "tn: " << tn << std::endl;
          return 1;
          }
        }
      else if ( sc == gdcm::SwapCode::BigEndian )
        {
        if( tn != 0x1234 )
          {
          std::cerr << std::hex << "tn: " << tn << std::endl;
          return 1;
          }
        }

      if( myfunc() )
        {
        return 1;
        }

      uint16_t array[] = { 0x1234 };
      gdcm::ByteSwap<uint16_t>::SwapRangeFromSwapCodeIntoSystem(array,
        gdcm::SwapCode::BigEndian,2);
       if ( array[0] != 0x3412 )
         {
         return 1;
         }

      return 0;
    }