Convert mpz_t to binary representation - c++

I'm using mpz_t for big numbers. I need to convert the mpz_t to binary representation. I tried to use the mpz_export, but the returned array contains only 0s.
mpz_t test;
mpz_init(test);
string myString = "173065661579367924163593258659639227443747684437943794002725938880375168921999825584315046";
mpz_set_str(test,myString.c_str(),10);
int size = mpz_sizeinbase(test,2);
cout << "size is : "<< size<<endl;
byte *rop = new byte[size];
mpz_export(rop,NULL,1,sizeof(rop),1,0,test);

Using gmpxx (since it's taged as c++)
#include <iostream>
#include <gmpxx.h>
int main()
{
mpz_class a("123456789");
std::cout << a.get_str(2) << std::endl; //base 2 representation
}
There should be equivalent function in plain GMP

You have a minor error in your code: sizeof(rop) is either 4 or 8, depending on whether a pointer is 4 or 8 bytes on your system. You meant to pass simply size, not sizeof(rop).
Here's some code that works for me, with g++ -lgmp -lgmpxx:
#include <stdio.h>
#include <iostream>
#include <gmpxx.h>
int main()
{
mpz_class a("173065661579367924163593258659639227443747684437943794002725938880375168921999825584315046");
int size = mpz_sizeinbase(a.get_mpz_t(), 256);
std::cout << "size is : " << size << std::endl;
unsigned char *rop = new unsigned char[size];
mpz_export(rop, NULL, 1, 1, 1, 0, a.get_mpz_t());
for (size_t i = 0; i < size; ++i) {
printf("%02x", rop[i]);
}
std::cout << std::endl;
}

Related

when i try to store a ranged value to a fixed width integer of 8 bit it shows me some other value [ASCII]

#include <iostream>
#include <cstdint>
int main() {
std::uint8_t i{5}; // direct initialization
std::cout << i;
return 0;
}
I could not able to get the value 5 rather I am getting some other.
Why this code gives me some other ASCII value rather than giving value 5?
Use
std::cout << static_cast<int>( i );
The type std::uint8_t is defined as an alias for unsigned char.
Here is a demonstrative program.
#include <iostream>
#include <cstdint>
int main()
{
std::uint8_t i { 65 };
std::cout << i << '\n';
std::cout << static_cast<int>( i ) << '\n';
}
Its output is
A
65

c++ reading argv into unsigned char fixed size: Segmentation fault

I am trying to read command line argument into a fixed size unsigned char array. I get segmentation fault.
My code:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <memory.h>
unsigned char key[16]={};
int main(int argc, char** argv){
std::cout << "Hello!" << std::endl;
long a = atol(argv[1]);
std::cout << a << std::endl;
memcpy(key, (unsigned char*) a, sizeof key);
// std::cout << sizeof key << std::endl;
// for (int i = 0; i < 16; i++)
// std::cout << (int) (key[i]) << std::endl;
return 0;
}
What am I doing wrong?
To call the program:
compile: g++ main.cpp
Execute: ./a.out 128
You get SEGV because your address is wrong: you convert a value to an address. Plus the size is the one of the destination, should be the size of the source
The compiler issues a warning, that's never good, you should take it into account because that was exactly your error:
xxx.c:12:38: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
memcpy(key, (unsigned char*) a, sizeof key);
^
fix that like this:
memcpy(key, &a, sizeof(a));
BTW you don't have to declare key with 16 bytes. It would be safer to allocate it like this:
unsigned char key[sizeof(long)];
and when you print the bytes, iterate until sizeof(long) too, or you'll just print trash bytes in the end.
Here's a fix proposal using uint64_t (unsigned 64-bit integer from stdint.h which gives exact control on the size), zero initialization for your key and parsing using strtoll:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <memory.h>
#include <stdint.h>
unsigned char key[sizeof(uint64_t)]={0};
int main(int argc, char** argv){
std::cout << "Hello!" << std::endl;
uint64_t a = strtoll(argv[1],NULL,10);
memcpy(key, &a, sizeof a);
for (int i = 0; i < sizeof(key); i++)
std::cout << (int) (key[i]) << std::endl;
return 0;
}
(if you want to handle signed, just change to int64_t)
Test on a little endian architecture:
% a 10000000000000
Hello!
0
160
114
78
24
9
0
0
Looks like you are copying too much data.
I also added a &a for the memcpy.
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <memory.h>
unsigned char key[16]={};
int main(int argc, char** argv)
{
memset(key,0x0, sizeof(key));
std::cout << "Hello!" << std::endl;
long a = atol(argv[1]);
std::cout << a << std::endl;
// the size parameter needs to be the size of a
// or the lesser of the size of key and a
memcpy(key,(void *) &a, sizeof(a));
std::cout << "size of key " << sizeof(key) << "\n";
std::cout << "key " << key << "\n";
for (int i = 0; i < 16; i++)
std::cout << " " << i << " '" << ((int) key[i]) << "'\n";
return 0;
}

C++ How to output int as 32-bit binary?

I want to output an int in 32-bit binary format. Is looping and shifting my only option?
Looping is a way. You can also use bitset library.
#include <iostream>
#include <bitset>
int main(int argc, char** argv) {
int i = -5, j = 5;
unsigned k = 4000000000; // 4 billion
std::cout << std::bitset<32>(i) << "\t" << std::bitset<32>(j) << std::endl;
std::cout << std::bitset<32>(k) << std::endl;
return 0;
}
And the output will be:
11111111111111111111111111111011 00000000000000000000000000000101
11101110011010110010100000000000

Converting char to int c++

I'm loosing my mind at the moment and below is what I'm trying to do.
char* buffer;
sprintf(buffer, "0x%08x", 5);
*(int *)(0x834AF2AC + 0x1a) = ?buffer?;
Buffer = 0x05000000
I need to set that in memory, if I just set 05 it will set 0x00000005
Question asked better.
How can I convert an INT into a format of "0x%08x"
So 5 becomes 0x05000000
ANSWERD:
The correct answer is *(int *)(0x834AF2AC + 0x1a) = 5<<24;
Something like this:
#include <iostream> // for std::cout, std::endl
#include <string> // for std::string, std::stoi
int main()
{
std::string s{"0x05"};
int i = std::stoi(s, nullptr, 16); // convert base 16 number in s to int
std::cout << i << std::endl;
}
Two result from google which points to stackoverflow (result 1 and 2).
Convert char to int in C and C++
C char* to int conversion
I'm not sure if I understand correctly but if you want to convert an entire string to int, then I would suggest stringstream.
http://www.cplusplus.com/reference/sstream/stringstream/stringstream/
For hexadecimal string:
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream
int main () {
std::stringstream ss;
ss << std::hex << 0x05;
int foo;
ss >> foo;
std::cout << "foo: " << foo << '\n';
return 0;
}

converting from strings to ints

I would like to know what is the easiest way to convert an int to C++ style string and from C++ style string to int.
edit
Thank you very much. When converting form string to int what happens if I pass a char string ? (ex: "abce").
Thanks & Regards,
Mousey
Probably the easiest is to use operator<< and operator>> with a stringstream (you can initialize a stringstream from a string, and use the stream's .str() member to retrieve a string after writing to it.
Boost has a lexical_cast that makes this particularly easy (though hardly a paragon of efficiency). Normal use would be something like int x = lexical_cast<int>(your_string);
You can change "%x" specifier to "%d" or any other format supported by sprintf. Ensure to appropriately adjust the buffer size 'buf'
int main(){
char buf[sizeof(int)*2 + 1];
int x = 0x12345678;
sprintf(buf, "%x", x);
string str(buf);
int y = atoi(str.c_str());
}
EDIT 2:
int main(){
char buf[sizeof(int)*2 + 1];
int x = 42;
sprintf(buf, "%x", x);
string str(buf);
//int y = atoi(str.c_str());
int y = static_cast<int>(strtol(str.c_str(), NULL, 16));
}
This is to convert string to number.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
int convert_string_to_number(const std::string& st)
{
std::istringstream stringinfo(st);
int num = 0;
stringinfo >> num;
return num;
}
int main()
{
int number = 0;
std::string number_as_string("425");
number = convert_string_to_number(number_as_string);
std::cout << "The number is " << number << std::endl;
std::cout << "Number of digits are " << number_as_string.length() << std::endl;
}
Like wise, the following is to convert number to string.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
std::string convert_number_to_string(const int& number_to_convert)
{
std::ostringstream os;
os << number_to_convert;
return (os.str());
}
int main()
{
int number = 425;
std::string stringafterconversion;
stringafterconversion = convert_number_to_string(number);
std::cout << "After conversion " << stringafterconversion << std::endl;
std::cout << "Number of digits are " << stringafterconversion.length() << std::endl;
}
Use atoi to convert a string to an int. Use a stringstream to convert the other way.