473,326 Members | 2,061 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,326 software developers and data experts.

Converting hexadecimal to binary

I am going to make a program that will convert from hexadecimal string, then to binary and finally to decimal. Now here's the problem (i guess) : when I try to convert hexadecimal string to binary string, I just did a "cout" and this is not a binary string. So, what am I exactly going to do to make a binary string from my hexadecimal string? I don't know what to do, I tried my best for days but still, there's no improvement.. T_T

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string.h>
  3. #include <conio.h>
  4. using namespace std;
  5.  
  6.  
  7. //this function is for my hexadecimal input, to filter the keys from the keyboard
  8. char* hex (char str1[])
  9. {
  10.  
  11.      int i=0,x=0;  
  12.      do{         
  13.              str1[x]=getch();
  14.              str1[x]=toupper (str1[x]); 
  15.                 if (((str1[x]>='0') && (str1[x]<='9')) || ((str1[x]>='A') && (str1[x]<='F'))){
  16.                     cout<<str1[x];
  17.                     x++;
  18.               }
  19.  
  20.  
  21.     }while (str1[x]!=13);
  22.     str1[x]='\0';
  23. return str1;
  24. }
  25.  
  26. //and this function is for my binary to decimal function.
  27. int bd (char bin[]){
  28.  
  29.      int c,x=0,i,factor=1;
  30.      int decimal=0;
  31.      int len;
  32.  
  33.      len = strlen (bin)-1;
  34.  
  35.      for (i=0;i<=len;len--){
  36.             if (bin[len]==49){
  37.                c=bin[len]-48;}
  38.             else if (bin[len]==48){
  39.                c=bin[len]-48;}
  40.          decimal = decimal + c*factor;
  41.          factor = factor*2;
  42.      }     
  43.     return decimal; 
  44. }
  45.  
  46. main (void)
  47. {
  48.      int i;
  49.      char s[40];
  50.      hex (s);
  51.  
  52.      //this is for my hexadecimal to binary (the part where I need help T_T)
  53.       for(i=0;s[i]!='\0';i++)
  54.          {
  55.  
  56.           switch(s[i])
  57.            {
  58.             case '0':
  59.              cout<<"0000";
  60.              break;
  61.             case '1':
  62.              cout<<"0001";
  63.              break;
  64.             case '2':
  65.              cout<<"0010";
  66.              break;
  67.             case '3':
  68.              cout<<"0011";
  69.              break;
  70.             case '4':
  71.              cout<<"0100";
  72.              break;
  73.             case '5':
  74.              cout<<"0101";
  75.              break;
  76.             case '6':
  77.              cout<<"0110";
  78.              break;
  79.             case '7':
  80.              cout<<"0111";
  81.              break;
  82.             case '8':
  83.              cout<<"1000";
  84.              break;
  85.             case '9':
  86.              cout<<"1001";
  87.              break;
  88.             case 'A':
  89.              cout<<"1010";
  90.              break;
  91.             case 'B':
  92.              cout<<"1011";
  93.              break;
  94.             case 'C':
  95.              cout<<"1100";
  96.              break;
  97.             case 'D':
  98.              cout<<"1101";
  99.              break;
  100.             case 'E':
  101.              cout<<"1110";
  102.              break;
  103.             case 'F':
  104.              cout<<"1111";
  105.              break;
  106.            }
  107.           }
  108. system ("PAUSE");
  109. return 0;
  110. }
  111.  
  112.  
  113.  
:((
Mar 14 '10 #1
3 9397
weaknessforcats
9,208 Expert Mod 8TB
Presumably by a binary string you mean a char string with only 1 and 0 characters.

That being the case, just use a string object.

Your cout << "0101" would become:

Expand|Select|Wrap|Line Numbers
  1. string str;
  2. //etc...
  3.  
  4. str += "0101";
Mar 14 '10 #2
whodgson
542 512MB
Converting hexadecimal to binary is really quite simple:
Just convert each hexadecimal character to its four digit binary equivalent.
so 0xff = 1111 1111
or 0x27b=0010 0111 1011 //I have left spaces so that it easier to read
so write the code that will accomplish this and half the problem is solved
Mar 15 '10 #3
donbock
2,426 Expert 2GB
Hexadecimal digits can be upper or lower case. That is, you should for example check for both 'A' and 'a'. Alternatively, you could switch on toupper(s[i]).
Mar 15 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gurra | last post by:
I am using XMLhttp and responsebody to receive info from a webpage I dont use responsetext since i need the international chars åäö The information I recieve is binary is there any fast way to...
0
by: Chandra Mohan | last post by:
Hi All, In continuation to previous posting, I have one more query : A column with String containing hex values say, CA02B. I have to access the value in a int variable, any hints on how we...
8
by: Ramiro Barbosa, Jr. | last post by:
All, Any ideas on how to convert the first 8 bytes of raw uninterpreted sequence of bytes from 'char array;' (populated with _binary_ data read from a socket), into a 'long id'? Thank you! ...
5
by: nickisme | last post by:
Hi - sorry for the possibly stupid question, but I'm still a wee starter on c++... Just wondering if there's a quick way to convert data into binary strings... To explain, I'm trying to convert...
0
by: ChrisWoodruff | last post by:
I have a C++ function in a COM object that I am trying to implement in VB.NET (the functionality, NOT the COM object, I want to remove the requirement for the COM DLL) I am an experienced VB...
5
by: genojoe | last post by:
I thought this would be easy but it is not. I have tried: Dim sLetter As String = Chr(&H4C) 'This works Dim s As String = "&H4C" sLetter = Chr(s) 'This causes an error. My best guess...
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...
3
by: dru103079 | last post by:
How do I process a binary number to Real 32.
0
by: Terry Reedy | last post by:
A. Joseph wrote: These are number representation systems that can be applied to or used with integral, rational (numberator,denominator), and 'point' numbers. Try Wikipedia or any search...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.