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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:27:31+00:00 2026-06-17T21:27:31+00:00

Anyway have any idea how to do this? Let’s say i have char x[]

  • 0

Anyway have any idea how to do this?

Let’s say i have

char x[] = "ABCD";

and i want to put it into an int, so i’ll have

int y = 'ABCD';

I can only put individual chars, such as int y = x[0]; The purpose is to find the decimal representation, but i want the decimal representation of “ABCD” not just “A”.

Finally i would use sprintf(dest, "%.2u", value); to get the decimal representation of the char.

EDIT:

I dont understand why, but for “ABCD” this code works

//unrolled bit ops
const char* x = "ABCD";
uint32_t y = 0;
y |= (uint32_t(x[0]) << 24); //MSB
y |= (uint32_t(x[1]) << 16);
y |= (uint32_t(x[2]) <<  8);
y |= (uint32_t(x[3]) /*<< 0*/);

however, per instance if i use “(¸þ¶” i dont get the same result.

EDIT2 **:

I’ve tried your last edit Sam, but it still doesnt work. The value i’m getting is “4294967294” as opposed to “683212470” the correct value.
I also did this

int h1 = '(';
int h2 = '¸';
int h3 = 'þ';
int h4 = '¶';

Output:

40
-72
-2
-74

I googled for the complete ascii table, and i found out that for “þ” the value is “254”. I suppose it has something to do with this… i also tried with usigned but no good results.

edit3: If i replace const char *x = "(¸þ¶" with int x[] = {40, 184, 254, 182}; (decimal representation of each character, it works. I can understand where things go wrong, but i have no idea how to fix it.

  • 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-17T21:27:33+00:00Added an answer on June 17, 2026 at 9:27 pm

    You need to assure int alignment for that char array for a proper cast or do a memcpy into that int.
    Also take care of the integer’s endianness! Furthermore, usage of C99 integer types such as uint32_t, will also help to make your code portable.

    See this question for how to convert the bits:
    strict aliasing and alignment

    EDIT:

    What R. Martinho Fernandes means, might be this (not tested):

    //unrolled bit ops
    const char* x = "ABCD";
    uint32_t y = 0;
    y |= (uint32_t(uint8_t(x[0])) << 24); //MSB
    y |= (uint32_t(uint8_t(x[1])) << 16);
    y |= (uint32_t(uint8_t(x[2])) <<  8);
    y |= (uint32_t(uint8_t(x[3])) /*<< 0*/);
    

    Above example avoids specific code for any endianness

    EDIT 2:

    For dynamic char arrays (assuming leading zero chars if less than 4 have to be converted):

    const char* x = "ABC";
    size_t nChars = 3;
    
    assert(0 < nChars && nChars <= sizeof(uint32_t));
    
    uint32_t y = 0;
    
    int shift = (nChars*8)-8;
    for(size_t i = 0;i < nChars;++i)
    {
        y |= (uint32_t(uint8_t(x[i])) << shift);
        shift -= 8;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a web request: WebRequest webRequest = WebRequest.Create(Url); webRequest.BeginGetResponse(this.RespCallback, webRequest); Now
Let say we have set of URLs like this yoursite.com/39827-key1-key2-key3 yoursite.com/132-key1-key2-key3 yoursite.com/9223455-key1-key2-key3 and so
Let's say I have some code like this in AS3 for(...) thing = new
Can I have any way to know which is the depth of a child
Does anyone know do there have any way that I can encrypt the array
Is there any way to have a default MessageBox.Show() caption? Let says I would
Is there any way to have this: [one[two]][three] And extract this with a regex?
I'm new to MongoDB and I've used RDBMS for years. Anyway, let's say I
I have a very nice idea for a kernel patch, and I want to
Let's say I have an interface in Java: interface I { void add(I foo);

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.