Converting Base64 to GMP integer - c

I have a scenario where the I get a Base64 (64 bit encoded) string. My requirement is to convert this string to gmp integer (mpz_t).
But according to GMP manual only "The base may vary from 2 to 62" for the function mpz_set_str() .
Is there any approach I can follow to perform a successful conversion?
One idea that struck me was to convert the Base64 to binary and then set the mpz_t variable using mpz_set_str with base 2.
Help would be really appreciated. Thanks.

GMP bases are not the same thing as base64 encoding. You're on the right track - apply the base64 decode, then use mpz_import on the result.

Related

Convert number in scientific notation string to float with limited C-like library

I'm writing a program in CAPL (which is based on C and minus some concepts) to convert a string containing a number displayed in scientific notation to a float (doesn't strictly need to be a float but I think its an appropriate type for this). For example:
-7.68000000E-06 should be converted to -0.00000768
I've done some looking around for this and atof() comes up a lot but this is not supported in CAPL so I cannot use that.
A list of the other C concepts not supported in CAPL:
Update: Thanks everyone for the help. M. Spiller's answer proved to be the easiest solution. I have accepted this answer.
In CAPL the function is called atodbl with the same signature as C's atof.

C: Read Binary and Write as encoded text

I wish to read a jar in binary mode and write the binary as string(encoded if need be) to a file.
This string would be included in code to be read and converted back into binary and written as a jar file.
I'm trying to replicate what get-pip.py has done to distribute pip.
What would be the best way to do this in C?
You can use base64 encoding for this. Base64 is a standard, and you can use the encoder from one implementation, and the decoder from another, and you'd get back the original string on the other side. The typical straightforward implementation is a table driven one, as in this answer here: https://stackoverflow.com/a/6782480/1212725

Glib number conversions

Is there a way how to convert a number to array of lower numbers and back? For example converting guint64 to glong and back.
Thank you very much for your help

How do I decode an email subject

There is a problem parsing the subject in the mail header.
For example, the form of the subject is as follows.
subject: =?iso-2022-KR?B?DjlMOC4PIA....gyDzogT?=
My guess is that base64 decoding should include the escape character -SO, SI, ESC$)C-. However, decoding is not included.
How can I get a normal string?
I hope the results are as below.
Subject: like this, 안녕하세요.
Please give me a hint how to respond at the code level. in C
Update
sorry. I had a SO, SI, but I missed it. But there was no ESC$)C, The problem is resolved immediately and shared for others.
In the absence of a ESC$)C, the libiconv is a problem, but the gconv(in glibc) was not a problem. What I used was the libiconv. Changing to gconv has solved the problem.
thanks.
So in =?iso-2022-KR?B?DjlMOC4PIA....gyDzogT?= the Bsandwiched by question marks means base64 encoded. The iso-2022-KR is the character set. The DjlMOC4PIA....gyDzogT is the base64 encoded title.
You first base64 decode the title. It's easy to find a solution for this in C.
This will leave you with an array of binary bytes which is the title encoded in the ISO-2022-KR character set. Presumably you want to convert that to UTF-8 or some other character set your computer can handle. Your best bet for this part is to use a character set conversion utility. If you are on Linux or macOS, you can use the iconv library. See iconv_open, iconv and iconv_close.

Print a number in base 4

Lately I had a task that included printing base-4 representation of a number. Since I didn't find a function to do it for me, I implemented it (which is not so hard of course), but I wonder, is there a way to do it using format placeholders?
I'm not asking how to implement such function, but if such function / format placeholder already exists?
There is no standard C or C++ function, but you may be able to use itoa
The closest you could get to doing it with printf is using snprintf to convert it to hex, then a lookup table to convert hex digits to pairs of base-4 digits. :-)
No, not in the Standard C library.
I think that printf can handle only decimal, hexadecimal and octal values.
So i think no.

Resources