473,396 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to convert single char to long in c

Ex :
Char x='2';
long out=0;
out=atol(x);

here i want to get out value as 2 but it is not happening
Now out value get 0 . Please suggest as soon as ossible
Feb 18 '14 #1
3 1311
stdq
94 64KB
Hello! I think you could do a cast:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.     char x = '2';
  6.     long int out = ( long int )x;
  7.  
  8.     printf( "%ld\n", out );
  9. }
However, be careful, because '2' is a character whose decimal value in ASCII is 50, so the output of this code will be 50.
Feb 18 '14 #2
weaknessforcats
9,208 Expert Mod 8TB
So just code:

Expand|Select|Wrap|Line Numbers
  1. char x=2;
  2. long out=0;
  3. out= x;
A char and a long are both integers so you can assign the char to the long. The compiler won't object because the long is larger than the char. You just can't go the other way without getting a warning from the compiler about possible loss of data.
Feb 18 '14 #3
Banfa
9,065 Expert Mod 8TB
The problem is that atol expects a char* not a char. I'm surprised the code you posted even compiles, I am sure it doesn't compile without warnings.

You could just take a pointer to your char
Expand|Select|Wrap|Line Numbers
  1. out=atol(&x);
Problem with that is atol doesn't just expect a char* it expects a pointer to a C string, that is a zero terminated array of char which you do not have you have a single char. You could convert it to a string

Expand|Select|Wrap|Line Numbers
  1. char buffer[2];
  2. buffer[0] = x;
  3. buffer[1] = '\0';
  4. out=atol(buffer);
  5.  
But personally I find it rather long winded. As long as you are sure there is a digit character in your variable then you can just rely on the language guarantee that digit character values are contiguous (i.e. '1' = '0' + 1 ...)

Expand|Select|Wrap|Line Numbers
  1. out= x - '0';
Feb 18 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Vincent | last post by:
Hi all, I want to convert a char (binary) to an unsigned long. How can I do this? Thanks, Vincent
4
by: Vijai Kalyan | last post by:
I wrote the following function as a curiosity: template<typename SourceType, typename DestinationType> DestinationType NumericCast(const SourceType& value) { std::wstringstream strbuf; strbuf...
1
by: Steven Woody | last post by:
somewhere in my code, char* rtnStrs = { "ok", "error", NULL }; foo( rtnStrs ); here, foo was decleared as 'void foo( const char * rtnStrs ). but the gcc compiler will report ' can not...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
3
by: David | last post by:
Hi, how to convert a char array(byte) to a string variable? byte buffer; string strTest; /*the blow codes all get type of 'buffer': System.Byte! strTest = buffer.ToString() strTest =...
9
by: Simple Simon | last post by:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it...
5
by: Zytan | last post by:
I am surprised that a single character string is not auto-created from a single char. It is hard to find information on converting a char into a string, since most people ask how to convert char...
2
by: Oki | last post by:
I appreciate any help; I'm currently getting a problem with trying to concatenate a single char from a structure to an existing string. Basically, the problem is here: strcat( res, morse_c.letter...
2
by: slizorn | last post by:
error is as stated in the topic above: error C2440: '=' : cannot convert from 'char *' to 'char' code is below void handleOneLine(string string1) { char * cstr, *p; int counter; string...
2
by: Frnk Carleo | last post by:
I am looking to convert a char array defined as char pstrInputClaim to a std::wstring trying to use the constructor as below std::wstring strInputClaim(pstrInputClaim);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.