Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 352 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert char to int in C and C++

#1
How do I convert a `char` to an `int` in C and C++?
Reply

#2
Presumably you want this conversion for using functions from the C standard library.

In that case, do (C++ syntax)

typedef unsigned char UChar;

char myCppFunc( char c )
{
return char( someCFunc( UChar( c ) ) );
}

The expression `UChar( c )` converts to `unsigned char` in order to get rid of negative values, which, except for EOF, are not supported by the C functions.

Then the result of that expression is used as actual argument for an `int` formal argument. Where you get automatic promotion to `int`. You can alternatively write that last step explicitly, like `int( UChar( c ) )`, but personally I find that too verbose.

Cheers & hth.,
Reply

#3
It sort of depends on what you mean by "convert".

If you have a series of characters that represents an integer, like "123456", then there are two typical ways to do that in C: Use a special-purpose conversion like [atoi()][1] or [strtol()][2], or the general-purpose [sscanf()][3]. C++ (which is really a different language masquerading as an upgrade) adds a third, stringstreams.

If you mean you want the exact bit pattern in one of your `int` variables to be treated as a `char`, that's easier. In C the different integer types are really more of a state of mind than actual separate "types". Just start using it where `char`s are asked for, and you should be OK. You might need an explicit conversion to make the compiler quit whining on occasion, but all that should do is drop any extra bits past 256.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#4
I was having problems converting a char array like `"7c7c7d7d7d7d7c7c7c7d7d7d7d7c7c7c7c7c7c7d7d7c7c7c7c7d7c7d7d7d7c7c2e2e2e"` into its actual integer value that would be able to be represented by `7C' as one hexadecimal value. So, after cruising for help I created this, and thought it would be cool to share.

This separates the char string into its right integers, and may be helpful to more people than just me ;)

unsigned int* char2int(char *a, int len)
{
int i,u;
unsigned int *val = malloc(len*sizeof(unsigned long));

for(i=0,u=0;i<len;i++){
if(i%2==0){
if(a[i] <= 57)
val[u] = (a[i]-50)<<4;
else
val[u] = (a[i]-55)<<4;
}
else{
if(a[i] <= 57)
val[u] += (a[i]-50);
else
val[u] += (a[i]-55);
u++;
}
}
return val;
}

Hope it helps!
Reply

#5
For char or short to int, you just need to assign the value.

char ch = 16;
int in = ch;

Same to int64.

long long lo = ch;

All values will be 16.
Reply

#6
Use `static_cast<int>`:

int num = static_cast<int>(letter); // if letter='a', num=97

---
**Edit:** You probably should try to avoid to use <s>`(int)`</s>

> <s>int num = (int) letter;</s>

check out

[To see links please register here]

for more info.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through