Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. To convert BYTE type data to a WORD type

To convert BYTE type data to a WORD type

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chetan210183
    wrote on last edited by
    #1

    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...

    K J 2 Replies Last reply
    0
    • C chetan210183

      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...

      K Offline
      K Offline
      kakan
      wrote on last edited by
      #2

      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"

      1 Reply Last reply
      0
      • C chetan210183

        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...

        J Offline
        J Offline
        Johann Gerell
        wrote on last edited by
        #3

        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; // 0x3c19

        However, 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups