Thread: char array output confusion

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    7

    char array output confusion

    Hello all, I'm pretty new to C, so this might be a silly question. Im doing some bitshifting in chunks of 8 bytes and outputting 7 bytes. For the output im using a char array of size 7 storing a single byte in each location. Here's where things deviate from what I expect: when I print the values at all 7 locations of my char array in hex, I get 32 bit results form each array index (which should only be ably to hold 8 bits). Why is this? Heres my relevant (and probably poorly formatted) code:
    Edit: buff[] is a char array as well, declared earlier in the code. The values it outputs are correct.
    Code:
                    char out[7];    
              
                    out[0] = ((buff[i] & 0x7f) << 1) | ((buff[i+1] & 0x40) >> 6);
                    printf("%x -> %x\n",buff[i],out[0]);
                    
                    out[1] = ((buff[i+1] & 0x3f) << 2) | ((buff[i+2] & 0x60) >> 5);
                    printf("%x -> %x\n",buff[i+1],out[1]);
    
    
                    
                    out[2] = ((buff[i+2] & 0x1f) << 3) | ((buff[i+3] & 0x70) >> 4);
                    printf("%x -> %x\n",buff[i+2],out[2]);
    
    
                    
                    out[3] = ((buff[i+3] & 0x0f) << 4) | ((buff[i+4] & 0x78) >> 3);
                    printf("%x -> %x\n",buff[i+3],out[3]);
        
                    
                    out[4] = ((buff[i+4] & 0x07) << 5) | ((buff[i+5] & 0x7c) >> 2);
                    printf("%x -> %x\n",buff[i+4],out[4]);
    
    
                    
                    out[5] = ((buff[i+5] & 0x03) << 6) | ((buff[i+6] & 0x7e) >> 1);
                    printf("%x -> %x\n",buff[i+5],out[5]);
                    
                    
                    out[6] = ((buff[i+6] & 0x01) << 7) | (buff[i+7] & 0x7f);
                    printf("%x -> %x\n",buff[i+6],out[6]);
    Heres some sample output
    hex on right is orig val from buff[] index hex on left is val of out[] index:
    61 -> ffffffc362 -> ffffff8b
    63 -> 1e
    64 -> 4c
    65 -> ffffffb9
    66 -> ffffffb3
    67 -> ffffffe8
    Why are the values on the right hand side 32 bits when each one should only represent a char (1 byte) and both are printed using hex %x?
    Last edited by npc93; 02-12-2017 at 01:45 PM. Reason: Clarify

  2. #2
    Registered User
    Join Date
    Feb 2017
    Posts
    7
    Solved the issue... I forgot to declare out[] as unsigned char instead of just char causing a touch of undefined behavior in terms of the values. Should I leave this post up for others to reference or remove it? New to the forums and want to abide by the rules/norms as it's been a friendly resource thus far :-)

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    This happens probably because a negative char( if the type is signed ) gets converted to its long 32-bit form. For example:
    Code:
    8-bit -1  : 0xFF
    32-bit -1 : 0xFFFFFFFF
    If you use "unsigned char" instead, the problem will likely disappear.

    EDIT: Good, you figured it out by yourself. Yes, please leave this post as it is for others to learn from your mistake.
    Last edited by GReaper; 02-12-2017 at 01:55 PM.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 01-01-2014, 06:25 AM
  2. Output confusion
    By return0 in forum C Programming
    Replies: 4
    Last Post: 02-12-2013, 10:19 PM
  3. array confusion (int vs char)
    By Shinzu911 in forum C Programming
    Replies: 1
    Last Post: 05-02-2012, 05:12 PM
  4. Help with char array output
    By chimaera523 in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2007, 12:14 AM
  5. array and char confusion
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-13-2004, 05:59 AM

Tags for this Thread