Issues with GCC. Can't convert INT to a STRING.

Programming, for all ages and all languages.
TorakTu
Posts: 17
Joined: Tue Apr 16, 2013 7:47 pm

Issues with GCC. Can't convert INT to a STRING.

Post by TorakTu »

Ok.. here is the problem.. I can convert an int to a char.. GCC compiles it just fine.. HOWEVER.. I can't seem to add it to a string. So somehow I guess I am just not getting this.

I do not want to use any print functions as I am working on my own OS. So I am trying to solve this without exterior functions. However if there is a function I need to create, then that's what I gotta do. If I knew what I needed to do.

The standard :

Code: Select all

char j;
int a = 1;
j = (char)a;
this seems to work. BUT.. I try to add j with a to a string and the compiler complains.. big time. It says ( incompatible types when assigning to type 'char[80]' from
type 'char *' )
. So I am confused. OR I just don't know how to add a "byte" to a string. Which I thought was an array once I setup the array length. Which in this case is 80 bytes. I am fairly knowledgeable with C.. but... apparently not enough. :(

Code: Select all

#define MAX_STRING_LEN 80

void main(void)
{
   char STRI[MAX_STRING_LEN];

   char j[] = "The number is : ";
   int a = 1;
   STRI = j, (char *)a;

   // STRI should Equal ---->  "The number is : 1"
}
So I am just confused what I am missing.

My C compiler is GCC -DJGPP 4.7.2.
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Issues with GCC. Can't convert INT to a STRING.

Post by Mikemk »

shouldn't a equal 49? 1 is 'SOH', not '1'.
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
TorakTu
Posts: 17
Joined: Tue Apr 16, 2013 7:47 pm

Re: Issues with GCC. Can't convert INT to a STRING.

Post by TorakTu »

m12 wrote:shouldn't a equal 49? 1 is 'SOH', not '1'.

Where do you get 49 ? I am just trying to get he number 1 into the string. So I am confused where you get 49 from.. ?


EDIT : What I am guessing is your saying ASCII 49 as the number one. But no.. I am trying to get the literal number 1 into the integer and convert that to a char.. so I can ADD it to the end of a string.
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Issues with GCC. Can't convert INT to a STRING.

Post by Mikemk »

48 = '0', 48 + 1 = '0' + 1 = '1'
just like,
65 = 'A', 65 + 1 = 'A' + 1 = 'B'
and,
97 = 'a', 97 + 1 = 'a' + 1 = 'b'
ASCII
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
TorakTu
Posts: 17
Joined: Tue Apr 16, 2013 7:47 pm

Re: Issues with GCC. Can't convert INT to a STRING.

Post by TorakTu »

m12 wrote:48 = '0', 48 + 1 = '0' + 1 = '1'
just like,
65 = 'A', 65 + 1 = 'A' + 1 = 'B'
and,
97 = 'a', 97 + 1 = 'a' + 1 = 'b'
ASCII

lol we must have answered at the same time. . well so I am guessing your saying to put ' ' around the number 1 .. I tried that. it didn't convert it to character to be able to use in a string. Which is what i'm trying to do.

EDIT : Just for clarification .. when I had tried it with the ' ' it came out with same result : ( incompatible types when assigning to type 'char[80]' from type 'char *' )

Please keep in mind, I can program in C for windows just fine. But I am in a whole new world without the built in functions. printf of normal C libraries are easy to use. But since I am trying to make an OS, it changes things and I am trying to find ways around it since my OS doesn't have these libraries already. So do I need to make the conversion in assembler somehow or is it doable in C and I am just missing something in my code.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Issues with GCC. Can't convert INT to a STRING.

Post by gerryg400 »

Code: Select all

   STRI = j, (char *)a;
This line doesn't do what you want it to. You need something like

Code: Select all

strcpy(STRI, j);
strcat(STRI, "1");
or perhaps better

Code: Select all

sprintf(STRI, "%s %d", j, a);
Last edited by gerryg400 on Tue Apr 16, 2013 8:54 pm, edited 1 time in total.
If a trainstation is where trains stop, what is a workstation ?
TorakTu
Posts: 17
Joined: Tue Apr 16, 2013 7:47 pm

Re: Issues with GCC. Can't convert INT to a STRING.

Post by TorakTu »

gerryg400 wrote:

Code: Select all

   STRI = j, (char *)a;
This line doesn't do what you want it to. You need something line

Code: Select all

strcpy(STRI, j);
strcat(STRI, "1");

