Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Bash script problems int to binary Post 303041352 by Zedki on Thursday 21st of November 2019 08:06:00 PM
Old 11-21-2019
Bash script problems int to binary

Hi, I am trying to do a bash script that convert a decimal number to a binary value, but it doesn't work...
To begin, I am just trying to convert a positive number to 8 bits binary.
Code:
read -p"Entrez un nombre entre -128 et 127 pour l'encoder en binaire: " number
binaryValues=(128 64 32 16 8 4 2 1)
binaryNumber=()
positiveNum=$number
if [ $number -lt 0 ] #transformer la copie du nombre en positif.
then
(( positiveNum=$number-$number*2 )) 
fi
for i in "${binaryValues[@]}"
do
if (( $positiveNum -ge ${binaryValues[i]} ))
then
positiveNum=$"(( $positiveNum-${binaryValues[i]} ))"
binaryNumber[i]='1';
else
binaryNumber[i]='0';
fi
done
echo ${binaryNumber[@]}

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

2. Shell Programming and Scripting

unzip via bash startup script problems

i have two lines in my rc.local file that are wget -O/<path>/<file>.zip url://domain.com unzip -o /<path>/<file>.zip the wget works fine, but the unzip won't work. when i copy/pase the unzip line to the prompt it works fine. i thought that maybe the unzip was running before the wget... (0 Replies)
Discussion started by: easysnowboards
0 Replies

3. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

4. Shell Programming and Scripting

Problems editing file with awk in bash script

Hello dear users, here I have a script to manipulate .csv files that are like this originally: And I need to make a script to delete certain fields. Each field is separated with a comma. So, here is my script (at least a part of it): Field $1 is composed of a name, and then a... (5 Replies)
Discussion started by: sr00t
5 Replies

5. Shell Programming and Scripting

Execution Problems with bash script

Hello, can someone please help me to fix this script, I have a 2 files, one file has hostname information and second file has console information of the hosts in each line, I have written a script which actually reads each line in hostname file and should grep in the console file and paste the... (8 Replies)
Discussion started by: bobby320
8 Replies

6. Shell Programming and Scripting

Execution problems with BASH Shell Script

Hi I need help with my coding , first time I'm working with bash . What i must do is check if there is 3 .txt files if there is not 3 of them i must give an error code , if al three is there i must first arrange them in alphabetical order and then take the last word in al 3 of the .txt files... (1 Reply)
Discussion started by: linux newb
1 Replies

7. Shell Programming and Scripting

Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008)

Hi, I have the following requirement. There will be following text/line in a file (eg: search-build.txt) PRODUCT_VERSION="V:01.002.007.Build1234" I need to update the incremental build number (eg here 007) every time I give a build through script. I am able to search the string and get... (4 Replies)
Discussion started by: drwatson_droid
4 Replies

8. Shell Programming and Scripting

bash script error with binary operator expected.

Hello, I am not sure, where I am missing in the scirpt, I am trying to grep few users from /etc/passwd file and if exists, I added line to echo as user exist, if not create it. #!/bin/bash for vid in v707 z307 z496 z163 z292 ; do if then echo " $vid User exists " else ... (2 Replies)
Discussion started by: bobby320
2 Replies

9. Shell Programming and Scripting

Bash script having variable substitution problems

Hi I am setting the variables like this : setenv MODULE1 modem5__3 setenv MODULE2 modem5__2 setenv MODULE3 modem_ctrl_1_1 setenv MODULE4 modem_1_0 setenv COUNT 10 I am having a bash script as shown below ################################################ #!/bin/bash for ((... (5 Replies)
Discussion started by: kshitij
5 Replies

10. Shell Programming and Scripting

sed problems - Bash Script

Hi I keep getting the following error sed: -e expression #1, char 32: unterminated `s' command sed: -e expression #1, char 35: unterminated `s' command sed: -e expression #1, char 35: unterminated `s' command whenever I use the following bash script #! /bin/bash... (2 Replies)
Discussion started by: spbr
2 Replies
INET_NET_PTON(3)					     Linux Programmer's Manual						  INET_NET_PTON(3)

