# got a problem in ltoa ()

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 01-17-2009
m37h0d
this is a better base conversion algorithm:

Code:

std::valarray<unsigned int> baseConvert(unsigned int value,unsigned int base)
{
unsigned int digits = 1+log((double)value)/log((double)base);
std::valarray<unsigned int>result(digits);
for(int i=0;i<digits-1;i++)
{
result[i]=value%base;
value/=base;
}
result[digits-1]=value;
return result;
}

check out the method of complements
• 01-17-2009
Masterx
Quote:

Originally Posted by laserlight
Why not have Oct2Bin take a string as its argument? If you really want it to take an int as an argument then you are merely expressing the number in binary as opposed to converting it from an octal representation to a binary representation.

the reason i take an integer argument is that , this is an emulation project , this simple computer uses octal system to manipulate stuffs, and any program written in the language of this system must use octal! so i assume the user inputs his data, now its the time to convert the stuff to the machine language and store it in memory! thats why im trying to convert an integer octal number to its binary string , so that i can dump this low-level instruction in memory!

Quote:

Originally Posted by m37h0d
this is a better base conversion algorithm:

Code:

std::valarray<unsigned int> baseConvert(unsigned int value,unsigned int base)
{
unsigned int digits = 1+log((double)value)/log((double)base);
std::valarray<unsigned int>result(digits);
for(int i=0;i<digits-1;i++)
{
result[i]=value%base;
value/=base;
}
result[digits-1]=value;
return result;
}

check out the method of complements

many thanx pal . i appreciate it.

i really need to know why my program crashes! please some one help me ! this part plays a crucial role in my project! if i fail to solve this , my project fails to dump instruction in machine language (binary format) into the memory (a 2D array)!!:(
• 01-17-2009
m37h0d
well, first thing's first:

quit doing it the hard way and see if your problem goes away. if not, then debugging will be easier!

also, i don't see why you're forcing the user to input something in valid octal representation - just convert dec->oct. any number is a valid octal number, it just may be represented in a different format (in our case, decimal)
• 01-17-2009
laserlight
Quote:

Originally Posted by m37h0d
this is a better base conversion algorithm

No, it is a more general base conversion algorithm, but "worse" in this case since it does not take advantage of the one to one mapping between octal digits and three consecutive binary digits.

Quote:

Originally Posted by Masterx
the reason i take an integer argument is that , this is an emulation project , this simple computer uses octal system to manipulate stuffs, and any program written in the language of this system must use octal!

That does not seem like a convincing reason to use an int argument. Once you have stored it as an int value, the original representation of the input no longer matters.

Quote:

Originally Posted by Masterx
so i assume the user inputs his data, now its the time to convert the stuff to the machine language and store it in memory! thats why im trying to convert an integer octal number to its binary string , so that i can dump this low-level instruction in memory!

Why waste time reading the input as an int and then converting the int to a binary string? You might as well read the input as a numeric string in octal representation and then do the conversion to binary representation directly.

Quote:

Originally Posted by Masterx
i really need to know why my program crashes!

Use a debugger to step through the program. This can help you pinpoint the crash.
• 01-17-2009
m37h0d
arguably so i suppose but i'm not totally convinced that's a substantive advantage
• 01-17-2009
Masterx
Quote:

Originally Posted by m37h0d
well, first thing's first:

quit doing it the hard way and see if your problem goes away. if not, then debugging will be easier!

also, i don't see why you're forcing the user to input something in valid octal representation - just convert dec->oct. any number is a valid octal number, it just may be represented in a different format (in our case, decimal)

this feature is going to be implemented ! (some part of the program now supports decimal input too, )
Quote:

Originally Posted by laserlight
No, it is a more general base conversion algorithm, but "worse" in this case since it does not take advantage of the one to one mapping between octal digits and three consecutive binary digits.

That does not seem like a convincing reason to use an int argument. Once you have stored it as an int value, the original representation of the input no longer matters.

Why waste time reading the input as an int and then converting the int to a binary string? You might as well read the input as a numeric string in octal representation and then do the conversion to binary representation directly.

Use a debugger to step through the program. This can help you pinpoint the crash.

its done.i'll do it this way. so far so good.
before i head for debugging stuff(which i dont know how the stuffs work !) is it right to say that the program is ok ?. i mean i didnt do anything contrary to the basics of C++!?
• 01-17-2009
laserlight
Quote:

Originally Posted by Masterx
is it right to say that the program is ok ?. i mean i didnt do anything contrary to the basics of C++!?

Well, what is the current code for which you want such an evaluation to be made?
• 01-18-2009
Masterx
ive finaly figured it out! the crash is because of string str;
in the loop
Code:

for (int i=0;i<9;i++)
{
memory[4][i]=str[i];

}

i tried to access the str by using index ! and it showed me that string str is rather different than string str[length] !! so that was the catch!
after that i noticed that the function doesnt return anything ! at least if in returns anything , that thing is empty!! why ? because i tried to copy str elements to a buffer using
Code:

char buffer[12];
int length=0;

length=str.copy(buffer,9,0);
buffer[length]='\0';

and then tried to print the stuffs (buffers) on the console screen! but nothing was printed!
well where is the catch this time ? is there any other ways to copy this (string str) form of string to another string (like string str1[n])?
• 01-18-2009
Masterx
Quote:

Originally Posted by laserlight
Well, what is the current code for which you want such an evaluation to be made?

the codes we were discussing !here:
for (int i=0;i<9;i++)
{
memory[4][i]=str[i];

}
(it seems i was wrong , idid it the wrong way .)
• 01-18-2009
manzoor
Quote:

Originally Posted by Elysia
If you work with arrays, use std::tr1::array or boost::array.

Can you explain why? What's wrong with the built in arrays?
• 01-18-2009
Elysia
They are unsafe (at least boost::array will assert if you access out-of-bounds), does not provide you any means to get its length, and does not always work with well standard containers.
• 01-18-2009
laserlight
Quote:

Originally Posted by manzoor
What's wrong with the built in arrays?

• 01-18-2009
Masterx
what about me then ? any help?! how to copy str struffs to a char * based string?!!
• 01-18-2009
laserlight
Quote:

Originally Posted by Masterx
what about me then ? any help?! how to copy str struffs to a char * based string?!!

Why do you want to use C style null terminated strings in the first place? I am afraid that I find your posts #23 and #24 to be rather incoherent due to the lack of context.
• 01-18-2009
Masterx
well, in post 23 and 24 im saying that because i declared str as "string str" i couldnt use subscripting to point to each elements of str and then copying them to another string .
when i tried to use these commands
Code:

for (int i=0;i<9;i++)
{
memory[4][i]=str[i];

}

, the application compiles fine , without any error, but at runtime it crashes! so far i explained post 23 . and 24
now why C Style null terminated string ! because to me they are easier to manipulate . i can convert them to integer if needed (using atoi() , which i cant use string str sytle ) and i can point to any element of it , whenever i want to ( using *(ptr+counter) ) while i cant use string str format !

see to me C Style strings are awesome , I as a rookie dont need to do stuffs the hard way and get stuck in the middle of the way ! and you know thats because i dont know much about C++ and its strings . (and sadly im on a project that needs these knowledge ! ).

now , i used that string str format , and i need to copy the stuff at least to another string that can be subscripted!(like string str2[10]) .

hope it is clearer now .
(and most of the strings manipulations are done using C Style strings in my project so far ) .
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last