LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2015, 03:58 AM   #1
wowy
Member
 
Registered: Mar 2015
Location: France
Distribution: Ubuntu 14.04
Posts: 61

Rep: Reputation: Disabled
Function behave differently with similar input (C++)


I am trying to work with the modbus protocol and right now i am calculating the LRC of the messages. I made a function which worked with no issue whatever i was putting into and then i noticed that id did not worked with one input and i can't find a logical explanation on why this don't work.

The function is :

Code:
void LRCstring(std::string example)
{

std::stringstream ss;
std::string hex =example.substr(1, example.length()-5);
std::vector<unsigned char> hexCh;
unsigned int buffer;
int offset = 0;
while (offset < hex.length()) {
   ss.clear();
   ss << std::hex << hex.substr(offset, 2);
   ss >> buffer;
   hexCh.push_back(static_cast<unsigned char>(buffer));
   offset += 2;
}

unsigned char LRC=0x00;
int i;
for (i=0;i<hexCh.size();i++)
{
    LRC=LRC+hexCh[i];
}

LRC = 0xFF-LRC; // 1 complement
LRC = LRC+1; // 2 complement

//std::string s = std::to_string(LRC);

//int deci = atoi(s.c_str());
int deci = LRC;
int reste=deci  16;
std::string temp;
int partiehexa=(deci-reste)/16;
std::string temp2;

std::cout << "deci : " << deci << std::endl;
std::cout << "reste : " << reste << std::endl;
std::cout << "partiehexa : " << partiehexa << std::endl;

std::stringstream ss2;
ss2 << reste;
ss2 >> temp;
ss2 << partiehexa;
ss2 >> temp2;

if (partiehexa<10) {LRCascii+=temp2;}
if (partiehexa==10) {LRCascii+='A';}
if (partiehexa==11) {LRCascii+='B';}
if (partiehexa==12) {LRCascii+='C';}
if (partiehexa==13) {LRCascii+='D';}
if (partiehexa==14) {LRCascii+='E';}
if (partiehexa==15) {LRCascii+='F';}
if (reste<10) {LRCascii+=temp;}
if (reste==10) {LRCascii+='A';}
if (reste==11) {LRCascii+='B';}
if (reste==12) {LRCascii+='C';}
if (reste==13) {LRCascii+='D';}
if (reste==14) {LRCascii+='E';}
if (reste==15) {LRCascii+='F';}

std::cout << "LRC : " << LRCascii << std::endl;

return;
}
Examples on what is the input and the result when it is working :

input > ":040100130013??\r\n"

The cout display "LRC : D5"

input > ":0401CD6B05??\r\n"

The cout display "LRC : BE"

D5 and BE are the right results.

I tried with other inputs and had no problem until this :

input > ":0403006B0003??\r\n"

The cout display "LRC : B"

input > ":040306022B00000064??\r\n"

The cout display "LRC : 2"

It should be 8B and not simply B and it should be 62 and not simply 2.

We can see that the last part of the LRC is good but the other part is ignored. What is even stranger is that in this case the cout of "partiehexa" is showing "8" and "6", it is not like this int was empty. I fail to understand why this is happening in this case.
 
Old 05-12-2015, 07:04 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Can't yet compile this in a test program to provide any assistance. I run into a problem understanding what hexCh is. What headers do you use when you compile this program?

Note that my strategy here would be to run in GDB and step through it to see the construction of LRCacsii on a case by case basis. I.e. my main simply calls this function four or five times using each of your test input strings.

Have you tried to run under GDB and stepped through this program?

Have you just put debug output for LRCascii as it gets constructed?
 
Old 05-12-2015, 07:54 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you need to print partiehexa and reste just to check their contents, looks like one of them is out of range and there is no error check in your code.
 
Old 05-12-2015, 08:56 AM   #4
wowy
Member
 
Registered: Mar 2015
Location: France
Distribution: Ubuntu 14.04
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
Can't yet compile this in a test program to provide any assistance. I run into a problem understanding what hexCh is. What headers do you use when you compile this program?

Note that my strategy here would be to run in GDB and step through it to see the construction of LRCacsii on a case by case basis. I.e. my main simply calls this function four or five times using each of your test input strings.

Have you tried to run under GDB and stepped through this program?

Have you just put debug output for LRCascii as it gets constructed?
Sadly i use a kit of qt with a faulty debugger and no rights on this session to modify it right now.

The header i use are (some of them useless here for this function):
include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>

It seems that if i replace :
Code:
if (partiehexa<10) {LRCascii+=temp2;}
by :

Code:
if (partiehexa<10) 
{ 
    std::stringstream ss3;
    ss3 << partiehexa;
    ss3 >> temp2;
    LRCascii += temp2;
}
The problem is solved. So it works but for me this is not logical, since the modification is really close to what it was before.
 
Old 05-12-2015, 10:57 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
First, this is an amazingly ugly way to convert a number into two digits in ascii hex. That ought to be a trivial task (many different trivial ways are possible) yet you made it both difficult and wrong. Was there a reason?

Second, you have shown something is wrong with a specific small bit of code you posted:
Code:
std::stringstream ss2;
ss2 << reste;
ss2 >> temp;
ss2 << partiehexa;
ss2 >> temp2;
I don't use stringstream often enough myself to recognize what is wrong. I've never reversed direction repeatedly like that and don't even know if that is supported.

If I were a bit more motivated, I might look it up and/or test it. But I'm not that motivated. You could test it yourself.

reste is a one or two digit int. partiahexa is a one digit int. temp and temp2 are std::string.

You have indicated the above code puts the "wrong" thing into temp2. You want it to put (std::string)( (char)('0'+partiahexa) ) into temp2. It should be easy to test what it actually puts into temp2. But why you ever even needed temp2 is questionable.
 
  


Reply

Tags
c++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
msg_flags of sctp_rcvmsg () behave differently in redhat rel5 and redhat rel6 LearningGeek Linux - Server 2 08-09-2014 12:24 PM
[SOLVED] gawk+gensub beckreference behave differently in while loop? stillsmil Programming 4 07-09-2012 03:40 AM
Is dpkg/lock working differently in Oneiric? It didn't behave this way before Fennippee Linux - Newbie 1 10-29-2011 12:50 AM
How to make mediatomb behave similar to ftp/nfs/sshfs server? 10110111 Linux - Software 2 08-12-2010 06:00 AM
Make mysql order by to behave differently raven Linux - Server 2 11-30-2007 03:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration