Joining 2 data (bytes) array

This is a discussion on Joining 2 data (bytes) array within the C Programming forums, part of the General Programming Boards category; Hi All, Hope you are all well. I would need some advise on data byte joining. I develop CAN interface ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    Joining 2 data (bytes) array

    Hi All,

    Hope you are all well.

    I would need some advise on data byte joining.

    I develop CAN interface software which receiving data frames from CanGateway hardware.

    Then 8 bytes data arrives regularly.

    Ex. Data 1 = 10 (0x0A)
    Data 2 = 20 (0x14)
    Data 3 = 30 (0x1E)
    Data 4 = 40 (0x28)
    Data 5 = 50 (0x32)
    Data 6 = 60 (0x3C)
    Data 7 = 70 (0X46)
    Data 8 = 80 (0x50)

    Data 4, 5 must be joined and interpret as engine rpm (for example). I have the PGN and I am able to unpacking each data byte.

    Questions

    How can we write the code to joined such array element (data4, 5) ?


    Below some program that I have,
    Code:
    case 61444: 
    { 
    int i; 
    printf("Databytes:"); 
    for (i = 0; i < rcvInfos->dataLen; i++) 
    printf("%lu ", rcvInfos->data[i]); 
    printf("actual engine torque : %d engine torque\n ",rcvInfos->data[4], data5]...???); 
    printf("\n"); 
    } 
    break;

    I appreaciate your advise and valuable insights !


    Thanks

    Su

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    26
    Use a larger data type to join the two bytes

    Code:
    unsigned char a=4;
    unsigned char b=5;
    unsigned short int c=0;
    c=a<<8;
    c+=b;
    printf("\nRPM=%d", c); //Prints 1029

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Here, since you've spammed >= 3 forums, lemme make it a bit easier for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

Tags for this Thread


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