Thread: Simple way to store a 64 bit unsigned long into four unsigned short with union

  1. #1
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66

    Simple way to store a 64 bit unsigned long into four unsigned short with union

    Hi guys!

    Here is a simple program what shows how to store a 8 byte long unsigned long into 4 unsigned short:
    first the header lintunion.h:
    Code:
    #ifndef LONGINTUNION_H_INCLUDED
    #define LONGINTUNION_H_INCLUDED
    
    #include <iostream>
    #include <iomanip>
    
    union einheit
     {
      unsigned long ulwert;
      unsigned char mwertbytes[8];
      unsigned short mwertushort[4];
     };
    
     void buzubin(unsigned int letter, char wort[]);
     void zeigeWerte(union einheit ehe);
     void dectoshort( unsigned short zahl, char zudi[], short vornullen);
    
    #endif // FLOATINTUNION_H_INCLUDED
    now lintunion.cpp
    Code:
    #include "lintunion.h"
    
    /* -----------------------------------------------------------------------
     * buzubin converts a decimal value(only 0 to 255) given by argument 1
     * in to a binary value shown as string. This string is stored in argument 2.
     * The sting shows all eight bit of the value in argument 1.
     * the other bytes is for to terminate the string
     * syntaxc:  buzubin( char-value_as_int, Binaer_value_as_string);
     */
    
    void buzubin(unsigned int letter, char wort[])
    {
    unsigned int slei, dum, testwert;
     if ((letter < 0) || (letter > 255)) goto finito;
    
     /* Ininitalisert die 8 Binärstellen mit Null */
     for (slei = 0; slei <= 11; slei++)
      wort[8] = '\0';
    
     for (slei = 0; slei <= 7; slei++)
      {
       testwert = 128 >> slei;  /* Bit7(128dez) ist auf Char-Nr.0 */
       dum = letter & testwert;
       if (dum != 0)  wort[slei] = '1';
        else          wort[slei] = '0';
      }
    finito:    ;
    }
    
    // shows all values
    void zeigeWerte(union einheit ehe)
    {
     int i;
     char binwort[15], longwort[100];
    
     //Werte von unsigned char zeigen
      std::cout << "unsigned long value: " << ehe.ulwert << "     binary: "; 
    
      for (i = 7; i >= 0; i--)
       {
        buzubin(ehe.mwertbytes[i], binwort);
        std::cout << binwort << " ";
       }
       std::cout <<std::endl;    
     std::cout <<"Now as unsigned char: " << std::endl;
     for (i = 7; i >= 0; i--)
      {
       buzubin(ehe.mwertbytes[i], binwort);
       std::cout << "mwertbytes[" << i << "] = " << std::setw(3) << (int)ehe.mwertbytes[i] << "   binary: " <<  binwort << std::endl;
      }
     //Werte von unsigned char zeigen
     std::cout << std::endl << "Nun als unsigned short: " << std::endl;
     for (i = 3; i >= 0; i--)
      {
       dectoshort( (unsigned long)ehe.mwertushort[i], longwort, 1);
       std::cout << "mwertshort[" << i << "] = " << std::setw(6) << (int)ehe.mwertushort[i] << "   binary: " << longwort <<std::endl;
      } 
     std::cout << "---------------------------------------------------------" << std::endl;
    }
    
    /** converts arg 1(unsigned short decimal)
      * in a char-string (argument2), if argument 3(with leading zeros) 0 
      * the leading Zerows will not shown in sthe string, else
      * with leading zeros 
      */
    void dectoshort( unsigned short zahl, char zudi[], short vornullen)
    {
    char wort[35], worz[35];
    int slei, sleib;
    unsigned short dum2, testwert, kon = 1;
    // String vorbereiten
    for (slei = 0; slei <= 34; slei++)
    wort[slei] = worz[slei] = '\0';
    
    kon <<= 15;
    
    for (slei = 0; slei <= 15; slei++)
      {
       testwert = kon >> slei;  /* Bit30(1073741824dez) is char number 0 */
       dum2 = zahl & testwert;
       if (dum2)  wort[slei] = '1';
        else     wort[slei] = '0';
      }
    // begin to set ende of leading zeros
    for (slei = 0; slei <= 15; slei++)
     if (wort[slei] == '1') break;
    // wort ist mit Vornullen, worz ohne Vornullen
     for (sleib = 0; sleib <= 32; sleib++)
            {
             worz[sleib] = wort[slei];
             if (wort[slei] == '\0')
              break;
             slei++;
            }
    // take result in to argument 2 
     for (slei = 0; slei <= 15; slei++)
      if ( vornullen == 0)
       zudi[slei] = worz[slei]; /* if arg 3(vornullen) = 0 string with no leading zeros */
        else
         zudi[slei] = wort[slei]; // if arg 3 = 1 then with leading zeros
    
    }
    here main.cpp:
    Code:
    #include <iostream>
    #include "lintunion.h"
    
    
    int main()
    {
     union einheit flint;
    
     std::cout << "Test" << std::endl;
     std::cout << "length of long int: " << sizeof(long int) << " byte" << std::endl;
     std::cout << "length of short...: " << sizeof(short) << " byte"  << std::endl << std::endl;
    
     flint.ulwert = 1234567890;
    
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     flint.ulwert = 0;
    
     std::cout << std::endl <<"Now all variables are deleted:" << std::endl;
    
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     flint.mwertushort[0] = 59861;
     flint.mwertushort[1] = 17142;
     flint.mwertushort[2] = 9861;
     flint.mwertushort[3] = 7142;
    
     #include <iostream>
    #include "lintunion.h"
    
    
    int main()
    {
     union einheit flint;
    
     std::cout << "Test" << std::endl;
     std::cout << "length of long int: " << sizeof(long int) << " byte" << std::endl;
     std::cout << "length of short...: " << sizeof(short) << " byte"  << std::endl << std::endl;
    
     flint.ulwert = 1234567890;
    
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     flint.ulwert = 0;
    
     std::cout << std::endl <<"Now all variables are deleted:" << std::endl;
    
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     flint.mwertushort[0] = 59861;
     flint.mwertushort[1] = 17142;
     flint.mwertushort[2] = 9861;
     flint.mwertushort[3] = 7142;
    
      //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     return EXIT_SUCCESS;
    }
    
    
     //Werte von unsigned char zeigen
     zeigeWerte(flint);
    
     return EXIT_SUCCESS;
    }
    output on screen:
    Code:
    Test
    length of long int: 8 byte
    length of short...: 2 byte
    
    unsigned long value: 1234567890     binary: 00000000 00000000 00000000 00000000 01001001 10010110 00000010 11010010 
    Now as unsigned char: 
    mwertbytes[7] =   0   binary: 00000000
    mwertbytes[6] =   0   binary: 00000000
    mwertbytes[5] =   0   binary: 00000000
    mwertbytes[4] =   0   binary: 00000000
    mwertbytes[3] =  73   binary: 01001001
    mwertbytes[2] = 150   binary: 10010110
    mwertbytes[1] =   2   binary: 00000010
    mwertbytes[0] = 210   binary: 11010010
    
    Nun als unsigned short: 
    mwertshort[3] =      0   binary: 0000000000000000
    mwertshort[2] =      0   binary: 0000000000000000
    mwertshort[1] =  18838   binary: 0100100110010110
    mwertshort[0] =    722   binary: 0000001011010010
    ---------------------------------------------------------
    
    Now all variables are deleted:
    unsigned long value: 0     binary: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
    Now as unsigned char: 
    mwertbytes[7] =   0   binary: 00000000
    mwertbytes[6] =   0   binary: 00000000
    mwertbytes[5] =   0   binary: 00000000
    mwertbytes[4] =   0   binary: 00000000
    mwertbytes[3] =   0   binary: 00000000
    mwertbytes[2] =   0   binary: 00000000
    mwertbytes[1] =   0   binary: 00000000
    mwertbytes[0] =   0   binary: 00000000
    
    Nun als unsigned short: 
    mwertshort[3] =      0   binary: 0000000000000000
    mwertshort[2] =      0   binary: 0000000000000000
    mwertshort[1] =      0   binary: 0000000000000000
    mwertshort[0] =      0   binary: 0000000000000000
    ---------------------------------------------------------
    
    Jetzt Zuweisung von Werten an short-Variable:
    unsigned long value: 2010336637463488981     binary: 00011011 11100110 00100110 10000101 01000010 11110110 11101001 11010101 
    Now as unsigned char: 
    mwertbytes[7] =  27   binary: 00011011
    mwertbytes[6] = 230   binary: 11100110
    mwertbytes[5] =  38   binary: 00100110
    mwertbytes[4] = 133   binary: 10000101
    mwertbytes[3] =  66   binary: 01000010
    mwertbytes[2] = 246   binary: 11110110
    mwertbytes[1] = 233   binary: 11101001
    mwertbytes[0] = 213   binary: 11010101
    
    Nun als unsigned short: 
    mwertshort[3] =   7142   binary: 0001101111100110
    mwertshort[2] =   9861   binary: 0010011010000101
    mwertshort[1] =  17142   binary: 0100001011110110
    mwertshort[0] =  59861   binary: 1110100111010101
    ---------------------------------------------------------
    Have much fun to work with them!

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Trivial, but your code has several errors (exemple: shifting a signed integer left) and since this is a C language programming forum, it is completely wrong...

    Here's a more direct way to convert a byte to ascii binary representation:
    Code:
    #include <stdio.h>
    
    char *bin2ascii_char( unsigned char ch )
    {
      static char buffer[9];
      char*p;
      int count = 8;
    
      // Notice ch is 'unsigned char', so right shift is well defined by ISO 9989
      p = buffer;
      while ( count-- )
        *p++ = '0' + ( ( ch >> count ) & 1 );
      *p = '\0';
    
      return buffer;
    }
    /* Or:
    char *bin2ascii_char( char ch )
    {
      static char buffer[9];
      char *p;
      int count;
    
      p = buffer;
      count = 8;
      while ( count-- )
      {
        *p++ = '0' + ( ch < 0 );
    
        // left shifts for signed types are well defined.
        ch <<= 1;
      }
    
      *p = '\0';
    
      return buffer;
    }
    */
    
    int main( void )
    {
      printf( "%d -> 0b%s\n", 240, bin2ascii_char( 240 ) );
    }
    Of course, assuming 'char' is byte sized.
    Last edited by flp1969; 02-21-2020 at 12:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 07:43 PM
  2. Replies: 7
    Last Post: 02-08-2008, 06:31 PM
  3. Short/long/signed/unsigned int
    By scuzzo84 in forum C Programming
    Replies: 4
    Last Post: 09-13-2005, 11:40 PM
  4. Converting an unsigned int to to unsigned long long (64 bit)
    By ninjacookies in forum C Programming
    Replies: 18
    Last Post: 02-11-2005, 12:09 PM
  5. Replies: 8
    Last Post: 05-13-2003, 02:47 PM

Tags for this Thread