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";

[Code] ....

View 1 Replies


ADVERTISEMENT

C/C++ :: How To Convert A Decimal Value To Hex

Mar 27, 2014

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

View 8 Replies View Related

C++ :: Convert From Decimal To Binary With Base 2

Dec 7, 2014

I had an exercise to convert from decimal to binary with base 2, it was simple simple i did the following:

Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void Conversion (int n);

[Code] .....

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);

[Code] ......

View 5 Replies View Related

C :: Convert Binary Numbers To Decimal

Mar 27, 2013

The code should convert binary numbers to decimal but it doesn't..

Code:
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{

[Code] ....

View 4 Replies View Related

C :: Convert Decimal To Binary On Language

Nov 15, 2013

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.

Code:

#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 20
void translate_dec_bin(char s[]){
char st[sizeof(unsigned)*20] = { 0 };
}

[code]....

View 2 Replies View Related

C++ :: Function To Convert Decimal To Binary

Jul 11, 2014

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;

[Code] .....

View 3 Replies View Related

C/C++ :: Convert Decimal Number To Roman?

Sep 17, 2014

#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.

View 2 Replies View Related

C/C++ :: Convert Decimal To Another Base And Put It Into String

Jun 14, 2014

I have written this function.

void intToBase(unsigned int val,char *buff, unsigned int base) {
int digit = val%base;
if(val == 0)
return;
intToBase((val / base), buff + 1, base);
if(digit >= 0 && digit <= 9)
*buff = (digit + '0');
if(digit >= 10 && digit <= 15)
*buff = (digit + 'A' - 10);
}

This function is suppose to convert a decimal number (val) to any other base number and put it into a string (buff).

But the function puts the value in a opposite way, like if the answer is suppose to be "123" i get this "321".

Note: the function must be recursive and i can't use loops.

View 1 Replies View Related

C :: Function To Convert String To Floating Decimal

Dec 24, 2014

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[])

[Code].....

View 7 Replies View Related

C :: Convert Roman Numerals Into Decimal Numbers

Oct 11, 2013

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.

View 5 Replies View Related

C++ :: Program To Convert Octal Number To Decimal

Mar 27, 2013

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).

#include <iostream>
#include <math.h>
using namespace std;
void main() {
double numOfDig, num, newNum;

[Code] ....

View 1 Replies View Related

Visual C++ :: Roman Numeral Converter To Decimal Using Array Of Strings

Jun 27, 2013

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

[Code] ....

View 14 Replies View Related

C :: Convert Decimal Number To Binary And Store It In A String

Apr 16, 2014

I want to convert a decimal number to binary and store it in a string so that I can use that binary output for later computation. Here is what I did

Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()

[Code] ....

​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 ?

View 7 Replies View Related

C :: Pass Decimal Number To Function And Convert It To Binary?

Feb 26, 2013

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){

[Code].....

View 6 Replies View Related

C/C++ :: How To Convert Byte Array With Hex Number To Equivalent Decimal Value

Sep 18, 2012

I tried to convert byte array with hex to equivalent decimal value as follows but it gives me unexpected results:

byte hex_arr[] = { 0x00, 0x01, 0xab, 0x90};
unsigned long i=0;
i = hex_arr[3] + (hex_arr[2] << 8) + (hex_arr[1] << 16) + (hex_arr[0] << 24);
the output is 4294945680
Correct output should be 109456

but when I try with byte hex_arr[]={0x00,0x00,0x0f,0xff};

it gives correct out put 4095

the correct output works until the hex values is {0x00,0x00,0x7f,0xff}

View 8 Replies View Related

C++ :: Convert List Of Binary Numbers From A File Into Decimal And Print To Screen

Sep 23, 2014

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....

View 4 Replies View Related

C :: Possible To Print Float Values Without Decimal Point?

Apr 8, 2013

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..

View 2 Replies View Related

C# :: How To Insert Decimal Values In MySQL Database

Feb 26, 2014

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.

MySqlParameter MySqlParameterCollection.Add(string parameter Name,MySqlDBTYPE dbType,int size);

Here my Code and i get error on last Line

MySqlCommand insert = new MySqlCommand("Insert into
Plan(Plan_ID,Plan_Title,Plan_rate,Plan_Length)
values(@p_id,@p_t,@p_r,@p_l);", con);
insert.Parameters.Add("@p_id", MySqlDbType.VarChar, 10).Value = p.p_id;
insert.Parameters.Add("@p_t", MySqlDbType.VarChar, 45).Value = p.p_title;
insert.Parameters.Add("@p_r", MySqlDbType.Decimal,10,2)=p.p_rate;

View 7 Replies View Related

C/C++ :: Reading Hex Values From File And Printing Them In Decimal

Dec 1, 2013

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];

[Code] ....

View 11 Replies View Related

C# :: Align Integer / Decimal Values On The Left (Devex Gridview)

Mar 22, 2015

I want to learn ow to align an integer/decimal values on the Left inside devexpress gridview.

View 4 Replies View Related

C++ :: Convert Chars To UTF-8 Hex Strings

Sep 27, 2013

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'.

View 14 Replies View Related

C :: How To Convert From Array Of Chars To Strings

Jan 13, 2014

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).

View 8 Replies View Related

C :: Program That Uses Tolower To Convert Strings Into Lowercase?

Oct 6, 2014

Any simple program that uses tolower to convert strings into lowercase? Or simply just the general syntax of tolower pls. Can find it in google.

View 5 Replies View Related

C :: Convert From Strings To Integers And Pass Into Linked List

Feb 11, 2013

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] ......

View 10 Replies View Related

C++ :: Convert QT String Manipulation Function To Work With Strings?

Feb 15, 2012

I have the following function I would like to convert to work with std:string. I don't understand QT strings at all.

Code:
void FromHexString(const QString &hexText, void* pData, int dataSize) {
for (int i = 0; i < hexText.length(); ++i) {
bool ok = false;
((uint8_t*)pData)[ i ] = hexText.mid( 2*i, 2 ).toInt( &ok, 16 );
}
}

View 1 Replies View Related

C++ :: Convert 8-digit Binary Number To Decimal Number?

Jul 6, 2013

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;

[code]....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved