How to Convert a char array to int?


all, I'm new to C and I'm having a heck of a time trying to convert a char array element to an integer. Declaration of my char array is: char myarray[80]; ....then I scanf a number from the user ...
Posted On: Monday 26th of November 2012 12:45:53 AM Total Views:  299
View Complete with Replies

RELATED TOPICS OF C Language PROGRAMMING LANGUAGE




Creating mutually non-convertible yet identical classes

The problem I am facing is that an integer is the ideal representation for many different classes I am using. I want to write overloaded functions that are able to differentiate between the classes without mutual interference. The naive, yet wrong, solution is: typedef int A; typedef int B; void f(A a); void f(B b); // Error since typedef does not introduce a new type. In addition, I want the naked expression "b = a" to error out during compile time. The solution that I have implemented looks like this: #define DECLARE_UNIQUE_CLASS_ALIAS(_ALIAS, _BASE)\ struct _ALIAS\ {\ _BASE value;\ _ALIAS(const _BASE& v)\ {\ this->value = v;\ }\ operator _BASE() const\ {\ return this->value;\ }\ }; DECLARE_UNIQUE_CLASS_ALIAS(A, int) DECLARE_UNIQUE_CLASS_ALIAS(B, int) This has the deed effect, but I am not happy with it because: 1. It uses macros, and is not debuggable. 2. Since the machine code for each alias is identical, and this will be used a lot, this code will not be optimized by the compiler. Could someone could suggest a solution that does not have the defects that my code has
VIEWS ON THIS POST

109

Posted on:

Wednesday 7th November 2012
View Replies!

convert binary to decimal in template at compiler time.

i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin::value == 65535)); BOOST_STATIC_ASSERT((bin::value == 15)); BOOST_STATIC_ASSERT((bin::value == 0)); BOOST_STATIC_ASSERT((bin::value == 163)); you can find my implementation at: http://pastebin.org/2271 the first three cases were ok, but the last one failed, because, compiler will parse 0011 as a octal number 9, instead of decimal number 11 because of the leading 0s. to resolve this, i defined 4 macros: #define BIN1(a) bin::value #define BIN2(a, b) bin::value #define BIN3(a, b, c) bin::value #define BIN4(a, b, c, d) bin::value these macros could pass the last test case, but it's not good enough that i have to specify how many numbers i will input. is there any elegant way to resolve this
VIEWS ON THIS POST

151

Posted on:

Wednesday 7th November 2012
View Replies!

avi converter

hello everybody! is there any body hava avi medya converter thaks a lot )
VIEWS ON THIS POST

92

Posted on:

Wednesday 7th November 2012
View Replies!

double to fractional converter

