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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:19:56+00:00 2026-05-23T05:19:56+00:00

How do you convert a string to an unsigned long so that the unsigned

  • 0

How do you convert a string to an unsigned long so that the unsigned long represents the characters in the string as 8bit numbers?

Thanks in advance.

EDIT: What I want to do is turn a string of 4 characters into a long of the same four characters in ASCII code format.

  • 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-23T05:19:56+00:00Added an answer on May 23, 2026 at 5:19 am

    largely the same solution, making less assumptions on integral types sizes and checking the maximum string size that can be translated this way:

    #include <string>
    #include <algorithm>
    #include <iostream>
    #include <climits>
    
    int main()
    {
      const size_t MAX = sizeof(unsigned long);
    
      std::string s("abcd");
      unsigned long res = 0;
    
      for (size_t i=0; i < std::min(MAX, s.size()); ++i)
      {
        res <<= CHAR_BIT;
        res += (unsigned char) s[i];
      }
    
      std::cout << std::hex << res;
    }
    

    prints

    61626364
    

    the casting to unsigned char is for the case your string contains high ASCII, i.e. values higher than 127, in which case char would be negative. Try the string "\226\226\226\226" instead and you will get incorrect result)

    EDIT: BTW, in your subject you say “and back”, so here’s the reverse transformation:

    #include <limits>
    
    std::string tostr(unsigned long x)
    {
      // this will mask out the lower "byte" of the number
      const unsigned long mask = std::numeric_limits<unsigned char>::max();
    
      if (x == 0)
        return std::string("0");
    
      std::string res;
      for (; x > 0; x >>= CHAR_BIT) {
        res += (char) (x & mask);
      }
    
      // we extracted the bytes from right to left, so need to reverse the result
      std::reverse(res.begin(), res.end());
      return res;
    }
    

    http://ideone.com/Cw7hF

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

Sidebar

Related Questions

I have unsigned char* , want to convert it to std::string . Can you
Possible Duplicate: Converting a four character string to a long I want to convert
I am trying to convert a string to unsigned long. Here is the code
I have the following function that will convert a string into a numeric data
When I convert an unsigned 8-bit int to string then I know the result
I want to convert String to Date in different formats. For example, I am
What's the safest and best way to retrieve an unsigned long from a string
PHP does not support unsigned ints. Is there a way to convert a string
sqlite3_column_text returns a const unsigned char*, how do I convert this to a std::string?
today I tried to convert a hex string to an unsigned char[] string test

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.