Click here to Skip to main content
15,938,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I will be reading 8 hexadecimal data to an array and i need to store it to data in a class
can anyone tell me how can i read and store it.

data is in the following manner
0.002893 1 201 Rx d 8 06 0D 00 B0 89 00 0D E7

my function is

void CANReader::readData(std::istream& is, CANReader& data)
{
std::string line;
std::getline(is, line);
std::istringstream iss(line);

iss >> data.Time;
iss >> data.State;
iss >> data.ID;
iss >> data.Tx_Rx_State;
iss >> data.Type;
iss >> data.Byte_info;
iss >> data.message;


}

What I have tried:

i am unable to get exact method
Posted
Updated 5-Dec-17 4:00am

thank you so much for the guidence and help
 
Share this answer
 
Comments
Patrice T 5-Dec-17 12:43pm    
Use button "Have a Question or Comment?" to talk with solution author.
Advantage: author gets notification.
if you want to store the numbers (for instance into an array of bytes) represented by the hexadecimal strings then try:
C++
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdint>
#include <array>
#include <exception>
using namespace std;


uint8_t hex(const string & str)
{
  uint8_t b = 0;
  for (size_t n=0; n<2; ++n)
  {
    b <<= 4;
    char c = str.at(n);
    if ( c >= '0' && c <= '9')
      b |= static_cast<uint8_t>(c-'0');
    else if ( c >= 'A' && c <= 'F')
      b |= static_cast<uint8_t>(c-'A'+0xA);
    else if ( c >= 'a' && c <= 'f')
      b |= static_cast<uint8_t>(c-'a'+0xA);
    else
      throw(invalid_argument("not an hex string"));
  }
  return b;
}

int main()
{
  string line = "06 0D 00 B0 89 00 0D E7";

  istringstream iss(line);

  array<uint8_t, 8> data;

  for (auto & d : data)
  {
    string s;
    iss >> s;
    d = hex(s);
  }

  for (const auto & d : data)
    cout << static_cast<unsigned int>(d) << endl;
}


[update]
Following Jochen suggestion, there is no need for manula hex conversion:
C++
#include <iostream>
#include <sstream>
#include <cstdint>
#include <array>
#include <exception>
using namespace std;

int main()
{
  string line = "06 0D 00 B0 89 00 0D E7";

  istringstream iss(line);

  array<uint8_t, 8> data;

  for (auto & d : data)
  {
    size_t pos = 0;
    string s;
    iss >> s;
    d = static_cast<uint8_t>( stoul(s, &pos, 16));
    if ( pos != 2)
       throw(invalid_argument("not a byte hexadecimal representation"));
  }

  for (const auto & d : data)
    cout << static_cast<unsigned int>(d) << endl;
}


[/update]
 
Share this answer
 
v2
Comments
Jochen Arndt 5-Dec-17 4:33am    
Using std::stoul() (or std::strtoul for pre C++11) avoids doing the conversion "manually". Invalid input can be detected with the pos parameter and the return value can be casted to uint8_t after checking for overflow.
CPallini 5-Dec-17 6:29am    
Yes, thank you for pointing it out.
Richard MacCutchan 5-Dec-17 6:04am    
Nice code; something more I have learned about C++.
CPallini 5-Dec-17 6:31am    
Modern C++ language features are nice, indeed.
Richard MacCutchan 5-Dec-17 6:34am    
Being a dinosaur means I find them useful rather than nice. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900