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 6131387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:58:07+00:00 2026-05-23T16:58:07+00:00

This question may looks silly, but please guide me I have a function to

  • 0

This question may looks silly, but please guide me
I have a function to convert long data to char array

void ConvertLongToChar(char *pSrc, char *pDest)
{
    pDest[0] = pSrc[0];
    pDest[1] = pSrc[1];
    pDest[2] = pSrc[2];
    pDest[3] = pSrc[3];
}

And I call the above function like this

long lTemp = (long) (fRxPower * 1000);
ConvertLongToChar ((char *)&lTemp, pBuffer);

Which works fine.
I need a similar function to reverse the procedure. Convert char array to long.
I cannot use atol or similar functions.

  • 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-05-23T16:58:07+00:00Added an answer on May 23, 2026 at 4:58 pm

    Leaving the burden of matching the endianness with your other function to you, here’s one way:

    unsigned long int l = pdest[0] | (pdest[1] << 8) | (pdest[2] << 16) | (pdest[3] << 24);
    

    Just to be safe, here’s the corresponding other direction:

    unsigned char pdest[4];
    unsigned long int l;
    pdest[0] = l         & 0xFF;
    pdest[1] = (l >>  8) & 0xFF;
    pdest[2] = (l >> 16) & 0xFF;
    pdest[3] = (l >> 24) & 0xFF;
    

    Going from char[4] to long and back is entirely reversible; going from long to char[4] and back is reversible for values up to 2^32-1.

    Note that all this is only well-defined for unsigned types.

    (My example is little endian if you read pdest from left to right.)

    Addendum: I’m also assuming that CHAR_BIT == 8. In general, substitute multiples of 8 by multiples of CHAR_BIT in the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi guys this question may look to be very silly. But still i have
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
This question may have been asked before, but I had trouble finding an answer,
This may be a silly question for some of you, but what are Pros/Cons
Apologies in advance, because I suspect this may be a silly question. I have
This may be a bit silly question, but I just started doing pretty things
This question may be too product specifc but I'd like to know if anyone
This question may sound fairly elementary, but this is a debate I had with
This question may seem trivial, but I hope you won't ignore it. Before destroying
This question may not directly relate to programming. I have noticed that the technology

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.