2

I would like to optimize my code and avoid errors, I have this function that does the "work" but I think I can improve and avoid memory problems.

void function(char* message)
{
    char * pointer;
    unsigned char buffer[2048] = {0};
    int buffer_len = 0;

    memset(buffer, 0, sizeof(buffer));
    strcpy(buffer, message);

    buffer_len = strlen(buffer);
    memset(buffer, 0, sizeof(&buffer));

    for(int i = 0, pointer = message; i < (buffer_len / 2); i++, pointer += 2)
    {
        sscanf(pointer, "%02hhX", &buffer[i]);
    }
}

The idea of ​​the function is to receive a string of this style "0123456789" and pass it to 0x01, 0x23, 0x45, ... in an unsigned char array. Any tip, good practice or improvement would be very useful.

The ussage is something like this:

function("0123456789");

In the function buffer ends like:

buffer[0] = 0x01
buffer[1] = 0x23
...
5
  • 6
    This would be better on codereview.stackexchange.com Commented Dec 15, 2017 at 18:40
  • 1
    Might be good to show the target function too. Right now, there are quite a few potential issues with just the code you've shown (strcpy is not safe, multiple memset to same data, probably not needing to memset anyway. Commented Dec 15, 2017 at 18:44
  • 1
    If the string is gigantic, and copying it is a problem, consider using a real compression method. Something like a Huffman encoder can do better than 4-bits per character as it will take into account the usage frequency of each value, Commented Dec 15, 2017 at 18:48
  • sizeof(&buffer) ??? Commented Dec 15, 2017 at 22:28
  • I'm voting to close this question as off-topic because the code is working. Optimizing/reviewing questions can be asked on Code Review. Commented Dec 16, 2017 at 1:49

2 Answers 2

3

There are a few optimizations possible. The biggest optimization comes from avoiding doing 2x memset and strcpy,

No need to:

// memset(buffer, 0, sizeof(buffer)); 
// strcpy(buffer, message);
// memset(buffer, 0, sizeof(&buffer));  

which drastically simplifies the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void function(char* message)
{
    unsigned char * pointer;
    unsigned char buffer[2048]; // not needed = {0}; 
    int buffer_half_len; // not needed = 0;

    buffer_half_len = strlen(message)/2;  // avoiding division in the for loop;
    pointer = message;

    for(int i = 0;  i < buffer_half_len; i++, pointer += 2)
    {
        sscanf(pointer, "%02hhX", &buffer[i]);
        printf("%02hhX\n", buffer[i] );
    }
}

OUTPUT:

01
23
45
67
89
Sign up to request clarification or add additional context in comments.

1 Comment

buffer store the A B C D E F,how to change to store lower case?
0
char * a= -80; // let supposed you get returned value( i represented in int) from whatever source.
unsigned char b = * a; // now b equal the complemntry of -80 which will be 176
std::cout << b ;

1 Comment

While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find how to write a good answer very helpful. Please edit your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.