Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7823397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:13:55+00:00 2026-06-02T08:13:55+00:00

I have a helper function that takes an unsigned char array of a fixed

  • 0

I have a helper function that takes an unsigned char array of a fixed length, and returns it as a formatted char *. However, I’m having some problems.

I tried

char* byteArrayToString(unsigned char byte[6]) {
    char t[18] = {""};
    char* str = t;
    sprintf(str, "%02X:%02X:%02X:%02X:%02X:%02X", byte[0], byte[1], byte[2], byte[3], byte[4], byte[5]);
    return str;
}

and

char* byteArrayToString(unsigned char byte[6]) {
    std::string t = "";
    char* str = t;
    sprintf(str, "%02X:%02X:%02X:%02X:%02X:%02X", byte[0], byte[1], byte[2], byte[3], byte[4], byte[5]);
    return str;
}

and

char* byteArrayToString(unsigned char byte[6]) {
    char* str = new char();
    sprintf(str, "%02X:%02X:%02X:%02X:%02X:%02X", byte[0], byte[1], byte[2], byte[3], byte[4], byte[5]);
    return str;
}

The second one results in some side effects of the value of that string being changed. The first one ends up giving me junk values and the last seg faults (but I can’t figure out why).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:13:56+00:00Added an answer on June 2, 2026 at 8:13 am

    The problem with your first one is not in the printing, but in the returning. You’re returning a pointer to an array which has been reclaimed (because it is an automatic variable, its lifetime ends when the function returns).

    Instead try:

    string byteArrayToString(const unsigned char* const byte)
    {
        char t[18] = {""};
        sprintf(t, "%02X:%02X:%02X:%02X:%02X:%02X", byte[0], byte[1], byte[2], byte[3], byte[4], byte[5]);
        return t;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function f that takes a vector v and returns a
I use symfony 1.4.11 with doctrine. I have helper: function filterwords($text){ $filterWords=array ('some','filter','words'); $filterCount
I have a function that takes optional arguments as name/value pairs. function example(varargin) %
I have a function that takes a string object name and I need the
I have a function uint8_t EE_Write(uint8_t addr, uint8_t len, uint8_t * buf) that takes
I need to run a function that takes two arguments several times. I have
I'm writing a function that takes a list and returns a list of permutations
I'm looking for an SML function that takes a nonnegative integer and returns a
Suppose I have a function which takes some form of predicate: void Foo( boost::function<bool(int,int,int)>
I have a helper function, which basically calls CompareTo on two objects, but does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.