Does anyone know of a method to convert a double into a fraction I'm thinking along the line of passing a double to the function, and having it return a string value along the lines of: double x; string s1; x = 3.75; s1 = doubletofraction(x); with the result be 3 3/4 stored in the s1 variable. I know I can write a function to do it, but why not be lazy..... If I do need to write my own, what are your thoughts on how to evaluate the decimal remainder ( .75 ect ) A series of if/then statements, or maybe a case statement might be better This will be a workhorse in my app, called thousands of time each run. My apologies if the above syntax is not exactly right, I am only 3 weeks into self teaching myself C++ Michael , woodguy23701@remov.yahoo.com wrote: > On 22-May-2007, Mark P wrote: > >>> Does anyone know of a method to convert a double into a fraction >>> >>> I'm thinking along the line of passing a double to the function, >>> and having it return a string value along the lines of: >>> >>> double x; >>> string s1; >>> x = 3.75; >>> s1 = doubletofraction(x); >>> >>> with the result be 3 3/4 stored in the s1 variable. >>> >>> I know I can write a function to do it, but why not be lazy..... >>> >>> If I do need to write my own, what are your thoughts on how to evaluate >>> the >>> decimal remainder ( .75 ect ) >> What do you mean, "evaluate the decimal remainder" > > I need to learn to be more clear. What I am looking for is to be able to > take a double, say 3.75, > I can get the integer value easy enough, but I want to translate the > remainder .75 into a fractional representation > of "3/4" in a string value, then I'll concat a string together to look > something like "3 3/4" > >>> A series of if/then statements, or maybe a case statement might be >>> better >>> This will be a workhorse in my app, called thousands of time each run. >>> >>> My apologies if the above syntax is not exactly right, I am only 3 weeks >>> into self teaching myself C++ >>> >> If you're looking for a built-in function in C++ I think you're out of >> luck. >> >> If you're looking for an algorithm, that's a different story. Obviously >> you can do the stupidest thing and convert .xyz, say, into xyz/1000. >> Slightly smarter is to remove common factors from the fraction. Quite a >> bit better would be a continued fraction expansion which would let you >> neatly approximate fractions which don't simplify exactly to compact >> results. > > I don't understand what you mean by continued fraction expansion. But do get > the point > I'm going to have to code it myself, my first thought is a series of if > statements. At most I would only > level of precision to 1/8, so I would have 9 if statements I think. This will depend how fancy you want to get. If you really just want to round everything to multiples of 1/8, then it's quite simple. You don't even need all those if-cases. You can simply divide the factional part by 1/8 (i.e., multiply by 8) to see how many eighths you have, possibly rounding to get the closest value. So 0.333 * 8 = 2.664 which rounds to 3, giving 3/8. But maybe rounding what is obviously very close to 1/3 isn't very satisfying. You could mess around with different denominators besides 8, maybe try a bunch of values in a range and pick the one that's closest, but what this really wants is continued fractions. http://mathworld.wolfram.com/SimpleC...dFraction.html Generating one is very simple. First pull of the whole number portion of the original number to get a decimal between 0 and 1. Then follow the algorithm below to fill an array called values: i = 0 // an index while n in (0,1] { compute 1/n = a.b values[i] = a n = 0.b ++i } When the process stops, or when you've done as many iterations as you'd like, the array values has the important information: n can be approximated as: 1/(a[0] + 1/(a[1] + 1/(a[2] + ... + 1/a[N] ... ))) The nice thing here is that you can collapse the fraction, keeping as many of the terms as you like, to get your answer. Here's an example: n = 0.13725 1/n = 7.28597... a[0] = 7 n = .28597... 1/n = 3.49681... a[1] = 3 n = 0.49681... 1/n = 2.01282... a[2] = 2 n = 0.01282... 1/n = 78 a[3] = 78 n = 0 STOP So my original decimal = 1/(7 + 1/(3 + 1/(2 + 1/78))) Now to turn this into a proper fraction, we keep as many denominator terms as we want. If I keep only the first term I get 1/7 = 0.14285... which is an OK approximation. If I keep the first two terms I get 1/(7 + 1/3) = 1/(22/3) = 3/22 = 0.13636... which is already quite accurate (within 0.1%) If I keep the first three terms I get 1/(7 + 1/(3 + 1/2)) = 1/(7 + 2/7) = 7/51 = 0.1372549... As you can see this is extremely close to the original value. I'll leave the result of all four terms to you. The point I want to illustrate is that you typically need only a few terms of the continued fraction to get a very good approximation to the original decimal. This actually looks like a fun little project. And truly, it's not as complicated as it may look. Once you get past all the explanation, the implementation is quite compact. Hope that helps, Mark
VIEWS ON THIS POST

96

Posted on:

Wednesday 7th November 2012
View Replies!

Problem in the convertion of any number base B to decimal

the program is asking the user for a number ant he base of the number, then it will convert the number to decimal. But is running right only with base 2, 3, 4, 5, 6, 7, 8 but if I try 16 it is not working. can some one help me please.
VIEWS ON THIS POST

212

Posted on:

Wednesday 7th November 2012
View Replies!

how to convert a std::vector to a string array (std::vector)

I use std::vector buf(50); rData = sio_read(DataModule2->DruckCOMPort_(),&buf[0],50); as result I get rData=42 and buf = "82485051486143484648484848484869434848131082485049536143484648484848484869434848131000000000" or in hex: 52 30 32 33 30 3D 2B 30 2E 30 30 30 30 30 30 45 2B 30 30 ...
VIEWS ON THIS POST

129

Posted on:

Saturday 10th November 2012
View Replies!

converting to cb2007 from b5: iterators are const?

It seems iterator are const in 2007 and not only const_iterator This worked in builder 5 #include #pragma hdrstop #include using namespace std; struct MyClass { int id; int data; MyClass() : id(0), data(0) {} MyClass(int _id) : ...
VIEWS ON THIS POST

267

Posted on:

Saturday 10th November 2012
View Replies!

Simple Question convert String^ into char*

Hallo, I just started with Visual-c++ 2005 express How I can convert "String^ - result of a dialog" into a "string" or in a "char *" public ref class f1 : public System::Windows::Forms::Form { public:         // ...
VIEWS ON THIS POST

111

Posted on:

Saturday 10th November 2012
View Replies!

convertion problem? from char* to unsigned char*?

, 1) First problem. In my project i need to convert from char* to BYTE*( BYTE is unsigned char), is there any way to make the convertion BYTE* bite; char* ch="help me, please"; bite = (BYTE*)ch; is this not ...
VIEWS ON THIS POST

177

Posted on:

Saturday 10th November 2012
View Replies!

converting to utf8?

hi, i've not done much i18n stuff before but i was wondering how, if given a string (encoded as russian or kanji mb) how i would get this converted to utf8. i'm working with forte compiler on solaris i've something ...
VIEWS ON THIS POST

293

Posted on:

Saturday 10th November 2012
View Replies!

convertion form char** to const char** error

I have a question regarding the conversion (in c or c++) from char** to const char**. The fallowing code refuses to compile with g++ (and others): char **p; const char **q = static_cast(p); or, exact the same situation: ...
VIEWS ON THIS POST

120

Posted on:

Sunday 11th November 2012
View Replies!

Storing Arrays in Maps (Or, alternatively, convert hex to dec)

I need to store 2 - 4 values that I have in hex in a map. I've tried storing an int[] in a map and can't make it work. As of now, the only way I can think of is ...
VIEWS ON THIS POST

201

Posted on:

Sunday 11th November 2012
View Replies!

vector of (int,string). can't convert i->first to int

int first=15,latest=15; QString typ=NULL; std::map::iterator i; for(i = SeznamPracovniDoby.begin(); i != SeznamPracovniDoby.end(); i++) { if (typ==NULL) typ=i->second.c_str(); if (typ!=i->second.c_str()) { std::cout<first; } else { latest=i->first; typ=i->second.c_str(); } std::cout << i->first << " " << i->second << std::endl; }...
VIEWS ON THIS POST

171

Posted on:

Sunday 11th November 2012
View Replies!

write small currency converter

My country Ghana is changing its currency. I want to write a small programme in C++ that can covert from the old currency into the new one. I would like this programme to run behind a simple calculator- looking ...
VIEWS ON THIS POST

163

Posted on:

Monday 12th November 2012
View Replies!

convert string into an array

I'm trying to convert a string something like this "{201,23,240,56,23,45,34,23}" into an array in C++ Please help.
VIEWS ON THIS POST

83

Posted on:

Monday 12th November 2012
View Replies!

converting from IPADDRESS string to unsigned char array

On Sun, 30 Mar 2008 05:32:28 -0700, sam.barker0 wrote: > Hi , > I am trying to convert from an IPADDRESS string [say "12.12.1.2"]to a > unsigned char array[containing the octets witout the dots] > > I tried to use ...
VIEWS ON THIS POST

280

Posted on:

Sunday 25th November 2012
View Replies!

Fastest way to normalize doubles and convert to float?

I have a huge array of doubles. I want to normalize them to the range -1 to 1 converting them to floats in the process. Is there a 'best' / fastest way to do this Does C++ provide a way ...
VIEWS ON THIS POST

189

Posted on:

Sunday 25th November 2012
View Replies!

converting integer to a string(without the help of stringstreamvariable)

I am using a string variable in which I do lot of appending. The only difficulty I am facing as of now is appending a integer/float value to this variable. Although I can accomplish this task using a stringstream ...
VIEWS ON THIS POST

243

Posted on:

Sunday 25th November 2012
View Replies!

convert char * to char []

Help! I am trying to figure this out, and I have the impression that it should be really easy, but I'm at a loss. Here's my problem: I want to take the output from const char * string::c_str(); and convert ...
VIEWS ON THIS POST

282

Posted on:

Sunday 25th November 2012
View Replies!

Looking for a C/C++ header to XML file converter

Does anyone know of a good one Can you point me in the right direction...
VIEWS ON THIS POST

95

Posted on:

Monday 26th November 2012
View Replies!