NAME
inet_net_pton, inet_net_ntop - Internet network number conversion SYNOPSIS
#include <arpa/inet.h> int inet_net_pton(int af, const char *pres, void *netp, size_t nsize); char *inet_net_ntop(int af, const void *netp, int bits, char *pres, size_t psize); Link with -lresolv. Feature Test Macro Requirements for glibc (see feature_test_macros(7)): inet_net_pton(), inet_net_ntop(): Since glibc 2.20: _DEFAULT_SOURCE Before glibc 2.20: _BSD_SOURCE || _SVID_SOURCE DESCRIPTION
These functions convert network numbers between presentation (i.e., printable) format and network (i.e., binary) format. For both functions, af specifies the address family for the conversion; the only supported value is AF_INET. inet_net_pton() The inet_net_pton() function converts pres, a null-terminated string containing an Internet network number in presentation format to net- work format. The result of the conversion, which is in network byte order, is placed in the buffer pointed to by net. (The netp argument typically points to an in_addr structure.) The nsize argument specifies the number of bytes available in netp. On success, inet_net_pton() returns the number of bits in the network number field of the result placed in netp. For a discussion of the input presentation format and the return value, see NOTES. Note: the buffer pointed to by netp should be zeroed out before calling inet_net_pton(), since the call writes only as many bytes as are required for the network number (or as are explicitly specified by pres), which may be less than the number of bytes in a complete network address. inet_net_ntop() The inet_net_ntop() function converts the network number in the buffer pointed to by netp to presentation format; *netp is interpreted as a value in network byte order. The bits argument specifies the number of bits in the network number in *netp. The null-terminated presentation-format string is placed in the buffer pointed to by pres. The psize argument specifies the number of bytes available in pres. The presentation string is in CIDR format: a dotted-decimal number representing the network address, followed by a slash, and the size of the network number in bits. RETURN VALUE
On success, inet_net_pton() returns the number of bits in the network number. On error, it returns -1, and errno is set to indicate the cause of the error. On success, inet_net_ntop() returns pres. On error, it returns NULL, and errno is set to indicate the cause of the error. ERRORS
EAFNOSUPPORT af specified a value other than AF_INET. EMSGSIZE The size of the output buffer was insufficient. ENOENT (inet_net_pton()) pres was not in correct presentation format. CONFORMING TO
The inet_net_pton() and inet_net_ntop() functions are nonstandard, but widely available. NOTES
Input presentation format for inet_net_pton() The network number may be specified either as a hexadecimal value or in dotted-decimal notation. Hexadecimal values are indicated by an initial "0x" or "0X". The hexadecimal digits populate the nibbles (half octets) of the network num- ber from left to right in network byte order. In dotted-decimal notation, up to four octets are specified, as decimal numbers separated by dots. Thus, any of the following forms are accepted: a.b.c.d a.b.c a.b a Each part is a number in the range 0 to 255 that populates one byte of the resulting network number, going from left to right, in network- byte (big endian) order. Where a part is omitted, the resulting byte in the network number is zero. For either hexadecimal or dotted-decimal format, the network number can optionally be followed by a slash and a number in the range 0 to 32, which specifies the size of the network number in bits. Return value of inet_net_pton() The return value of inet_net_pton() is the number of bits in the network number field. If the input presentation string terminates with a slash and an explicit size value, then that size becomes the return value of inet_net_pton(). Otherwise, the return value, bits, is inferred as follows: * If the most significant byte of the network number is greater than or equal to 240, then bits is 32. * Otherwise, if the most significant byte of the network number is greater than or equal to 224, then bits is 4. * Otherwise, if the most significant byte of the network number is greater than or equal to 192, then bits is 24. * Otherwise, if the most significant byte of the network number is greater than or equal to 128, then bits is 16. * Otherwise, bits is 8. If the resulting bits value from the above steps is greater than or equal to 8, but the number of octets specified in the network number exceed bits/8, then bits is set to 8 times the number of octets actually specified. EXAMPLE
The program below demonstrates the use of inet_net_pton() and inet_net_ntop(). It uses inet_net_pton() to convert the presentation format network address provided in its first command-line argument to binary form, displays the return value from inet_net_pton(). It then uses inet_net_ntop() to convert the binary form back to presentation format, and displays the resulting string. In order to demonstrate that inet_net_pton() may not write to all bytes of its netp argument, the program allows an optional second com- mand-line argument, a number used to initialize the buffer before inet_net_pton() is called. As its final line of output, the program dis- plays all of the bytes of the buffer returned by inet_net_pton() allowing the user to see which bytes have not been touched by inet_net_pton(). An example run, showing that inet_net_pton() infers the number of bits in the network number: $ ./a.out 193.168 inet_net_pton() returned: 24 inet_net_ntop() yielded: 193.168.0/24 Raw address: c1a80000 Demonstrate that inet_net_pton() does not zero out unused bytes in its result buffer: $ ./a.out 193.168 0xffffffff inet_net_pton() returned: 24 inet_net_ntop() yielded: 193.168.0/24 Raw address: c1a800ff Demonstrate that inet_net_pton() will widen the inferred size of the network number, if the supplied number of bytes in the presentation string exceeds the inferred value: $ ./a.out 193.168.1.128 inet_net_pton() returned: 32 inet_net_ntop() yielded: 193.168.1.128/32 Raw address: c1a80180 Explicitly specifying the size of the network number overrides any inference about its size (but any extra bytes that are explicitly speci- fied will still be used by inet_net_pton(): to populate the result buffer): $ ./a.out 193.168.1.128/24 inet_net_pton() returned: 24 inet_net_ntop() yielded: 193.168.1/24 Raw address: c1a80180 Program source /* Link with "-lresolv" */ #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0) int main(int argc, char *argv[]) { char buf[100]; struct in_addr addr; int bits; if (argc < 2) { fprintf(stderr, "Usage: %s presentation-form [addr-init-value] ", argv[0]); exit(EXIT_FAILURE); } /* If argv[2] is supplied (a numeric value), use it to initialize the output buffer given to inet_net_pton(), so that we can see that inet_net_pton() initializes only those bytes needed for the network number. If argv[2] is not supplied, then initialize the buffer to zero (as is recommended practice). */ addr.s_addr = (argc > 2) ? strtod(argv[2], NULL) : 0; /* Convert presentation network number in argv[1] to binary */ bits = inet_net_pton(AF_INET, argv[1], &addr, sizeof(addr)); if (bits == -1) errExit("inet_net_ntop"); printf("inet_net_pton() returned: %d ", bits); /* Convert binary format back to presentation, using 'bits' returned by inet_net_pton() */ if (inet_net_ntop(AF_INET, &addr, bits, buf, sizeof(buf)) == NULL) errExit("inet_net_ntop"); printf("inet_net_ntop() yielded: %s ", buf); /* Display 'addr' in raw form (in network byte order), so we can see bytes not displayed by inet_net_ntop(); some of those bytes may not have been touched by inet_net_ntop(), and so will still have any initial value that was specified in argv[2]. */ printf("Raw address: %x ", htonl(addr.s_addr)); exit(EXIT_SUCCESS); } SEE ALSO
inet(3), networks(5) Linux 2014-05-28 INET_NET_PTON(3)
All times are GMT -4. The time now is 10:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy