LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-09-2012, 12:53 PM   #1
jamesd_burgess
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Rep: Reputation: Disabled
How to insert binary value into array with fixed length of 10 digits


Hi Guys,

Hopefully someone can help me here.

I have a bash script which provides a user with 10 options for which they can input a yes or no answer. Selecting Yes sets a flag to 1 and No sets a flag to 0 each of which are stored in an array, for example:

1. Select Option1 [Yes/No]:Yes
2. Select Option2 [Yes/No]:Yes
3. Select Option3 [Yes/No]:Yes
4. Select Option4 [Yes/No]:Yes
5. Select Option5 [Yes/No]:Yes
6. Select Option6 [Yes/No]:Yes
7. Select Option7 [Yes/No]:Yes
8. Select Option8 [Yes/No]:Yes
9. Select Option9 [Yes/No]:Yes
10. Select Option10 [Yes/No]:Yes

This will create an array with the following binary value (1111111111) all enabled flags.

My script then extracts the contents of the array and converts it to decimal and outputs the value (1023 in this case) to another file which all works fine.

I also need to convert the decimal back to binary (to show the permissions) which is where I'm having problems as I need to populate the array with a 10 digit binary value. For example, converting 01111111111 to decimal equals 511, but when converting 511 back to binary it outputs 111111111 (9 digit value) and is missing the preceding zero as I need it to read 0111111111.

Does anyone know how I can convert the decimal back to binary with the preceding or trailing zeros inserted into the array?

Hopefully that makes sense.

Many thanks
James

Last edited by jamesd_burgess; 01-09-2012 at 12:54 PM.
 
Old 01-09-2012, 02:06 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Maybe I'm just slow, but
Quote:
Originally Posted by jamesd_burgess
Does anyone know how I can convert the decimal back to binary with the preceding or trailing zeros inserted into the array?
a binary value is a binary value with or without the preceding zeroes. So if I'm reading between the lines correctly, what you want is a string representation of a binary value? Where the string contains a '0' or '1' for each flag cleared or set respectively?

Or do you want an array with each location containing a numeric 0 or 1? Or an array with each location containing a character '0' or '1'?

Last edited by Dark_Helmet; 01-09-2012 at 02:07 PM.
 
Old 01-09-2012, 02:20 PM   #3
jamesd_burgess
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
a binary value is a binary value with or without the preceding zeroes. So if I'm reading between the lines correctly, what you want is a string representation of a binary value? Where the string contains a '0' or '1' for each flag cleared or set respectively?

Or do you want an array with each location containing a numeric 0 or 1? Or an array with each location containing a character '0' or '1'?
Yes sorry, I probably didn't phrase it very well. For each menu item in my script, it generates a numerical value (either 1 or 0) which is stored at specific locations in an array i.e. menu option 1 = [0], menu option 2 = [1] ..... menu option 10 = [9]. I could end up with an array that contains (0000011111) and when converted to decimal equals 31.

I then need to be able to take that decimal value, convert it back to 0000011111 and insert in an array, but of course 31 converts to 11111. I need to be able to add in the preceding five zeros but not sure how you would achieve this?

Hope that makes more sense?

Thanks
James

Last edited by jamesd_burgess; 01-09-2012 at 02:21 PM.
 
Old 01-09-2012, 02:28 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Well, I'm still fuzzy on how you want the binary conversion represented. What you're using it for I understand. But rather than engage in a back-and-forth where my confusion might spread.... let me offer something. Then maybe it will help clear up what I'm getting at.

Here's a quick bash script that will do the binary conversion. It spits out the conversion as a string representation. Your script would need to then process the string to clear or set the appropriate array locations.
Code:
#!/bin/bash

# Convert the command line argument into a numeric value
let workingValue=${1}+0

# Keep track of digits in the string
digits=0

# Set the conversion string to an empty value
conversionString=""

# Loop until we have added 10 digits
while [ ${digits} -lt 10 ] ; do

  # Do a binary mask of our working value. If the mask returns 1, then the number is odd
  # and we need to add a 1 to the string
  let addOne=${workingValue}\&1
  if [ ${addOne} -eq 1 ] ; then
    conversionString="1${conversionString}"
  else
    conversionString="0${conversionString}"
  fi

  # Adjust our digit count and workingValue respectively
  let digits=${digits}+1
  let workingValue=${workingValue}/2;
done

# Output our result
echo "${conversionString}"

exit 0
EDIT:
A sample run:
Code:
user@localhost:~/temp$ chmod u+x binconvert.bash
user@localhost:~/temp$ ./binconvert.bash 10
0000001010
user@localhost:~/temp$ ./binconvert.bash 511
0111111111
user@localhost:~/temp$ ./binconvert.bash 512
1000000000
user@localhost:~/temp$ ./binconvert.bash 1013
1111110101

Last edited by Dark_Helmet; 01-09-2012 at 02:32 PM.
 
Old 01-09-2012, 02:49 PM   #5
jamesd_burgess
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
That's great, this code is exactly what I need. Many thanks for all your help and really clear steps, very much appreciated.

Thanks
James
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I convert to fixed length format. rainmk Linux - Newbie 6 06-09-2011 05:47 AM
BASH - convert single digits to double digits. rickenbacherus Programming 7 05-07-2008 07:53 AM
gawk, comma delim to fixed length? johniccp Programming 1 01-20-2005 05:21 PM
problems reading in fixed-length record file naijaguy Programming 1 08-24-2004 03:34 PM
csv to fixed-length file roballen Programming 0 03-11-2004 04:12 AM


All times are GMT -5. The time now is 02:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration