To convert BYTE type data to a WORD type
-
Hi, I am receiving 60 BYTE type data to the serial port from an external device. I want to convert the 2 consequent received bytes each to a 16 bit word and store it in excel file? Does anyone have Idea , how to do this? For example, i received 60,25 consequently to the serial port. Now i have to store 6025 in the Excel sheet. Thanks, Please help me out to solve this problem. Chetan. Helping others satisfies you...
-
Hi, I am receiving 60 BYTE type data to the serial port from an external device. I want to convert the 2 consequent received bytes each to a 16 bit word and store it in excel file? Does anyone have Idea , how to do this? For example, i received 60,25 consequently to the serial port. Now i have to store 6025 in the Excel sheet. Thanks, Please help me out to solve this problem. Chetan. Helping others satisfies you...
Like this? (It's one way to do it, there are other ways as well) unsigned char serialBuffer[x]; // Assume serialBuffer contains two chars with "ASCII" value 60 and 25 (decimal). unsigned int word = (((unsigned int) serialBuffer[0]) * 100) + (unsigned int) serialBuffer[1]; // Now word is 6025. // if you want a char[] char chValue[10]; itoa(word, chValue, 10); // Now, chValue contains "6025"
-
Hi, I am receiving 60 BYTE type data to the serial port from an external device. I want to convert the 2 consequent received bytes each to a 16 bit word and store it in excel file? Does anyone have Idea , how to do this? For example, i received 60,25 consequently to the serial port. Now i have to store 6025 in the Excel sheet. Thanks, Please help me out to solve this problem. Chetan. Helping others satisfies you...
I assume that by 60 and 25, you mean the hex values 0x3c and 0x19. If the byte stream arriving is ordered as 0x3c, 0x19, ..., and you want to interpret that as 0x3c19, then you have a big endian stream:
typedef unsigned char byte;
typedef unsigned short word;const byte low = 0x19; // 25
const byte high = 0x3c; // 60
const word merged = (word(high) << 8) | low; // 0x3c19However, since you want to store it as 6025 in Excel, I assume that you by that mean the string "6025". Otherwise you need to state your need better. To get "6025", you can do this:
const char buffer[33];
const std::string left(_itoa_s(high, buffer, 33, 10));
const std::string right(_itoa_s(low, buffer, 33, 10));
const std::string merged(left + right);Note: this of course come without warranties, it's the concept that's important here. -- The Blog: Bits and Pieces