New line(\n) gets added when printing with Hex byte in loop
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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");
}
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.