Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 755
  • Last Modified:

biginteger print out in decimal for c

Hello,
I looking for a way to printout a big integer in decimal format from a array, the index 0 is the LSB.
example 0xBC 0x8C 0xB7  = 12029116
but how can i manege this (LSB -> 3C69845D9EECF3B4A5E26644076A1C363C69845D9EECF3B4A5E26644076A1C36949050 7B9AC1E2965370)
i don't wont to use big-number libraries like GMP, just looking for an easy method  for decimal printout
0
_Thomas
Asked:
_Thomas
  • 3
  • 2
1 Solution
 
TommySzalapskiCommented:
How is the biginteger stored?
0
 
TommySzalapskiCommented:
Or check out how this guy did it and see if it works for you.
http://stackoverflow.com/questions/4735622/convert-large-hex-string-to-decimal-string
0
 
d-glitchCommented:
Converting Binary (or Hexadecimal) integers to decimal is straight forward but tedious.

But if you don't want to use a BIGINT library, you will have to write some subset of it yourself.

You have to choose the format of you output result, probably digit array or character string.
And you have to write functions for 2^n (or 16^n) as well as addition and multiplication.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
_ThomasAuthor Commented:
the biginteger is stored in memory from lsb to msb bits in line the integer is from 2048 bit and up to 8192 bit i looking for a size independent method .
typedef struct
{
 	uint8_t  sign;
	uint16_t size;
	void *integer;
} bignum;

Open in new window

void bn_init(bignum *nr, uint16_t size)
{
	nr->integer = malloc(size);
  	nr->size = size;
  	nr->sign = 0; //set positiov (0) 1= negativ
}

Open in new window

function for hex print out
void bn_print_msb(bignum *nr)
{
	void *p_integer;
	void *p_end;
	p_integer = nr->integer;
	p_end = p_integer + nr->size;
	p_integer--;
	p_end--;
	while(p_end > p_integer)
		printf("%02X ", *((uint8_t *)p_end--));
	printf("\n");
}

Open in new window

0
 
TommySzalapskiCommented:
That link I posted has code that will work perfectly for you. Either steal pieces for what you need or just use it outright (to do so you'll need to convert your hex to a std::string but you could do it easily like this:

std::string bn_to_string(bignum *nr)
{
        char* str = new char[nr->size*2+1]; // +1 for null terminator
        char* piece = new char[nr->size*2+1]; // +1 for null terminator

	void *p_integer;
	void *p_end;
        str[0] = '\0'; //Start it empty
	p_integer = nr->integer;
	p_end = p_integer + nr->size;
	p_integer--;
	p_end--;
	while(p_end > p_integer)
		sprintf(piece, "%02X ", *((uint8_t *)p_end--)); //get next piece
                strcat(str, piece);  //add it to the end
	printf("\n");

        return str; //Should convert to std::string, might need to use a cast or local var
}
                                            

Open in new window

0
 
_ThomasAuthor Commented:
the solutions is c++
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now