Convert C++ byte array to a C string

19,555

Solution 1

Strings in C are byte arrays which are zero-terminated. So all you need to do is copy the array into a new buffer with sufficient space for a trailing zero byte:

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

typedef unsigned char BYTE;

int main() {
    BYTE byteArray[5] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F };
    char str[(sizeof byteArray) + 1];
    memcpy(str, byteArray, sizeof byteArray);
    str[sizeof byteArray] = 0; // Null termination.
    printf("%s\n", str);
}

Solution 2

C strings are null terminated, so the size of the string will be the size of the array plus one, for the null terminator. Then you could use memcpy() to copy the string, like this:

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

typedef unsigned char BYTE;

int main(void)
{
  BYTE byteArray[5] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F };

  // +1 for the NULL terminator
  char str[sizeof(byteArray) + 1];
  // Copy contents
  memcpy(str, byteArray, sizeof(byteArray));
  // Append NULL terminator
  str[sizeof(byteArray)] = '\0';

  printf("%s\n", str);    
  return EXIT_SUCCESS;
}

Output:

Hello

Run it online

Share:
19,555
Dan James Palmer
Author by

Dan James Palmer

Been working for a software development company for the last 10 years (and counting). Current areas I work in include android development, web development and Internet marketing.

Updated on June 21, 2022

Comments

  • Dan James Palmer
    Dan James Palmer almost 2 years

    I'm trying to convert a byte array to a string in C but I can't quite figure it out.

    I have an example of what works for me in C++ but I need to convert it to C.

    The C++ code is below:

    #include <iostream>
    #include <string>
    
    typedef unsigned char BYTE;
    
    int main(int argc, char *argv[])
    {
      BYTE byteArray[5] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F };
      std::string s(reinterpret_cast<char*>(byteArray), sizeof(byteArray));
      std::cout << s << std::endl;
    
      return EXIT_SUCCESS;
    }
    

    Can anyone point me in the right direction?

  • Alexis Wilke
    Alexis Wilke over 4 years
    You're missing the '\0'.
  • Dan James Palmer
    Dan James Palmer over 4 years
    @πάνταῥεῖ every changes my tags for the OP. The first line states the code IS in C++ but I want it in C. This works a treat and will accept when able
  • tadman
    tadman over 4 years
    @DanJamesPalmer Please don't play games, even unintentionally. If you want C, show us C code. Don't use C++ as a way to demonstrate C code. These are two different languages with different tools and approaches suitable to each. That C++ can use C code is not a reason to confuse the two.
  • Alexis Wilke
    Alexis Wilke over 4 years
    It looks slow. Why clear the entire buffer before a copy? Also strcpy() is incorrect since the source is not null terminated.
  • Alexis Wilke
    Alexis Wilke over 4 years
    Only if the string is really large the .. = { '\0' } is going to be a real waste.
  • Konrad Rudolph
    Konrad Rudolph over 4 years
    @tadman That’s an unfair accusation. The tags say C, the title says C, the body of the question says C. We (me included!) are just all jumping at the sight of C++ code and ignore mentions of C in tags and title like Pavlovian dogs (because we’ve been conditioned to expect mis-tagging between the two languages) but the question makes it clear why the C++ code is included, and how it’s relevant and legitimate.
  • Vlad from Moscow
    Vlad from Moscow over 4 years
    @AlexisWilke So as I said the terminating zero can be appended manually.:)
  • gsamaras
    gsamaras over 4 years
    @AlexisWilke right, I could just memcpy(), which doesn't need a cast also. No need to clear indeed, I could just append the NULL terminator, right?
  • tadman
    tadman over 4 years
    @KonradRudolph There is C++ code present which is a red herring, and that's really not called for. I'm of the opinion that if there's C++ code in the question, it is, by definition, a C++ question and should be tagged as such. If you want C and C only, don't even go there, just use C and deal.
  • Vlad from Moscow
    Vlad from Moscow over 4 years
    @gsamaras No I need not. But this variant is already shown in my answer.:)
  • gsamaras
    gsamaras over 4 years
    I thought you meant s as the byteArray, which would be wrong, now it's fine, misread that, συγγνώμη! :)