Hexadecimal to binary operation


 
Thread Tools Search this Thread
Top Forums Programming Hexadecimal to binary operation
# 1  
Old 05-13-2014
Hexadecimal to binary operation

Dear all,
I am trying to write c-program [2] to read the following file [1] containing hexadecimal values (snippet of big data file).
I want to combine two hexadecimal values together like A0A03E01 and then would like to have the binary equivalent to perform further test on it. Unfortunately, it failed to give me binary equivalent.

This project is kind of urgent for me, any help is highly appreciated.

thanking you very much,
emily,

[1]
---------------------------
Code:
file.dat:
0x1A0A0
0x13E01
0x10400
0x12A03
0x1C010

[2]
--------------------------
Code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <bitset>
#include <string>
#include <limits>
#include <stdlib.h>
using namespace std;

int main(int argc, char *argv[])
{
  string data16, data32;
  unsigned value;
  bool dodebug = true;

  //open datafile
  std::fstream datafile;
  datafile.open("file.dat",std::fstream::in);

  stringstream ss;
  while(!datafile.eof()) {
    ss.clear();
    datafile >> ws;
    datafile >> data16;
    datafile >> data32;
    if(dodebug)   cout<<"data :"<< data16<< ", "<< data32<<endl;
    
    ss << std::hex << data16 << data32;
    ss >> value;
    bitset<32> data(value);
  }
}

# 2  
Old 05-13-2014
How do you intend to combine two 20-bit numbers into one 32-bit number?

Anyway:

Code:
#include <stdio.h>

int main(void) {
        char buf[512];
        unsigned int v;

        while(fgets(buf, 512, stdin))
        {
                if(sscanf(buf, "0x%05x", &v) != 1)
                {
                        fprintf(stderr, "Unable to parse line '%s'\n", buf);
                        continue;
                }

                printf("Got hex value %x = decimal value %d\n", v, v);
        }
}

Run it like ./program < inputfile
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-13-2014
Dear Corona,
Thank you for your reply.
However, I realized that the data format is differently written [1] and should be read different [2]. So in principle, I need to read the first column of file[1] as this way [2] avoiding the rest of the columns. And then it solve the problem. So I would like to add this feature in coding to read it like [2].

Please suggest if this is possible.


Thanks again,

Code:
[1]
file.dat:
0x1A0A0 0x10000 0x1A480 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x13E01 0x10000 0x12801 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10400 0x10000 0x10410 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x12A03 0x10000 0x12203 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 

[2]
file.dat:
A0A0
3E01 
0400 
2A03


Last edited by emily; 05-13-2014 at 07:33 PM..
# 4  
Old 05-14-2014
The given code will still work even after you have made the said changes to the input file. if you only want the four hex digits, make the following change:
Code:
if(sscanf(buf, "0x1%04x", &v) != 1)

# 5  
Old 05-14-2014
Dear Chacko,
Thanks for reply. I wonder what is 'buf' here?

emily

---------- Post updated at 02:47 AM ---------- Previous update was at 02:43 AM ----------

I tried to use it like
Code:
 while(!datafile.eof()) {
    if(sscanf(datafile, "0x1%04x", &v) != 1) {

It complain about

Code:
error: ‘v' was not declared in this scope
make: *** [all] Error 1

thanks
# 6  
Old 05-14-2014
buf is the variable where you store the line read from the file. But in the code you gave, datafile is a fstream object, not a character array.

Can you provide the enitre code?
# 7  
Old 05-14-2014
Hello,
Plese find the entire code:
thanks

Code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <bitset>
#include <string>
#include <limits>
#include <stdlib.h>
using namespace std;

int main(int argc, char *argv[])
{
  string data16, data32;
  unsigned value;
  bool dodebug = true;

  //open datafile
  std::fstream datafile;
  datafile.open("file.dat",std::fstream::in);

  stringstream ss;
  while(!datafile.eof()) {
    ss.clear();
    datafile >> ws;
    datafile >> data16;
    datafile >> data32;
    if(dodebug)   cout<<"data :"<< data16<< ", "<< data32<<endl;
    
    ss << std::hex << data16 << data32;
    ss >> value;
    bitset<32> data(value);
  }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate hexadecimal

Hello I'm working on a script to list all ipv6 from given address so I've run this script which create hex part of ipv6 STR2=159 END2=200 SUM2=`expr $END2 - $STR2` for ((i=STR2;i<=END2;++i)); do x=$( printf "%x" $i ) ; echo $x echo -e "::"$x >> netpart.txt done output is : ::9f... (2 Replies)
Discussion started by: nimafire
2 Replies

2. UNIX for Advanced & Expert Users

6-digit hexadecimal ID in shell

Hi, i tried to do this script: Generate a "unique" 6-digit hexadecimal identifier for your computer. Do not use the flawed hostid command. Hint: md5sum /etc/passwd, then select the first 6 digits of output. Fom 0 to 9 and from a to f #!/bin/bash clear echo "" echo "--------------------"... (4 Replies)
Discussion started by: jose2802
4 Replies

3. Shell Programming and Scripting

Conversion from Hexadecimal to binary

How can I convert hexadecimal values to Binary from the second field to the end Input: WS-2 23 345 235 DT-3 45 4A3 000 pp-2 76 300 E4 Output: WS-2 100011 1101000101 1000110101 DT-3 1000101 10010100011 000 pp-2 1110110 1100000000 11100100 (16 Replies)
Discussion started by: aydj
16 Replies

4. Shell Programming and Scripting

Hexadecimal to Binary conversion

Hi Guys, Is it possible to convert the hexadecimal to Binary by unix command.....I could not figure out.... If I need to convert AF6D to binary...what could be the way to do? Thanks in advance!! ---------- Post updated at 02:57 AM ---------- Previous update was at 02:42 AM ---------- I... (6 Replies)
Discussion started by: Indra2011
6 Replies

5. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

6. Shell Programming and Scripting

Column operation : cosne and sine operation

I have a txt file with several columns and i want to peform an operation on two columns and output it to a new txt file . file.txt 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 ... (4 Replies)
Discussion started by: shashi792
4 Replies

7. Programming

Hexadecimal to ascii

Let's suppose i have a hexadecimal array with 16 cells.for example b3e2d5f636111780 i want to convert it to an array of ascii characters(in C) so that i can reduce total size of the file i want to put it in. But i am afraid i have not fully understand the difference between ascii and hex(i... (3 Replies)
Discussion started by: bashuser2
3 Replies

8. Shell Programming and Scripting

Test Hexadecimal number

Hi, I would like test if a number (with 2 digit, for example a9 , 0b ) is a hexadecimal number with 2 digit ? (2 Replies)
Discussion started by: francis_tom
2 Replies

9. UNIX for Dummies Questions & Answers

Hexadecimal to Decimal

Hi all, I have a small script to convert my HexaDecimal Input to Decimal as output. #!/bin/ksh hd=00208060 dec=`printf %d $hd` echo $dec Output of the above program: printf: 00208060 not completely converted 16 But my expected output is "2130016". How can i acheive this. I... (2 Replies)
Discussion started by: Arunprasad
2 Replies

10. Shell Programming and Scripting

Get Hexadecimal Value

I have a record in a file that ends with the new line character "\n". How dio I determine the hexadecimal value for that? (2 Replies)
Discussion started by: lesstjm
2 Replies
Login or Register to Ask a Question