stuck converting a char to an integer
-
- Member
- Posts: 29
- Joined: Mon Jun 30, 2008 9:51 am
stuck converting a char to an integer
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
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
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:
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.
It's as if you were doing:
Code: Select all
int integer = another_integer & 0xff;
C8H10N4O2 | #446691 | Trust the nodes.
-
- Member
- Posts: 29
- Joined: Mon Jun 30, 2008 9:51 am
Re: stuck converting a char to an integer
so i cannot convert a char value back to an integer value?
or did i miss something in your post?
or did i miss something in your post?
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: stuck converting a char to an integer
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.
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
A variable in C is simply a storage area for a series of bytes. 'Types' are very 'undefined'.
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.
- char - 1 byte
- short - 2 bytes
- int - 4 bytes
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.
C8H10N4O2 | #446691 | Trust the nodes.
-
- Member
- Posts: 29
- Joined: Mon Jun 30, 2008 9:51 am
Re: stuck converting a char to an integer
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
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
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.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.
Anyway, good luck.
-
- Member
- Posts: 29
- Joined: Mon Jun 30, 2008 9:51 am
Re: stuck converting a char to an integer
yes i am aware of that, and i am doing that!
thanks!
chris
thanks!
chris
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: stuck converting a char to an integer
From my own experience, university C++ courses tend to teach cargo cult programming
Re: stuck converting a char to an integer
Code: Select all
unsigned char ch = 'X';
unsigned long ln = (unsigned char)ch;
Website: https://joscor.com
Re: stuck converting a char to an integer
Because the problem he has is he can't recover an int from a char, because of overflow...01000101 wrote:why not just do that?Code: Select all
unsigned char ch = 'X'; unsigned long ln = (unsigned char)ch;
so if he does for example:
Code: Select all
int x = 256;
char y = ((char)(x));
x = ((int)(y));
Jules
Re: stuck converting a char to an integer
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.Combuster wrote: From my own experience, university C++ courses tend to teach cargo cult programming
Free energy is indeed evil for it absorbs the light.
Re: stuck converting a char to an integer
Barely any ASM/C, my God, what is this world coming to...
Jules
Jules
-
- Member
- Posts: 29
- Joined: Mon Jun 30, 2008 9:51 am
Re: stuck converting a char to an integer
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
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
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....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
You should have made yourself clearer...
Jules