C :: Convert Strings From Hex Values To Decimal Equivalents
Feb 8, 2014
I understand most of program below. Essentially, we have strings that we want to convert from hex values to decimal equivalents. We check if first two characters of string are 0x or 0X, which signifies hex format. If our hex string consists of solely digits like 0x25, then the processing is simple. We take the digit assign it to answer variable, and for each additional position in the hex base-16 system, we multiply the digit by 16.
Now if the hex string is something like 0x2A, then for 'A', the hexalpha_to_int() function is called, since we are able to find 'A' in the hexalpha string, we take the value of 'A', which is ascii 65 divide it by 2 and add 10 to it: 65/2+10=42.5. This doesn't make sense. What is the purpose of this logic right here: 10 + (i / 2).
Code:
#include <stdio.h>
#include <stdlib.h>
int hexalpha_to_int(int c){
char hexalpha[] = "aAbBcCdDeEfF";
How would you go about converting a decimal value to hex and then do math? Every example of converting decimal to hex that I have seen creates an array and I wouldn't be able to do math if I did that. Something like this.
15 decimal to hex F 17 decimal to hex 11 F hex + 11 hex = 20
I then had an follow up exercise which was to replicate but for any base up to 10, i thought i would just have to replace 2 with a variable obtained from the user, however this did not work as i got an error saying too few arguments function but i cannot see why i am getting this.
Code: #include <iostream> #include <iomanip> #include <cmath> using namespace std; float Conversion (int n, int b);
The goal of my program is to convert a decmial number to a binary number.First, the program gets an input to an array of chars, and the function translate_dec_bin converts this array to a decimal number through the strtoul function.The problem is that my program prints the binary number with an additional "0".For exmaple, for the input 1 - the program prints 01 instead of 1, for the input 3 - the program prints 011 instead of 11.
What is the difference between the two functions below? I created the function in the top and my friend created the function in the bottom. I just want to know why the function with the while loop prints the binary numbers backwards. And the recursive function prints the binary numbers correctly.
void findBinary(int num) { int remainder = 0; while ( num > 0) { remainder = num % 2; cout << remainder; num = num / 2;
#include "stdio.h" #include <stdarg.h> #include <math.h> // Main Function int main(void){ int number; printf(" Please enter a number from 1-10? "); scanf("%d", &number);
[Code] ....
I took the while statement out didn't want that in there.
This was an exercise from a book (convert a character string into a floating point value). It seems to work with both negative and positive decimal numbers.
It basically get each digit from the string multiplies by 10 to hold the place, and then adds the next to the result, if that makes sense.
Then determines where the decimal and null character are to figure out what to multiply by (1/1000 or whatever) to determine where the decimal should go. The variable names dealing with this part of the program aren't accurate names right now.
My question is, the output, is always putting 6 zero's. So if the argument is .95, Ideally the output should say .95 and not .950000, even though the value is still correct.
I know there the %.2f to determine the amount of decimals, but the amount of decimals in these instances would be varying depending on the argument sent to the function.
So, if 600.158 was sent as an argument, only 3 decimals would be displayed, as opposed to two from the previous example. Is there a way to do this?
Code: // Function to convert a string to an integer #include <stdio.h> double strToFloat (const char string[])
So I am trying to write a program that converts roman numerals into decimal numbers. I so far have come up with:
Code: #include <stdio.h> #include <ctype.h> // importing the tolowerfunction //variables int decimal, total; char numeral[];
[Code] .....
But each time I compile it, it times out as if it were hitting an infinite loop. I have a feeling that I am not passing an individual character to the roman_to_decimal function but am unsure why.
I nead to write a program that convert an octal number to decimal number, I thought I did it right but it doesn't work.. I have to use in the first for loop as it is because it is part of the instructions (student homework).
This program compiles, but has a bunch of logical errors. I know my problem is somewhere in the while loop that I have, but I can't figure out where. Here are some of the issues I am experiencing:
1. At the beginning of the program it asks you to enter a number, and when you do it does nothing while proceeding to the while loop where I have it asking the same question
Code: "Please enter a number between 1 and 20 (Enter 0 to stop) "; cin >> num; cout << endl;
I want to be able to eliminate that first statement but if I only run this in the loop without the above statement the program will display nothing on the screen and proceeds to stop.
2. This code runs fine, except that if you make a mistake, it will prompt you to enter a valid number, however; it ignores your first response if the number you enter is valid and asks you to enter a valid number anyway. Once you enter it a second time, it will accept it and the program will continue on.
Code: while(num != SENTINEL) { cout << "Please enter a number between 1 and 20 (Enter 0 to stop) "; cin >> num; cout << endl;
Also if you type in 0 on your first response, it will prompt you that it is not a valid number and ask you to try again, instead of stopping the program like it is supposed to do. On your second response the program will accept your 0 and stop the program correctly.
//Write a program that displays the roman numeral equivalent of any decimal number between 1 and 20 that the user enters. The roman numerals should be stored in an array of strings and the decimal number that the user enters should be used to locate the array element holding the roman numeral equivalent. The program should have a loop that allows the user to continue entering numbers until an end sentinel of 0 is entered.
Input validation: Do not accept scores less than 0 or greater than 20
#include <iostream> #include <string> using namespace std; int main() { // Declare constants and variables const int romanNum = 21; // Size of the elements in the array
Now as you can see that all the binary output is in a[] but how do I get it into a string so that I can use something like printf("%s",string) and get the binary output ?
I'm trying to pass a decimal number to a function and convert it to binary and return it and print it out in main. But it prints out 1011 and then seg faults...not sure where it's tripping up
Code: int main(){ char* binNum = decToBin(25); int i = 0; while(binNum != NULL){
I am trying to make a program that will convert a list of binary numbers from a file into decimal and print the decimal to the screen. I have no problem doing the conversion, the problem comes up when our teacher wants the input file in a format as such:
3 10110101 11111111 10101010
The first number is supposed to tell the program how many different 8bit strings it is going to have to convert, and then the following lines are those binary numbers.
I am not very experienced with file inputs, and I know how to open files and read lines in.. The problem is, how to say "ok the first line says 3, so now I have to convert the next 3 lines" . I am assuming it is just a simple loop that I am missing....
I was going through the exercises in one C programming ebook.There is this question which asks me to print a float variable in fixed decimal notation, field size of 6, right-justified, no digits after decimal point.I got printf("%6f", x );
x = float variable.
But the above code prints numbers after the decimal point, so I changed it to %d. But %d doesn't work with float variables..
i wanted to insert Decimal Values in MySQL database, but decimal values has precision and in C# there is not option to specify precision point, the last column is size which is integer.
I am trying to read hex data from a file (not just hex values in text format). I was able to read it (02 EC) with "fread" into an char before but I need to change the Hex value into an integer in decimal.
I already read about strtol but I would prefer reading the hex Value into an integer.
Here is my code so far:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int itrack1[1]; int itrack2[1];
I am trying to convert some chars to UTF-8 strings...
Example:
std::string gethex(char c) { /* EXAMPLE if (c == 'é') return "%c3%a9"; I need a function that converts chars like "á, é, í, ã" to UTF-8 hexadecimal strings... */ }
[Code] .....
[URL] .... does it. Choose UTF-8, type some character and click 'Url Encode'.
As a part of a program I am supposed to write, I would like to receive a string from the user (for example: "Hi my name is Joe").
obviously, the string is inserted to an array of chars (arr[0]='H', arr[1]='i', arr[2]=' ',... and so on).
What I would like to do, is to put each word separately in each array cell (for example arr[0]='Hi', arr[1]="my"..., and so on). How can I do this? (I can not use any functions, unless I write them myself).
I have a problem set where i have to read in numbers from a file as strings, convert from strings to integers, and pass the integers into a linked list, where each integer is a node. This is what I have so far:
Code: # include <stdio.h> # include <stdlib.h> # define MAX_INT_SIZE 10000 typedef struct integer BigInt; struct integer {
Code: Complete the program below which converts a binary number into a decimal number. Sample outputs are shown belowComplete the program below which converts a binary number into a decimal number. Sample outputs are shown below.
Sample Output 1:
8-bit Binary Number => 11111111 Decimal Number = 255
Sample Output 2:
8-bit Binary Number => 10101010 Decimal Number = 170
Sample Output 3:
8-bit Binary Number => 101010102 Number entered is not a binary number
#include <iostream> using namespace std; int main() { int num;