Glib number conversions - c

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

Related

In C, how can you get an int with leading zeros?

I need to generate a pin number, between 0 and 9999;
0's are important since I'm gonna use this pin to encrypt some files, and encrypting with '0024' is different than encrypting with '24'
I'm using an unsigned int and it's isn't working..
Is the only way an array?
You can't. An int is an integer, and for integers it really doesn't make sense to talk about leading zeros. They don't exist, an integer (in the mathematical sense, which is what int is trying to model on a computer) basically cannot have leading zeros. See comments for more pedantry about this, I tried to simplify it reasonably but might have failed since I'm just a lowly programmer and not a mathematician.
All the bits available for the int are used to store actual value bits, none are available to store that kind of representational information.
It sounds as if you want a string, or an array of digits.
Convert your integer value as a string and do formatting so it will return integer with leading zero in string format.

Program Wont Work As Expected

I have an issue here. I have to make a program that checks if a credit card number is valid or not using the checksum. I'm supposed to multiply every other digit starting from the second to last digit, then add the products then add the numbers that weren't multiplied to that sum as well. The result should produce 0 when divided. However, Im having an issue here with my program. When I enter large numbers, the values change up and at the end I get Floating point exception(core dumped). When I enter smaller numbers, sometimes it works, sometimes it doesn't. Please help me out.
Thank you for your help. Please explain the issue so I can avoid it later.
I think you may have a problem when iterating i up to a large cardNum because cardNum is long long which can hold huge numbers but int i is just an int relatively small.
To solve this problem try holding the cardNum as a string extracting each digit from it and parsing them into an int. You can then multiply and add them up without dealing with representing huge numbers.
cardnum needs to be a string not a number, you are looping over it expecting to get each digit. YOur for loop will give you all the number from 1 to the credit card number (a long loop)

Convert the integer value to hex value

I have this function in xilinx for giving output to Seven segment.
int result;
XIo_Out32(XPAR_SSG_DECODER_0_BASEADDR, result);
The function gets the int result and puts the output to seven segment as a hex value. So basicly, if i give result = 11; I would see A as a result in seven segment. To see a decimal value on sseg, one approach is to change the verilog code behind this and change the whole concept of the sseg. Another approach is to write a function that changes decimal value into a hex value. I've been searching for a good code block for this but it seems that every one of them, prints the values digit by digit with a loop. I need the whole value as a block. Unfortunately i cannot use the C++ libraries so i have primitive C code. Is there any known algorithms for converting?
Apparently, you want to convert symbol codes from ASCII to the ones from the 7-segment display character set. If so, you may create a simple mapping, maybe an array of codes indexed by ASCII character id. Then, you'll be able to call your function like:
XIo_Out32(XPAR_SSG_DECODER_0_BASEADDR, 'A');
Be careful to implement the mapping table for the whole ASCII range.
EDIT
Sorry, I've got your question wrong. You'll have to manually convert hexadecimal number to an array of decimal symbols. You may do it by dividing your number by increasing powers of 10 (10^0, 10^1, 10^2, etc) and thus get an array of remainders, which is a decimal representation of your number. You may use snprintf as H2CO3 recommends, but I would recommend against it in some of the embedded applications where RAM is limited; you may even be unable to use sprintf-like functions at all.

Converting Base64 to GMP integer

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.

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