Page 1 of 2
stuck converting a char to an integer
Posted: Wed Jul 02, 2008 12:32 pm
by cjhawley001
so i have been able to convert an integer to a char value, but i cannot reverse the process and convert a char to an integer.
i am making a simple calculator to start off small, in hopes of growing to a system that has many math procedures the user can access and help solve equations/problems
any help?
-chris
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 12:35 pm
by Alboin
When you convert the integer to a character, you loose the upper 3 bytes of the integer. (assuming a char is 1 byte and an integer is 4.) Thus, when you convert it back, you're only getting the lower byte.
It's as if you were doing:
Code: Select all
int integer = another_integer & 0xff;
In fact, converting an integer to a char shouldn't work in itself, unless the value is less than or equal to 0xff. (Being that the char is unsigned. Being signed, you'd have a range of -128 to 128.) Moreover, in this case, converting back would work fine as well.
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 12:45 pm
by cjhawley001
so i cannot convert a char value back to an integer value?
or did i miss something in your post?
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 12:50 pm
by Combuster
Given your previous question, I want to remark that you do not seem to grasp all the basics of C. Since OS development requires a very good understanding of the language you want to develop your OS in, this may not be the best choice at this time.
Consider running down several tutorials (from beginners to advanced) and writing a few programs on your own before trying to pursue this challenge once more.
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 12:53 pm
by Alboin
A variable in C is simply a storage area for a series of bytes. 'Types' are very 'undefined'.
- char - 1 byte
- short - 2 bytes
- int - 4 bytes
These values are probably what you're working with. (Use sizeof to check if you really care.) Each byte has 8 bits. If a byte is signed, the byte's most significant bit, or the highest bit, is used to determine whether or not the value is negative or positive, and this, obviously, impacts the range of the variable.
What you're saying is that you want to store 4 bytes in 1. (int -> char) Storing 1 in 4 is easy (char -> int), but not the opposite.
So, yes, you can cast a char to an int, but when you're casting an int to a char, you must be careful that your int uses no more than 1 byte of its space.
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 2:04 pm
by cjhawley001
alright, thank you very much. Now i understand.
Also, combuster, i know C++, i took it last year in highschool, and am just in converting stage between C and C++, there are just some issues that have come up when programming my OS though, such as that, but i have gotten decently far in my OS with my own personal code.
But i do appreciate the comments and will pause development for a few weeks and reaserch more about C and assembly.
Thank you all for taking time to answer!
-Chris
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 4:35 pm
by iammisc
cjhawley001 wrote:
Also, combuster, i know C++, i took it last year in highschool, and am just in converting stage between C and C++, there are just some issues that have come up when programming my OS though, such as that, but i have gotten decently far in my OS with my own personal code.
It doesn't matter. The primitive types in C++ are the same as in C and to convert between int and char in C++ and C is the exact same. Also, unless your highschool has an insanely detailed programming course, you probably have not been introduced to
everything about C++. To fully grasp and understand C++ you should really understand C. Altogether, unless you are spending all your time on a computer, you should really consider spending a couple of years getting extremely familiar with C. I have found through hard experience that the only way to learn any programming language well is to actually use it. I highly recommend that you first develop user space applications before you even attempt to write an Operating System. Listen to the general advice and take a hiatus on your operating system development to learn the nuances of the programming languages and architecture of the machine you are going to write for.
Anyway, good luck.
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 4:40 pm
by cjhawley001
yes i am aware of that, and i am doing that!
thanks!
chris
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 4:45 pm
by Combuster
From my own experience, university C++ courses tend to teach
cargo cult programming
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 7:35 pm
by 01000101
Code: Select all
unsigned char ch = 'X';
unsigned long ln = (unsigned char)ch;
why not just do that?
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 7:41 pm
by suthers
01000101 wrote:Code: Select all
unsigned char ch = 'X';
unsigned long ln = (unsigned char)ch;
why not just do that?
Because the problem he has is he can't recover an int from a char, because of overflow...
so if he does for example:
Code: Select all
int x = 256;
char y = ((char)(x));
x = ((int)(y));
x will be equal to 1 at then end...
Jules
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 7:56 pm
by Omega
Combuster wrote:
From my own experience, university C++ courses tend to teach cargo cult programming
Cargo Cult is just being nice. The Uni CS classes (at least where I am at) are just ridiculously weak. I don't buy books or even go to my CS classes, except for the exams (we turn our work in via Black Board) and I still get A's and B's. I tested out of all the intro classes and the so called advanced classes are just plain boring, because they move way too slow and teach you almost nothing about coding (except the basics). The Uni does not prepare creative and efficient programmers. They teach us Java, (f-ing Java for crying out loud!), they might as well just teach us VB on top of it and we will have officially learned the two most worthless languages on the planet. We barely touch ASM and C which is a sin. I can honestly say that I have learned more from this forum in 6 months time than I have from college in 2 years. It is just to bad you guys don't offer credit.
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 8:02 pm
by suthers
Barely any ASM/C, my God, what is this world coming to...
Jules
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 8:06 pm
by cjhawley001
i think i have solved my own problem...i don't know how efficient it is but here it goes
char charecter = '2';
int integer;
integer = (int)(charecter - '0');
then i just treat it as an Integer...
...its not efficient i realize...but it works fine when i run my OS and test it...
any help or comments(bad or good) welcome..
Thank you! also my highschool is bad at it too, i tested out of vb basic and did C++ and vb advance in one year but when i witnessed the intro to vb...wow....it hurt...
-chris
Re: stuck converting a char to an integer
Posted: Wed Jul 02, 2008 8:09 pm
by suthers
cjhawley001 wrote:i think i have solved my own problem...i don't know how efficient it is but here it goes
char charecter = '2';
int integer;
integer = (int)(charecter - '0');
then i just treat it as an Integer...
...its not efficient i realize...but it works fine when i run my OS and test it...
any help or comments(bad or good) welcome..
Thank you! also my highschool is bad at it too, i tested out of vb basic and did C++ and vb advance in one year but when i witnessed the intro to vb...wow....it hurt...
-chris
We could have told you how to do that in seconds, we assumed you meant at numerical level not converting an ASCII char which was a number back into a binary number....
You should have made yourself clearer...
Jules