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

  • SEARCH
  • Home
  • 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 6049737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:36:38+00:00 2026-05-23T07:36:38+00:00

Possible Duplicate: Converting a four character string to a long I want to convert

  • 0

Possible Duplicate:
Converting a four character string to a long

I want to convert a char array to a long and back again but I’m a bit stuck.

This is a fragment of the code I’ve got so far:

char mychararray[4] = {'a', 'b', 'c', 'd'};
unsigned long * mylong = (unsigned long *)&mychararray;
cout << *mylong << endl;

Which should take the char array, and represent the first 4 bytes (the length of a long) as a long (I think).

Is this correct? And how would I undo it to get the char array back?

Thanks for your help.

EDIT: The third line was a typo – *mychararray should have been *mylong

  • 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-23T07:36:39+00:00Added an answer on May 23, 2026 at 7:36 am
    1. You’re assuming sizeof(long) == 4, which can be wrong. (Especially on 64 bit platforms). If that assumption is broken, you’re in undefined behavior territory: manipulating mylong will read/write beyond mychararray‘s allocated memory.
    2. Your second line has no effect if you don’t use *mylong on the third line

    You can get back a char* with something like:

    char *thing = reinterpret_cast<char*>(mylong);
    

    (You should be using that type of cast in the first case also, it’s more explicit than the C-type cast.)

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

Sidebar

Related Questions

Possible Duplicate: Converting string in C# I want to camelize a string, for example:
Possible Duplicate: Converting a string to an enumeration value in C# How do I
Possible Duplicate: how can I convert String to Int ? Hi, I have the
Possible Duplicate: Converting XML to JSON using Python? I am importing an XML feed
Possible Duplicate: Converting Raw HTTP Request into HTTPWebRequest Object I've got a custom HTTP
Possible Duplicate: Converting from Mercurial to Subversion Hi. Does anyone know of a relatively
Possible Duplicate: Converting XML to JSON using Python? I'm doing some work on App
Possible Duplicate: Char to int conversion in C. I remember learning in a course
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the

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.