LinuxQuestions.org
Help answer threads with 0 replies.
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 01-12-2018, 07:29 AM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 98

Rep: Reputation: Disabled
New line(\n) gets added when printing with Hex byte in loop


I am using slog logger for C to log into file.
In C standard syntax printf when I use to print in hex it print output in single line.
Code:
for(loop = 0; loop < strLen; loop++)
{
     printf("%02X ", fstrParam[loop]);     
}
PHP Code:
Example is shown below.
30  30  30  30  30  30  30  30  36  30  30  30  7c  4c  45  30  30  30  30  30  30  30  31 
I want to write into file. So I am using slog.slog is logger I got from internet as project to print everything in file.

It works like printf statement for everything. But when I print as below format. It is printing each hex value in single line.

Code:
for(loop = 0; loop < strLen; loop++)
{
     slog_debug(0, "%02X ", fstrParam[loop]);
}
PHP Code:
30  
30  
30  
30
..
..    
30  
30  
31 
How can avoid this from my application side? Or I need to modify in slog.
 
Old 01-12-2018, 08:55 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,638

Rep: Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118
You might want to give an URL to this 'slog'-thing.
 
Old 01-12-2018, 09:37 AM   #3
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 955

Rep: Reputation: 418Reputation: 418Reputation: 418Reputation: 418Reputation: 418
Without knowing the slog functions - you can sprintf() and strcat() to a string variable, and slog_debug() it at the end.
 
Old 01-12-2018, 10:04 AM   #4
srinietrx
Member
 
Registered: May 2013
Posts: 98

Original Poster
Rep: Reputation: Disabled
@MadeInGermany
Your solution may solve my problem I think.I will implement and confirm.

@NevemTeve
https://github.com/kala13x/slog
 
Old 01-12-2018, 10:20 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,638

Rep: Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118
Well, it doesn't offer such functionality, so you have to build complete lines in your own buffers.
 
Old 01-15-2018, 07:25 AM   #6
srinietrx
Member
 
Registered: May 2013
Posts: 98

Original Poster
Rep: Reputation: Disabled
Hi,
I implemented code as shown below.It is working fine independently.But when I added this to my Project(application) it gets killed after sometime.For your information my application uses thread.
So, My question is whether Hex_Print_In_Single_Line() is correct

Code:
void Hex_Print_In_Single_Line(unsigned char digest[], unsigned char *hex_output);

int main()
{
        slog_init("log_TestProgram", "slog.cfg", 1, 3, 1);//1->File name where log is saved, 2->config file, 3-> Max log level, 4-> Max log file level, 5-> Thread safety flag
        unsigned char output[1024] = {0};
        unsigned char input[1024] = {0};
        strcpy(input, "hello");
        Hex_Print_In_Single_Line(input, output);
        slog_debug(0, "%s\n", output);


}


void Hex_Print_In_Single_Line(unsigned char digest[], unsigned char *hex_output)
{
        char hex_tmp[33]={0};
        int i;
        for (i = 0; i < strlen(digest); i++)
        {
                printf("%02x ", digest[i]);
                sprintf(&hex_tmp[i*2],"%02x", digest[i]);
                strcat(hex_output, &hex_tmp[i*2]);
                strcat(hex_output, " ");
        }
        printf("\n");
}
 
Old 01-15-2018, 07:39 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,638

Rep: Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118
You don't seem to check the maximum lengths.
 
Old 01-15-2018, 07:43 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,231
Blog Entries: 12

Rep: Reputation: 2702Reputation: 2702Reputation: 2702Reputation: 2702Reputation: 2702Reputation: 2702Reputation: 2702Reputation: 2702Reputation: 2702Reputation: 2702Reputation: 2702
My recommendation is that you write your own functions or macros to filter and generate your debug.

Some utility written by someone else suits their needs. If you have their source, you can copy it and edit it to suit what you want it to do.

I typically find that others have refined in ways or to limits that go astray from how I'd do it and instead I'll start with what they have published and morph that into my own rendition, or for something like this, I'll just invent it and incrementally add to it as I need more functionality. And then typically copy it forever into my many projects, providing I liked what I did that first time.
 
Old 01-15-2018, 08:06 AM   #9
srinietrx
Member
 
Registered: May 2013
Posts: 98

Original Poster
Rep: Reputation: Disabled
rtmistler:- In future I will start to read other person's code ,understand and customise according to my need.

NevemTeve:- Yes you are correct. It is because of length. When application got killed I received length more that declared.

Initially I kept it as
PHP Code:
char hex_tmp[1024] = {0}; 
When I changed to
PHP Code:
char hex_tmp[4096] = {0}; 
I will add max length check in my code. In general where length check is written. It should be written inside function(i.e.Hex_Print_In_Single_Line()) or Outside in main application.

Thanks.
 
  


Reply

Tags
c programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Searching for a specific hex string or byte array in various images plasma33 Linux - Software 24 05-08-2016 07:57 PM
kindly explain this line.... asm volatile(".byte 15;.byte 49" : "=a"(bottom),"=d"(top)); zaki Rahat Linux - Newbie 1 03-20-2016 05:39 AM
[SOLVED] How to replace 10th and 11th byte in a hex dump Heraton Linux - Newbie 8 02-20-2012 01:11 PM
Looking for tcpdump syntax to start hex output at a given byte offset in the packet silentquasar Linux - Networking 1 08-06-2011 07:03 AM
[SOLVED] converting byte in hex in kernel ano Linux - Kernel 3 12-21-2010 11:52 AM

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

All times are GMT -5. The time now is 01:13 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration