concate two byte to get short int

This is a discussion on concate two byte to get short int within the C Programming forums, part of the General Programming Boards category; I want to write a function like for getting the unsigned short int value for two given bytes least and ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    Unhappy concate two byte to get short int

    I want to write a function like for getting the unsigned short int value for two given bytes least and most significant byte.
    Code:
    unsigned short int getIt(unsigned char leastSignificant, unsigned char mostSignificant)
    {
        unsigned short int result;
       /*
          Code for getting the value of result from joining the two bytes;
          i.e. result = concate mostSignificant & lastSignificant  to get the value */
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Code:
    #include <limits.h>
    ...
    {
      ...
      // Little endian version:
      return mostSignificant<<CHAR_BIT|leastSignificant;
    }
    Note: if I remember correctly you're not ensured that sizeof(short)=2; you only have sizeof(short)>=2 (to be checked).

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    134
    I m getting the return value always zero why?

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Code:
    #include <limits.h>
    #include <stdio.h>
    
    int main() {
            unsigned char msb=0xAA;
            unsigned char lsb=0x44;
            unsigned short v=msb<<CHAR_BIT|lsb;
            printf("&#37;x\n",v);
            return 0;
    }
    This code returns "aa44" as expected (on an Intel cpu), so have you done exactly?

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    134
    Code:
    #include <stdio.h>
    #include <limits.h>
    unsigned short int getPart(unsigned char first, unsigned char second,char start, char end)       {
            unsigned short int code;
            unsigned short int op = 65535;
            unsigned short int most = first;
            unsigned short int least = second;
            least=second;
            printf("\nMost Least %d %d %d\n",most,least,second);
            code = (most<<CHAR_BIT)|least;
            printf("\nCode %d ",code);
            code=code>>(15-end);
            printf("\nCode %d ",code);
            op=op>>start;
            code = code & op;
            return(code);
    }
    int main()      {
            unsigned char first = 0xFF, second=0xFF, start,end;
            while(1)        {
                    printf("\nEnter two value\n");
                    scanf("%d",&start);
                    scanf("%d",&end);
                    printf("\nValue is %d\n",getPart(first,second,start,end));
            }
    }
    Where is the error?
    I want to get the value of binary code represented by the concated bytes i.e the returned value value from start bit upto end bit
    Last edited by kapil1089thekin; 05-18-2008 at 01:32 PM.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Where is the error?
    In your initial scanf() calls...check the values read and you'll see they are not correct. So the following problems are irrelevant.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    134
    Code:
    #include <stdio.h>
    int main()      {
            unsigned char first = 0xFF, second=0xFF, start,end;
            scanf("&#37;d",&start);
            scanf("%d", &end);
            printf("\nstart end %d %d\n",start,end);
    }
    I m getting segmentation fault here??
    plz debug it for me.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    You are reading an integer (~4 bytes) into a char (~1 byte).
    Re-read the manual for scanf.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 06:26 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21