stuck converting a char to an integer

Programming, for all ages and all languages.
cjhawley001
Member
Member
Posts: 29
Joined: Mon Jun 30, 2008 9:51 am

stuck converting a char to an integer

Post 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
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: stuck converting a char to an integer

Post 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.
C8H10N4O2 | #446691 | Trust the nodes.
cjhawley001
Member
Member
Posts: 29
Joined: Mon Jun 30, 2008 9:51 am

Re: stuck converting a char to an integer

Post by cjhawley001 »

so i cannot convert a char value back to an integer value?

or did i miss something in your post?
User avatar
Combuster
Member
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

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: stuck converting a char to an integer

Post 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.
C8H10N4O2 | #446691 | Trust the nodes.
cjhawley001
Member
Member
Posts: 29
Joined: Mon Jun 30, 2008 9:51 am

Re: stuck converting a char to an integer

Post 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
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Re: stuck converting a char to an integer

Post 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.
cjhawley001
Member
Member
Posts: 29
Joined: Mon Jun 30, 2008 9:51 am

Re: stuck converting a char to an integer

Post by cjhawley001 »

yes i am aware of that, and i am doing that!

thanks!

chris
User avatar
Combuster
Member
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

Post by Combuster »

From my own experience, university C++ courses tend to teach cargo cult programming :(
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: stuck converting a char to an integer

Post by 01000101 »

Code: Select all

unsigned char ch = 'X';
unsigned long ln = (unsigned char)ch;
why not just do that?
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: stuck converting a char to an integer

Post 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
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Re: stuck converting a char to an integer

Post 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. :)
Free energy is indeed evil for it absorbs the light.
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: stuck converting a char to an integer

Post by suthers »

Barely any ASM/C, my God, what is this world coming to... [-o< :shock:
Jules
cjhawley001
Member
Member
Posts: 29
Joined: Mon Jun 30, 2008 9:51 am

Re: stuck converting a char to an integer

Post 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
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: stuck converting a char to an integer

Post 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
Post Reply