473,672 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(i nt 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(cha r_array, max_size);

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

while(counter < strlen(char_arr ay)) {
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_arr ay);

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(i nt x) {
int power = x;
int counter = 0;
int result = 1;

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

return result;
}

Aug 12 '05 #1
2 39911
A_*********@hot mail.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
2563
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. I can able to get teh hex value 64 which of type char. But actually I want the ouput in the format of 0x64. can anyone tell me how to handle this conversion?
8
18647
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 adv.
14
16221
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 provide me with small sample code for that.
7
19213
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 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
7
6527
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 roman (dec2rom). int rom2dec(char rom) { //Local Variables int a = 0; //counter int dec = 0;//decimal equivalent
6
14491
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++){ printf("%02x",bs); } }
1
1881
by: Rupali12345 | last post by:
Hi all, int main() { FILE *fp1; char *c, *d; c = new char; d = c;
6
16905
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
3185
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> using namespace std; void binary(int number) { int remainder; if(number <= 1)
0
8505
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7481
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5725
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4246
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2845
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1842
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.