LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help me in writing the script (https://www.linuxquestions.org/questions/linux-newbie-8/help-me-in-writing-the-script-691505/)

prakash.akumalla 12-18-2008 10:59 AM

Help me in writing the script
 
Hi,

I have written a script which converts a give hexdecimal value to binary value in perl. But now, the problem is I should read every bit of it ( if its 10101010, i should read the value in each position and if the value in that position is 1 i should print a string and should exit if its 0).

I have written a hash table for positions 0,1,2..7 . It should check if the 0th position is either 1 or 0. If its 1 then it should print the key in the hash table for the value 0. If its 0 then it should exit.

Please help me in doing so.

Thanks,
Prakash.

unSpawn 12-18-2008 12:15 PM

Posting the actual code could get you meaningful responses.

TB0ne 12-18-2008 01:01 PM

Quote:

Originally Posted by prakash.akumalla (Post 3380112)
Hi,

I have written a script which converts a give hexdecimal value to binary value in perl. But now, the problem is I should read every bit of it ( if its 10101010, i should read the value in each position and if the value in that position is 1 i should print a string and should exit if its 0).

I have written a hash table for positions 0,1,2..7 . It should check if the 0th position is either 1 or 0. If its 1 then it should print the key in the hash table for the value 0. If its 0 then it should exit.

Please help me in doing so.

Thanks,
Prakash.

If you've already done it, why do you need help? You say you've written a script which does what you want, wrote the hash table, etc.

Or are you looking for someone to write this for you? Post what you've written so far, and what error(s) you've found, and we can try to help you. I doubt, though, anyone is going to write your programs for you for free....

prakash.akumalla 12-18-2008 11:15 PM

Hi,

What i need is to split a binary number (10101011) to separate entries into an array.

now $x = 10101011
it should be @x = (1,0,1,0,1,0,1,1)

How can we convert this?

Thanks,
Prakash.

Disillusionist 12-19-2008 02:03 AM

As you are using Perl.

Have a look at split

prakash.akumalla 12-19-2008 02:41 AM

Hi,

Thanks for your reply. I have tried it but could not get the required result.

I have tried by giving,

$x =~ s/undef/,/g;

But it did not work.

let me say my complete requirement once.

Input to the file will be hexadecimal of 2 digits (A1 or B1).

It should be converted to binary and next for every 1 in the string we should generate a message which is predefined for that position.

If the value in that position is 1 a flag should be set and a string should be displayed.

Please assist me in doing so.

Thanks,
Prakash.

Disillusionist 12-19-2008 01:25 PM

Code:

$x='0101011001';
@y=split(//, $x);


lumak 12-19-2008 01:49 PM

There shouldn't be any reason to split the binary number in an array. All you have to do is OR the value to check it. In fact! chances are there is already a sutible library or functions for handling this. The concept is called a bit vector... You store an integer which can then have 'OR' or 'AND' operators placed on them against another integer to see if the value is 1 or 0.

10110 = 22
check second position
22 & 2 = 2

Code:

10110
&00010
------
 00010

20 & 2 = 0
 10100
&00010
------
 00000

a bit vector library and documentation would be more helpful.


All times are GMT -5. The time now is 01:03 PM.