Page 2 of 2
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Tue Apr 16, 2013 10:18 pm
by Mikemk
TorakTu wrote:
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.
It depends on the library. Using a GPL library is NOT the way to go if you ever intend to have a remote chance of selling your os, as it requires projects using it to be open source and freely distributed. (If I'm not mistaken, the freestanding ones are exempt from this rule). Other libraries will allow you to use them with permission or attribution (Putting the author's name in an easily visible spot in your os - such as an about dialog). "Public domain" or "Copyright waived" libraries can be used for any purpose, commercial or personal, with or without attribution, etc, et al.
BTW, I am not a lawyer and am not offering legal advice.
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.
This would print your number backwards, but yes this is the standard method. What I do is make an empty character string large enough for the largest possible value + 1 and zero it (in base 10, 32 bits, unsigned):
Code: Select all
---------------------------------------------
| | | | | | | | | | | |
---------------------------------------------
Then, mod by 10 for the last digit and divide to get rest. Place the % value + 48 in the second to last position. (12345 is the number):
Code: Select all
---------------------------------------------
| | | | | | | | | | 53| |
---------------------------------------------
Continue:
Code: Select all
---------------------------------------------
| | | | | | 49| 50| 51| 52| 53| |
---------------------------------------------
trim the beginning of the array:
Code: Select all
-------------------------
| 49| 50| 51| 52| 53| |
-------------------------
And you now have a null terminated string with correct encoding ready for whatever it's being used for.
PS, bases > 10 are a bit more difficult. That's your homework.
EDIT: zeroes are represented as a space for readability.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 2:34 am
by dozniak
Code: Select all
std::string s{"hello"};
s += boost::lexical_cast<std::string>(1);
'ere you go.
PS. You can convert int to char because char is an integer type, string is not. This is not python, and compiler will not do things for you.
I am fairly knowledgeable with C.
There's at least one lie in here. I recommend you learn C.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 3:49 am
by Love4Boobies
This forum has reached a new low. Two things worth pointing out:
- char * is not the string type in C. C doesn't have a string type.
- char is not ASCII.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 4:16 am
by Griwes
dozniak wrote:Code: Select all
std::string s{"hello"};
s += boost::lexical_cast<std::string>(1);
'ere you go.
Did you mean std::to_string instead of so much more verbose lexical cast?
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 5:43 am
by dozniak
Griwes wrote:Did you mean std::to_string instead of so much more verbose lexical cast?
No, lexical_cast<> is not limited to integer types. In this case it will be more efficient to use to_string, if you use C++11, otherwise lexical_cast<> is slower but more portable.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 5:48 am
by TorakTu
Love4Boobies wrote:This forum has reached a new low. Two things worth pointing out:
- char * is not the string type in C. C doesn't have a string type.
- char is not ASCII.
Your correct. I said it wrong. I know what I was trying to say. I was knee deep in code and didn't say it correctly. But at least m12 got my meaning.
dozniak wrote:Code: Select all
std::string s{"hello"};
s += boost::lexical_cast<std::string>(1);
'ere you go.
PS. You can convert int to char because char is an integer type, string is not. This is not python, and compiler will not do things for you.
There's at least one lie in here. I recommend you learn C.
If you had bothered to read this thread, you would know that I can't use libraries. You are showing library functions there that I can't use. As for my knowledge of C, I would appreciate it if you keep your snide remarks to yourself. Your comment is not helpful by any means.
m12 wrote:
It depends on the library. Using a GPL library is NOT the way to go if you ever intend to have a remote chance of selling your os, as it requires projects using it to be open source and freely distributed. (If I'm not mistaken, the freestanding ones are exempt from this rule). Other libraries will allow you to use them with permission or attribution (Putting the author's name in an easily visible spot in your os - such as an about dialog). "Public domain" or "Copyright waived" libraries can be used for any purpose, commercial or personal, with or without attribution, etc, et al.
BTW, I am not a lawyer and am not offering legal advice.
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.
This would print your number backwards, but yes this is the standard method. What I do is make an empty character string large enough for the largest possible value + 1 and zero it (in base 10, 32 bits, unsigned):
Code: Select all
---------------------------------------------
| | | | | | | | | | | |
---------------------------------------------
Then, mod by 10 for the last digit and divide to get rest. Place the % value + 48 in the second to last position. (12345 is the number):
Code: Select all
---------------------------------------------
| | | | | | | | | | 53| |
---------------------------------------------
Continue:
Code: Select all
---------------------------------------------
| | | | | | 49| 50| 51| 52| 53| |
---------------------------------------------
trim the beginning of the array:
Code: Select all
-------------------------
| 49| 50| 51| 52| 53| |
-------------------------
And you now have a null terminated string with correct encoding ready for whatever it's being used for.
PS, bases > 10 are a bit more difficult. That's your homework.
EDIT: zeroes are represented as a space for readability.
So at least I am on the right track. Thank you m12. This helps.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 6:03 am
by dozniak
know that I can't use libraries. You are showing library functions there that I can't use.
If you want any sort of reusable functionality you have to write those libraries yourself (or port something like PDCLib).
As for my knowledge of C, I would appreciate it if you keep your snide remarks to yourself. Your comment is not helpful by any means.
It should be helpful. You're trying to look like you know C, but your attempt to sum together a string and a char clearly shows you're not. At least I know who's the liar here.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 6:05 am
by TorakTu
dozniak wrote:know that I can't use libraries. You are showing library functions there that I can't use.
If you want any sort of reusable functionality you have to write those libraries yourself (or port something like PDCLib).
As for my knowledge of C, I would appreciate it if you keep your snide remarks to yourself. Your comment is not helpful by any means.
It should be helpful. You're trying to look like you know C, but your attempt to sum together a string and a char clearly shows you're not. At least I know who's the liar here.
It was already established that I would have to write my own libraries. Again, READ the thread.
And I am unaware of what you mean by "I'M TRYING TO LOOK" like I know C. If you read this whole thread you would know what is going on. Again, your not being helpful. Calling people liars is uncalled for.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 6:08 am
by Combuster
TorakTu wrote:dozniak wrote:PS. You can convert int to char because char is an integer type, string is not. This is not python, and compiler will not do things for you.
There's at least one lie in here. I recommend you learn C.
If you had bothered to read this thread, you would know that I can't use libraries. You are showing library functions there that I can't use. As for my knowledge of C, I would appreciate it if you keep your snide remarks to yourself. Your comment is not helpful by any means.
Not knowing that you know is best.
Thinking that you know, while you don't, is a disease.
- Confucius (IIRC).
You demonstrated that you do not know basic C. Period. You can start a fight over it but that only causes more hostility rather than more support.
Complaining about not having a library: the functions are well defined and little more than stock utilities for those things programmers would be
guaranteed to end up writing over and over otherwise. Especially implementing string functions should be a really dull task for someone who knows how the fundamentals work.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 6:12 am
by TorakTu
Combuster wrote:TorakTu wrote:dozniak wrote:PS. You can convert int to char because char is an integer type, string is not. This is not python, and compiler will not do things for you.
There's at least one lie in here. I recommend you learn C.
If you had bothered to read this thread, you would know that I can't use libraries. You are showing library functions there that I can't use. As for my knowledge of C, I would appreciate it if you keep your snide remarks to yourself. Your comment is not helpful by any means.
Not knowing that you know is best.
Thinking that you know, while you don't, is a disease.
- Confucius (IIRC).
You demonstrated that you do not know basic C. Period. You can start a fight over it but that only causes more hostility rather than more support.
Complaining about not having a library: the functions are well defined and little more than stock utilities for those things programmers would be
guaranteed to end up writing over and over otherwise. Especially implementing string functions should be a really dull task for someone who knows how the fundamentals work.
As I stated right up front, I have a fairly good knowledge, but apparently not enough. I said this in my very first post. So I am not claiming that I KNOW C.. and I am some Master at it. Not what I said at all. And people calling me a LIAR IS hostility. Not helpful. All I did was ask him to be civil in his answers. Why am I the one being attacked ?
In my 2nd post you would know that I was not talking about a string. I just had said it wrong in other posts. m12 got what I am trying to do. His answer so far is the only one that was helpful. Seems the rest of you want to start some sort of flame war here. The MODs can close this thread. Because now its not productive at all.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 6:23 am
by dozniak
TorakTu wrote:As I stated right up front, I have a fairly good knowledge, but apparently not enough. I said this in my very first post.
Sorry, here we're getting to what I'm trying to tell for the good few posts - You Do Not Know C. Simple as that.
You started the thread by stating that you do not know anything about fundamental C types and how they are supposed to work.
I didn't want to quote you, but come on:
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.
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.
And as Love4Boobies correctly wrote, there isn't even a string type in C, only compiler-assisted kludge. But it has its own properties and features, and you clearly Do Not Know Anything about it.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 6:25 am
by TorakTu
dozniak wrote:TorakTu wrote:As I stated right up front, I have a fairly good knowledge, but apparently not enough. I said this in my very first post.
Sorry, here we're getting to what I'm trying to tell for the good few posts - You Do Not Know C. Simple as that.
You started the thread by stating that you do not know anything about fundamental C types and how they are supposed to work.
I didn't want to quote you, but come on:
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.
And as Love4Boobies correctly wrote, there isn't even a string type in C, only compiler-assisted kludge. But it has its own properties and features, and you clearly Do Not Know Anything about it.
I made the array ( Strings are similar to arrays with just a few differences ) with
STRI[80].
j is also an array ( OR STRING ) and I can successfully copy that to
STRI[80].. but
a being an integer won't work. THAT is the whole problem that I stated up front in this whole thread. Please READ.
m12 gave me the answer I needed.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 6:36 am
by dozniak
TorakTu wrote:m12 gave me the answer I needed.
He did as close as possible to help you, just follow his advice instead of spending time defending something you're not so sure about. Good luck.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 6:36 am
by TorakTu
dozniak wrote:TorakTu wrote:m12 gave me the answer I needed.
He did as close as possible to help you, just follow his advice instead of spending time defending something you're not so sure about. Good luck.
Thank you.
Re: Issues with GCC. Can't convert INT to a STRING.
Posted: Wed Apr 17, 2013 7:20 am
by AJ
Thread locked. I would suggest reading a good book on C and practicing more in the application domain first. Attempting to add strings together like that completely contracticts the claim of having good knowlege of C. My feeling is that using strcat et. al. at this stage is probably asking for buffer overruns until you understand more what is going on...
Cheers,
Adam