strcat () !! I didn't even think about that function. IT can append to the end of the string.. NICE. .i'll give that a try. Thank you.

EDIT UPDATE : I forgot, those are not built-in functions in C. Those belong to another library which I can't use.. I guess I'll have to make those functions.
Last edited by TorakTu on Tue Apr 16, 2013 8:57 pm, edited 1 time in total.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Issues with GCC. Can't convert INT to a STRING.

Post by gerryg400 »

Note that strcat can only append a string to a string. To put numbers into strings use sprintf as I described above.
If a trainstation is where trains stop, what is a workstation ?
TorakTu
Posts: 17
Joined: Tue Apr 16, 2013 7:47 pm

Re: Issues with GCC. Can't convert INT to a STRING.

Post by TorakTu »

gerryg400 wrote:Note that strcat can only append a string to a string. To put numbers into strings use sprintf as I described above.

As I mentioned, I can't use any printf commands as those are not able to be used. I am making an OS. So I can't use those library functions. I'm having to make these functions by hand. I'll just have to write an assembly code function and name it strcpy and strcat that do the same thing I guess. I was hoping there was something built into GCC without needing external libraries.

So basically I am back to my original question. What do the OS creator pros recommend ?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Issues with GCC. Can't convert INT to a STRING.

Post by gerryg400 »

You have 2 choices.

1. Compile and use an existing c library. Newlib is a very good choice.

2. Something else that's much more difficult.
If a trainstation is where trains stop, what is a workstation ?
TorakTu
Posts: 17
Joined: Tue Apr 16, 2013 7:47 pm

Re: Issues with GCC. Can't convert INT to a STRING.

Post by TorakTu »

gerryg400 wrote:You have 2 choices.

1. Compile and use an existing c library. Newlib is a very good choice.

2. Something else that's much more difficult.
Hmm, I was afraid that would be the answer. Guess number 2 is my only choice as I am trying not to use any one else's libraries.

Ok well back to the drawing board I go. At least I have a bootstrap that is in ASM and it boots up a kernel that prints to the screen in protected mode. Got this far by hand. Not using linux kernel or grub. But now I am working on int and string combinations and its a lot more fidgety then it looks.

Thanks for the help guys. I have read forum after forum on this very subject and apparently.. no one has done this without writing it in assembly.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Issues with GCC. Can't convert INT to a STRING.

Post by gerryg400 »

You don't need to use assembly. The standard c library can be written almost entirely in C. Certainly the string and print formatting functions can, and almost always are written entirely in c.
If a trainstation is where trains stop, what is a workstation ?
TorakTu
Posts: 17
Joined: Tue Apr 16, 2013 7:47 pm

Re: Issues with GCC. Can't convert INT to a STRING.

Post by TorakTu »

gerryg400 wrote:You don't need to use assembly. The standard c library can be written almost entirely in C. Certainly the string and print formatting functions can, and almost always are written entirely in c.

Yea, I would. The problem is, how ? From all the answers I have searched, every one of them requires you to use external libraries, most of those libraries are for windows only. Of which I can't use any of them in my OS.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Issues with GCC. Can't convert INT to a STRING.

Post by gerryg400 »

Several people on this forum have implemented c libraries from scratch. They are written in c and don't use any other c libraries apart from perhaps the internal libraries supplied by the compiler. Some people on this forum have even implemented those libraries.

I'm not really talking about what can and cannot be done. I'm just telling you what actually has been done.
If a trainstation is where trains stop, what is a workstation ?
TorakTu
Posts: 17
Joined: Tue Apr 16, 2013 7:47 pm

Re: Issues with GCC. Can't convert INT to a STRING.

Post by TorakTu »

gerryg400 wrote:Several people on this forum have implemented c libraries from scratch. They are written in c and don't use any other c libraries apart from perhaps the internal libraries supplied by the compiler. Some people on this forum have even implemented those libraries.

I'm not really talking about what can and cannot be done. I'm just telling you what actually has been done.
Aren't there licenses on those libraries though ? If by some LONG shot I was able to sell my tiny OS for what ever reason.. just for fun.. wouldn't I be bound to pay a fee to the creators of those said libraries ? I'm trying to avoid that.

On another note.. I wonder if I could do something like this pseudocode....

Code: Select all

a = 0
while(n != 0)
   answer[a] = n mod b
   n /= b
   a++
Convert the number to a binary, then bring it back in as a type of string by converting it back to ASCII into the end of the array as an appended byte. I know what I'm trying to say, I just probably am not clear about the way I am saying it. I'll have to experiment with this idea.
Locked