Thread: Characters to binary... hexadecimal to binary

  1. #46
    I did some research, and only c++ has unions and single-bit data structures. Now, through the demonic ingenuity of my thought processes, i recalled how i did the bit access.

    Code:
    union char_bin_hex {  //edit: changed struct to union
      char ch;
      union char_to_bin bits;
      union char_to_hex dibits;
    }
    
    struct char_to_bin {  //edit:  changed union to struct
      unsigned b1: 1
      unsigned b2: 1
      unsigned b3: 1
      unsigned b4: 1
      unsigned b5: 1
      unsigned b6: 1
      unsigned b7: 1
      unsigned b8: 1
    }
    
    struct char_to_hex {  //edit: changed union to struct
      unsigned d1: 2
      unsigned d2: 2
      unsigned d3: 2
      unsigned d4: 2
    }
    All you do is put the char into char_bin_hex.ch and access the individual bits/dibits through char_bin_hex.bits.b1 etc and char_bin_hex.dibits.d1 etc.

    Hope this helps! I will work on a function tht works with this when iget back frm church & lunch.

    ~Inquirer
    Last edited by Inquirer; 10-27-2002 at 03:47 PM.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  2. #47
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Inquirer
    I did some research, and only c++ has unions and single-bit data structures. Now, through the demonic ingenuity of my thought processes, i recalled how i did the bit access.

    Code:
    struct char_bin_hex {
      char ch;
      union char_to_bin bits;
      union char_to_hex dibits;
    }
    
    union char_to_bin {
      unsigned b1: 1
      unsigned b2: 1
      unsigned b3: 1
      unsigned b4: 1
      unsigned b5: 1
      unsigned b6: 1
      unsigned b7: 1
      unsigned b8: 1
    }
    
    union char_to_hex {
      unsigned d1: 2
      unsigned d2: 2
      unsigned d3: 2
      unsigned d4: 2
    }
    All you do is put the char into char_bin_hex.ch and access the individual bits/dibits through char_bin_hex.bits.b1 etc and char_bin_hex.dibits.d1 etc.

    Hope this helps! I will work on a function tht works with this when iget back frm church & lunch.

    ~Inquirer
    Is it just me or did you get that a little backwards? The unions should be structures, and the structure should be a union. Or else i'm an idiot. (Then again i've never done anything like that before)

  3. #48

    OOPS!

    Yeah! You're right!

    I am editing that now, so don't get confused by eibro's post guys...

    Now i can actually make the function correctly!
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  4. #49

    /split

    ::: see my new thread :::
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. Reading/Writing in Binary
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2001, 10:32 PM
  5. inserting characters into a binary tree
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-06-2001, 04:08 PM