#include <iostream> #include "string.h" using namespace std; typedef unsigned long long uint64; typedef unsigned int uint32; typedef unsigned short uint16; typedef unsigned char uint8; typedef long long int64; typedef int int32; typedef short int16; typedef char int8; #define CAN_BIG_ENDIAN 0 #define CAN_LITTLE_ENDIAN 1 #define LSHIFT64(x,s) \ x = (s > 32) ? ((uint64)x << 32) : x; \ x = (s > 32) ? ((uint64)x << (s-32)) : (x << s); #define CANGETSIGNAL(x,t,m,mlen,sb,bl,be,berror) \ uint8 __iLen=bl; \ uint16 __iBit=sb; \ uint8 __iMod;\ uint8 __iL;\ uint8 __iIndex;\ berror=0; \ memset(&x,0,sizeof(x));\ while ( __iLen > 0 && berror == 0)\ {\ __iMod=(__iBit % 8);\ __iL=8-__iMod;\ if (__iL>__iLen) __iL=__iLen;\ t=(((0x1 << __iL)-1) << __iMod);\ __iIndex = __iBit >> 3;\ if ( __iIndex >= mlen && berror == 0 )\ {\ berror = 1;\ } else {\ t=(t & m[__iBit >> 3]) >> __iMod;\ LSHIFT64(t,(bl-__iLen));\ x |= t;\ __iLen-=__iL;\ __iBit-=__iMod;\ __iBit+=(be == CAN_LITTLE_ENDIAN) ? 8 : -8;\ }\ } uint64 CANConvertSignalToUInt64(uint8 * msg, uint32 msg_len, uint16 sBit, uint8 bLen, uint8 bEnc) { uint64 x, t; uint8 iError; CANGETSIGNAL(x, t, msg,msg_len, sBit, bLen, bEnc,iError); if (iError!=0) { //LogError("Can signal decoding failed, msg id: 0x" << hex << uppercase << (*msg) << ", payload: " << (int)msg_len << ", startbit: " << (int)sBit << ", len: " << (int)bLen << ", encode: " << (int)bEnc); } return x; } int64 CANConvertSignalToInt64(uint8 * msg, uint32 msg_len, uint16 sBit, uint8 bLen, uint8 bEnc) { int64 ret; uint64 x = CANConvertSignalToUInt64(msg,msg_len, sBit, bLen, bEnc); memcpy(&ret, &x, sizeof(ret)); uint64 iSignMask=0x1; LSHIFT64(iSignMask,bLen); // -- Is Signed (check msb bit) -- if ((ret & iSignMask) != 0) { uint64 mask; memset(&mask, 0xFF, sizeof(mask)); if (bLen == 64) { mask = 0; } else { LSHIFT64(mask,bLen); } ret |= mask; } return ret; } int main() { uint64 iRawValue; uint8 data[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }; const auto mMuxStartBit = 56; const auto mMuxLength = 8; const auto mMuxByteOrder = CAN_BIG_ENDIAN; uint64 beVal = CANConvertSignalToInt64(data, 8, mMuxStartBit, mMuxLength, CAN_BIG_ENDIAN); uint64 leVal = CANConvertSignalToInt64(data, 8, mMuxStartBit, mMuxLength, CAN_LITTLE_ENDIAN); cout << hex << uppercase << "Big: 0x" << beVal << " Little: 0x" << leVal << endl; { uint8 data[8] = { 0x02, 0x00, 0x05, 0xFF, 0xFF, 0xB0, 0x26, 0x14 }; uint64 val = CANConvertSignalToInt64(data, 8, 56, 8, CAN_LITTLE_ENDIAN); cout << hex << uppercase << val << endl; } return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}