0

I'm needing to collect byte by byte of an unsigned char and convert it to a string, how can I do that?

I've tried everything but it didn't work.

My example code:

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

int main()
{
    unsigned char byte_loop[10] = {0xa0, 0xb1, 0xc2, 0xb3, 0xd4, 0xe5, 0xa6, 0xb7, 0xc8, 0xa9};
    char text_concat[11];

    for (int i = 0; i <= 10; i++)
    {
        text_concat[i] = byte_loop[i];
    }

    printf("%s", text_concat);

    return 0;
}

Thanks

0

1 Answer 1

0

The code is essentially correct. The problem is the data that you chose to put in byte_loop[10]. It's pretty much all unprintable or invalid ASCII codes. Try using 0x61 through 0x6a (a through j) instead.

You'll probably want to add code to handle non-printable values or non-ASCII values, after consulting an ASCII table to see what's valid for your purposes.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .