-
What is the best way to implement IOTA in CPP ? Using CPP Standards?
Given the following...I'd like to return a character version of a library call.
Code:
std::string afunctionthatcallsanunchangableLib(){
returnstring = convertitsimple(somelibraryfunc());
return(returnstring);
}
Thanks,
Dave
<<split from https://cboard.cprogramming.com/showthread.php?t=86470>>
-
-
Also,
This code...could it be made to work with integers easily?
I am trying to go from an integer to binary to a string.
http://faq.cprogramming.com/cgi-bin/...&id=1044780608
Regards,
Dave
to Sinkula:
thanks for the link...can you help with a better understanding of the overloading operator?
Code:
inline double convertToDouble(const std::string& s)
{
std::istringstream i(s);
double x;
if (!(i >> x))
throw BadConversion("convertToDouble(\"" + s + "\")");
return x;
}
and in
Code:
template <typename T>
T rev_bits ( T val )
{
T ret = 0;
unsigned int n_bits = sizeof ( val ) * CHAR_BIT;
for ( unsigned i = 0; i < n_bits; ++i ) {
ret = ( ret << 1 ) | ( val & 1 );
val >>= 1;
}
return ret;
}
-
If you want to get a the binary value of an integer, as a string:
Code:
#include <stdio.h>
#define size 100
void DecToBin(int num, char *str)
{
short num_bits = 32;
short start=0;
short i, bit;
for(i=num_bits-1; i>=0; i--)
{
bit=(num>>i) & 1;
if(start == 0) if(bit != 0) start=1;
if(start != 0)
{
str[start-1]=bit+'0';
if((i%8) == 0)
{
str[start]=' ';
start++;
}
start++;
}
}
str[start-1]='\0';
}
int main()
{
int num;
char binstring[size];
printf("Enter a whole number: ");
scanf("%i", &num);
DecToBin(num, binstring);
printf("%s", binstring);
getchar(), getchar();
}
-
You could always use a bitset, too, but that might be overkill.
-
>> If you want to get a the binary value of an integer, as a string:
Use std::bitset rather than coding a long implementation yourself.
>> can you help with a better understanding of the overloading operator?
Which overloaded operator?
Also note that if you have boost, then lexical_cast is preferable to your own version (like the example from the FAQ Dave_Sinkula linked to).
-
Mike,
Thanks for the code. I ran it on my end with no problems and it will integrate well into my application.
dave.thanks(10^6);
PS: found an error...won't work for zero :(
So added this at the very begining (other mods were made to handle the early return)
Code:
if(num == 0){
str[0]='0';
str[1]='\0';
return(str);
}
Daved,
> Which overloaded operator?
I don't know what this is saying.
val >>= 1;
-
It left-shifts val by one 1 bit and assigns the result to val (as any operator combined with = does). In mathematical terms that would be the equivalent of dividing by 2 (for unsigned integers).
As to overloading is concerned: you have probably met the >> operator with cin. Well, there it has been overloaded to be an insertion operator, but originally it is a bitwise operator.
-
Right shift, not left shift.
In addition, there are two different types of shifts:
- Logical
- Arithmetic
Logical is what when you want the entire number to be shifted over and treated as an unsigned number. Arithmetic is used when you want to keep the sign bit in place and preserve the overall "signess" or the number. The x86 processor has two instructions for each kind of shift, obviously one left and one right.
In C and C++, the decision to shift logically vs arithmetically is determined by whether or not the number to be shifted is signed or unsigned. Down at the assembly and hardware level, there is no such thing as signed and unsigned numbers except what the programmer does to interpret and use them as such.
-
> but originally it is a bitwise operator
Thanks to all.