Hexadecimal to binary operation


 
Thread Tools Search this Thread
Top Forums Programming Hexadecimal to binary operation
# 8  
Old 05-14-2014
file.dat
Code:
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

Modified version of your code:
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()) {
  stringstream ssData16, ssData32;
    ss.clear();
    ssData16.clear();
    ssData32.clear();
    datafile >> ws;
    getline(datafile,data16,'\n');
    getline(datafile,data32,'\n');
    ssData16 << data16;
    ssData32 << data32;
    ssData16 >> data16;
    ssData32 >> data32;
    data16.erase(0,3);
    data32.erase(0,3);
    if(dodebug) cout<<"data :"<< data16<< ", "<<data32<<endl;


    ss << std::hex << data16<< data32;
    ss >> value;
    bitset<32> data(value);
    cout<<"data: "<<data<<endl;
  }
}

Modified version of Corona's code:
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);

  while(!datafile.eof()) {
    datafile >> ws;
    getline(datafile,data16,'\n');
    //if(dodebug) cout<<"data :"<< data16<< ", "<<data32<<endl;

    sscanf(data16.c_str(),"0x1%04x",&value);

    cout<<"data: "<<value<<endl;
  }
}

This User Gave Thanks to chacko193 For This Post:
# 9  
Old 05-14-2014
You asked for C code, you were writing C++ code. Notice how the C code is all far simpler and more elegant anyway.

A slightly changed version which doesn't use stdin:

Code:
#include <stdio.h>

int main(void) {
        char buf[512];
        unsigned int v;
        FILE *fp=fopen("file.dat", "r");
        if(fp == NULL) return(1);

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

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

This User Gave Thanks to Corona688 For This Post:
# 10  
Old 05-14-2014
Dear corona and Chacko,
Thanks for the help.
Corona, I could see the simplicity of the C-programming, but I am more used to of c++ programming.

I have one more question, if in the same data.txt file, I would like to read the second column data format like e2e2e2fd
How that can be achieved in the same code [1]

thanks
emily

[1]
Code:
 while(!datafile.eof()) {
    stringstream ssData16, ssData32;
    ssData16.clear();
    ssData32.clear();
    datafile >> ws;
    getline(datafile,data16,'\n');
    getline(datafile,data32,'\n');
    ssData16 << data16;
    ssData16 >> data16;
    ssData32 << data32;
    ssData32 >> data32;
    data16.erase(0,3);
    data32.erase(0,3);
    cout<<"data : " << data16 <<", "<< data32<< endl;

    ss << std::hex << data16 << data32;
    ss >> value;

    bitset<32> data(value);
}


Code:
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x11E00 0x3E2FD 0x3F06E 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x13E00 0x3F9E2 0x3F8F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x12200 0x3E982 0x3F409 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x12200 0x3F9AD 0x3F846 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x12600 0x3F93D 0x1F871 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x12600 0x3E932 0x3F460 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x12A00 0x3E942 0x1F416 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF

# 11  
Old 05-14-2014
Very minor change.

Code:
#include <stdio.h>

int main(void) {
        char buf[512];
        unsigned int v, q;
        FILE *fp=fopen("file.dat", "r");
        if(fp == NULL) return(1);

        while(fgets(buf, 512, fp))
        {
                if(sscanf(buf, "0x1%04x 0x3%04x", &v, &q) != 2)
                {
                        fprintf(stderr, "Unable to parse line '%s'\n", buf);
                        continue;
                }

                q <<= 8;  q |= 0xfd;

                printf("Got values %x(%d), %x(%d)\n", v, v, q, q);
        }
}


Last edited by Corona688; 05-14-2014 at 04:23 PM..
# 12  
Old 05-14-2014
Thank you Corona,
But unfortunately, I would prefer to work with c++ code, as I feel more comfortable with it.
Can you please suggest the modifications needed in my script which is given in last thread.

Thank you again,
emily

Quote:
Originally Posted by Corona688
Very minor change.

Code:
#include <stdio.h>

int main(void) {
        char buf[512];
        unsigned int v, q;
        FILE *fp=fopen("file.dat", "r");
        if(fp == NULL) return(1);

        while(fgets(buf, 512, fp))
        {
                if(sscanf(buf, "0x1%04x 0x3%04x", &v, &q) != 2)
                {
                        fprintf(stderr, "Unable to parse line '%s'\n", buf);
                        continue;
                }

                q <<= 8;  q |= 0xfd;

                printf("Got values %x(%d), %x(%d)\n", v, v, q, q);
        }
}

# 13  
Old 05-15-2014
Quote:
Originally Posted by emily
I have one more question, if in the same data.txt file, I would like to read the second column data format like e2e2e2fd
Quote:
Originally Posted by emily
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.
In your OP, you said that you need to combine two lines, which contains 20 bit hex, and form a 32 bit value.

Now you are saying that you need to combine two column.
In e2e2e2fd, what is the third e2 pair? Is it the MSB of hex you read from file or the LSB?

Please make your actual requirement clear.
# 14  
Old 05-15-2014
Hello,
No I do not need to combine two different columns.
Task-01 is already achieved using [1] to read the Blue data at a time 00001E00.
But I also need to read the data from second column (only second column) like the red entries at time E2E2E2FD (omitting the 0x3)

thanks
emily

[1]
Code:
 while(!datafile.eof()) {
    stringstream ssData16, ssData32;
    ssData16.clear();
    ssData32.clear();
    datafile >> ws;
    getline(datafile,data16,'\n');
    getline(datafile,data32,'\n');
    ssData16 << data16;
    ssData16 >> data16;
    ssData32 << data32;
    ssData32 >> data32;
    data16.erase(0,3);
    data32.erase(0,3);
    cout<<"data : " << data16 <<", "<< data32<< endl;

    ss << std::hex << data16 << data32;
    ss >> value;

    bitset<32> data(value);
}


Code:
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x11E00 0x3E2FD 0x3F06E 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x13E00 0x3F9E2 0x3F8F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x12200 0x3E982 0x3F409 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x12200 0x3F9AD 0x3F846 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x12600 0x3F93D 0x1F871 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x12600 0x3E932 0x3F460 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x12A00 0x3E942 0x1F416 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 
0x10000 0x3E2E2 0x1F0F0 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF 0x3FFFF

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