473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Hexadecimal To Decimal Conversion (Via Char Array)


hey there,

I was able to read a char string containing only digits and convert it
to its int equivalent. however now I need to do the same thing with a
char string containing a hexadecimal number.

below is my clumsy attempt. I think I have most of the problem covered
but I am finding it somewhat difficult to convert the 'a' through 'f'
values correctly. any suggestions anyone has would be tremendously
appreciated.

thx

a newb


#include <iostream>
using namespace std;

int sixteen_power(int x);

int main() {
const int max_size = 100;
char char_array[max_size];
int int_array[max_size];
int i = 0;
int array_size = 0;
int counter = 0;

bool hexa = false;

cout << "Enter a numeric string. The maximum size is 100
characters.\n\n";

cin.getline(char_array, max_size);

if(char_array[0] == '0' && char_array[1] == 'x') {
hexa = true;
i = 2;

while(counter < strlen(char_array)) {
char_array[i - 2] = char_array[i];
i++;
counter++;
}

char_array[i - 1] = NULL;
char_array[i] = NULL;
i = 0;
counter = 0;
}

array_size = strlen(char_array);

while(i < array_size) {
int_array[i] = char_array[i];
int_array[i] -= 48;
i++;
}

i = 0;

if(hexa == true) {
int sum = 0;

while(i < array_size) {
int_array[i] = int_array[i] *
(sixteen_power(array_size - i - 1));
i++;
}
i = 0;

while(i < array_size) {
sum += int_array[i];
i++;
}

int_array[0] = sum;
i = 1;

while(i < array_size) {
int_array[i] = NULL;
i++;
}

array_size = 1;
i = 0;
}

cout << "\nThe number you entered is:\n\n";

while(i < array_size) {
cout << int_array[i];
i++;
}

cin.get();
return 0;
}

int sixteen_power(int x) {
int power = x;
int counter = 0;
int result = 1;

while(counter < x) {
result *= 16;
counter++;
}

return result;
}

Aug 12 '05 #1
2 39879
A_*********@hotmail.com wrote:
hey there,

I was able to read a char string containing only digits and convert it
to its int equivalent. however now I need to do the same thing with a
char string containing a hexadecimal number.

below is my clumsy attempt. I think I have most of the problem covered
but I am finding it somewhat difficult to convert the 'a' through 'f'
values correctly. any suggestions anyone has would be tremendously
appreciated.


I would probably use the ANSI C strtol function.

----------- test.cc --------------------------------
#include <iostream>
#include <cstdlib>

int main() {
char hex[] = "1000";
int i = static_cast<int>(strtol(hex, NULL, 16));

std::cout << "i = " << i << '\n';

return 0;
}
----------------------------------------------------

Output is 4096 by the way.

--John Ratliff
Aug 12 '05 #2

John Ratliff wrote:
I would probably use the ANSI C strtol function.

----------- test.cc --------------------------------
#include <iostream>
#include <cstdlib>

int main() {
char hex[] = "1000";
int i = static_cast<int>(strtol(hex, NULL, 16));

std::cout << "i = " << i << '\n';

return 0;
}
----------------------------------------------------

Output is 4096 by the way.

--John Ratliff

thx, John.

Aug 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: sara | last post by:
Hi everybody, can anyone tell me how to convert decimal number into hexadecimal of the folloxing format. If I give deciaml value of num=100, I need the output in the hexa decimal form as 0x64....
8
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in...
14
by: dharmdeep | last post by:
Hi friends, I need a sample code in C which will convert a Hexadecimal number into decimal number. I had written a code for that but it was too long, I need a small code, so request u all to...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
7
by: billbaitsg | last post by:
Hi, I need some help with figuring out why my program is all messed up. There are two portions two it, one that converts from roman to decimal (rom2dec) and another that converts from decimal to...
6
by: Andrea | last post by:
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){...
1
by: Rupali12345 | last post by:
Hi all, int main() { FILE *fp1; char *c, *d; c = new char; d = c;
6
by: sweeet_addiction16 | last post by:
hello Im writin a code in c... can sum1 pls help me out in writing a c code to convert decimalnumber to hexadecimal number.The hexadecimal number generated has to be an unsigned long.
14
by: Ellipsis | last post by:
Ok so I am converting to hexadecimal from decimal and I can only cout the reverse order of the hexadecimal?! How could I reverse this so its the right order? Heres my code: #include <iostream>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.