tags:

views:

199

answers:

1

Hi
the below code print the biginteger to a file in decimal format ,how do i convert the code into printing binary data and hex data into the File instead ??

static void
print_pos( FILE* f, bigint bi )
{
    if ( bi_compare( bi_copy( bi ), bi_10 ) >= 0 )
        print_pos( f, bi_int_divide( bi_copy( bi ), 10 ) );
    putc( bi_int_mod( bi, 10 ) + '0', f );
}

bi_10 is just a type of bigint , how do i modify the above code to print the hex / binary data instead of decimal data ?

+1  A: 

To print in base N, you need to make three changes:

  1. Instead of dividing by 10 the in the second line, divide by N
  2. Instead of doing mod 10 in the third line, do mod base N
  3. You will need to convert the the module from line 2 into the appropriate character. If you are doing base > 10, you will need to do an if/else

Here's what it would look like using plain integers. I'll let you make the appropriate changes to use your big int library:

static void
print_pos( FILE* f, int n, int base )
{
    if (n < 0)
    {
        n *= -1;
        putc( '-', f );
    }

    if (n >= base)
        print_pos( f, n / base, base );

    int d = n % base;
    const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    putc( digits[d], f );
}

A couple of last comments.

  1. Your original code forget to deal with negative numbers.
  2. This function works up to base 36. If you want even larger bases, you'll need to extend the char c = ... line.
R Samuel Klatchko
+1, Nice answer. Note that 'A' + 1 is not guaranteed to be 'B' (or in general, 'A'..'Z' need not be continuous). '0'..'9' are guaranteed to be continuous though.
Alok
@Alok - I know in theory that neither the letters nor the digits need to be sequential. That said, do you know of any encoding still in common use that doesn't do that? That said, I've changed to an array to make that work regardless of the encoding.
R Samuel Klatchko
digits have to be sequential because the C standard says so. For letters, EBCDIC is an example where the letters aren't contiguous in their values. Quote Wikipedia page on EBCDIC: *All IBM mainframe peripherals and operating systems (except Linux on zSeries or iSeries) use EBCDIC as their inherent encoding.*
Alok
I need to get in BINARY FORMAT (byte values?) if base 2 will give only 1 and 0 . but i need in unreadable binary format , which base has to be used for that??
If by unreadable binary format, you mean the raw data, then how you get that will be dependent on your bigint implementation. If you are using the bigint implementation from axTLS, look at bi_export.
R Samuel Klatchko
Thanks Samuel , i am using "Jef Poskanzer" bigint implementation which is slightly different but does not have bi_export method I am trying to make it from axTLS , still debugging ! i can see the byte values is getting stored in the char* , but later i need to print to verify. does %s can print it in raw format?
This is now way far away from the original question. Please start a new question.
R Samuel Klatchko