Converting char integer representations to binary [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a char array composed of elements 5 3. The array is used to represent number 53. What should be the approach to convert this number of three chars to its binary equivalent? I am implementing this in C, later on it will need to be rewritten in assembly. The solution I seek should be purely low stuff work without any helper libraries.
I am basically stuck with an idea to convert separately 5 and 4 (via mapping 5 and 4 to their ascii equivalents). Yet the idea would not work for sure. I have another idea to convert char '5' to int 5 by right shifting the byte by 4. Same with 4. Then multiply 5 by 10 and add 4, and then use division by two algorithm to find remainder and compose the binary number.

In C:
int asciToInteger(char *c)
{
int result = 0;
while (*c)
{
result *= 10;
result += (*c - '0');
c++;
}
return result;
}
Assumes input is valid.
You can get a head start on the assembly language version by compiling with certain switches which will output as ... assembly language! For example in GNU C: gcc -S -c ascii2int.c.

Related

How can I delete the first zero(s) in a string? (Without using atoi) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I was making a script that is calculating the additions between two natural numbers which decimal lengths should be smaller or same with 10000, and printing a result of the sum.
Of course, there ain't any variable type that can hold a integer which length is 10000 in C.
So, I made the program by utilizing the simple additions' calculating logic that all we learn in a school when we were young. And also, I just should use strings to get those gigantic numbers.
But some results were starting with zero. I knew why did the zero appeared there, but I did prefer to have a result that is like "1234", not "01234". By the way, all other stuffs were perfect.
I needed a function that gets input as string, and erases a single zero starts with a string if it exists.
And could you make it instead of me, please? You should probably consider that the strings we will deal with can have such a length that is smaller or same with 10000.
Maybe this:
char * f( char * str )
{
while ( *str == '0' && str[1] )
str++; // skips all zero-s when it is not last character in string
return str;
}

Return type of unsigned int function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a .so file which has C functions for RFID read card, and I'm calling the C functions in my python program.
The C function type is unsigned int get_card() which has unsigned int card as a variable, and returns the value in unsigned int format.
But when I call that C function in my python program, and print the returned value, I'm getting a negative value.
Can you please help on this?
This is because your python code “doesn’t knows” that the value is unsigned int, so if the number in binary starts with the number one, for example “10” that is equal to 2 in decimal, python will read it as “-0” in decimal. So You need to create in python an unsigned int variable and copy the return value in the new variable, then you can print it. Hope it helps you

Finding all solutions to equation using recursion [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I've been given a task to write program in C using recursion. The program is given an equation as a string, such as "123+123=246".
My task is to determine, whether the equation is right. The catch is, that numbers in the equation are sometimes replaced with a '?', for example "1??+??3=??6". Using recursion, I need to sum all possibilities, when the '?' is replaced with a digit and the equation is right.
Obviously, the solution will be based on trying out all possibilities and only selecting those, that make up the right equation. But I have no idea, how to implement it.
Could anyone give me a hint or reply with a piece of code I could base my solution on ?
Thanks very much !
Here's a general idea:
Basically we want to first of all be able to determine if the equation is right or not
Implement this function
int eval(char * string )
This will return 1 for true 0 for false and -1 when there are still '?'
Now we want write our recursion it will return a string and take a string
char * recursion (char * string)
First we need to see if the string holds a full equation.
Int res = eval(string);
if(res == 1)return string;
else if(res == 0)return "";
If it didn't stop yet that means that it can not determine because of the '?' , we need to find a way to kill them.
etch '?' can be 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9
Let's do a for loop
But first implement the method to replace the first '?' with a number,
char * Replace(char* string,int num);
After you've implemented this we create our for loop
for (int i = 0; i< 10 ; i++){
char * result =recursion(Replace(string , I));
if(Eval(result)==1) ;//we found a right answer add it to our return
}
return ""+ [all right answers we found if we even found ];
Good luck learning !

Extracting Numbers from String in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have an ASCII string coming from UART that looks something like "43.533a,5532" and I'd like to extract the two numbers. My idea was to separate them using strtok with the comma as delimiter and then remove the last character from the first string and afterwards convert the numbers using atoi() or is there an easier way with sscanf()? String manipulation is nothing I'm regularly using.
Another problem is, if the String looks different, how could I catch that beforehand?
Yes you can do this easily with sscanf().
Following is an example. See it working here:
#include <stdio.h>
int main(void) {
float a;
int b;
char *sNum = "43.533a,5532";
sscanf(sNum, "%fa,%d", &a, &b);
printf("a= %f || b= %d", a,b);
return 0;
}
Output:
a= 43.533001 || b= 5532
Note: Since float is having precision to 6 decimal place by default, so you may need to consider it and correct it if necessary.

C - Count the number of combinations possible in a string without repetitions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Good night, what's the best way in C to count the number of possibilities of UNIQUE anagrams in a string with the maximum length of 256 without allows repetitions caused by same letters? The input would be just in uppercase and only alphabet letters are allowed, A-Z. I got stuck in the worst case of the program that got 26! a very large number that overflow even my double. I think I'm very lost here, I'm not so good in C. The program just need to shows the number of possibilities, not the anagram. Like:
LOL = 3
HOUSE = 120
OLD = 6
ABCDEFGHIJKLMNOPQRSTUVWXYZ = 403291461126605635584000000
Thank you guys very much... I tried a lot and failed in every single tried, I'm in distress with it. The way I got more closer of do it was in Pascal, but it also failed in some tests, and I can't use Pascal anyway. I'm using CodeBlocks on Windows that compiles with GCC.
You should calculate factorial of the length of the given string divided by the factorial of the occurrence of every letter.
long double logFactorial (int i) {
return i < 2 ? 0.L : (logFactorial (i-1)+log(long double (i));
}
int countLetter(const char* str, char c) {
int res = 0;
while (str && *str) {
res += *str++ == c;
}
return res;
}
long double numPermutations(const char* str) {
auto res = logFactorial (strlen(str));
for (char c = 'A'; c<='Z'; c++) {
res -= logFactorial (countLetter (str,c));
}
return exp((long double)res);
}
Pay attention!
There are several people here who were correct by saying that factorial of 26 cannot be stored even in 64bit integer.
Therefore, I changed my calculation to the logarithmic of the factorial number and store it in long double which I hope is precise enough (I think that the exp() function is not precise enough)
Nevertheless you cannot use this result as an integer value, unless you find a way to store it in 128bit integer or bigger...
You should test it too if this fits your problem.
There is a faster way to calculate the factorial for this problem by storing the results of 0! up to 26! in an array of the size of [27].
I will leave it to you for asking another question.

Resources