Sponsored Content
Full Discussion: Generate hexadecimal
Top Forums Shell Programming and Scripting Generate hexadecimal Post 303012110 by RudiC on Tuesday 30th of January 2018 05:29:59 AM
Old 01-30-2018
Your question is a bit difficult to understand and answer as you're talking IPv6 and hexadecimal, but you're showing numbers and samples that don't comply.
- IPv6 uses 128 bit addresses, split into 8 subparts of 16bit each, represented in what is known as "colon-hexadecimal".
- depending on where you come from, a billion is 1E9 or 1E12, respectively. A hex number consisting of 16 sequential "F"s (equ. 4 subparts) is roughly 1,8E19, so way beyond that.
- in hexadecimal, "FFFF" would be the last representable number in four digits, not "each part max number is 9999". So the difference between 0x10000 and 0x95 would be 0xFF6B (65387), leading to quite unwieldy an output file.

Considering these implications, Chubler_XL's proposal handles the situation of non-IPv6 perfectly well:
Code:
STR2=65530
END2=65540
for ((i=STR2; i<=END2; i++)); do     printf -v HEX "%016X" $i;     printf "%s:%s:%s:%s\n" ${HEX:0:4} ${HEX:4:4} ${HEX:8:4} ${HEX:12:4}; done
0000:0000:0000:FFFA
0000:0000:0000:FFFB
0000:0000:0000:FFFC
0000:0000:0000:FFFD
0000:0000:0000:FFFE
0000:0000:0000:FFFF
0000:0000:0001:0000
0000:0000:0001:0001
0000:0000:0001:0002
0000:0000:0001:0003
0000:0000:0001:0004

 

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

looping in hexadecimal with bash

hello every one this is my first post ... well i'm new to linux .... i've been enjoying shell scripting tutorials and i'm new to writting scripts i want to write a script that creates a directory tree named in hexadecimal but i'm stuck at the hexadecimal part ... here is my code (incase i... (2 Replies)
Discussion started by: soba
2 Replies

3. 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

4. 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

5. Shell Programming and Scripting

hexadecimal replacing with awk ?

Hi there ! I have text files with some nonsense characters in it, so different text editors put different nonsense symbols, and, worse, the application that should be able to read these files doesn't. With xxd, the nonsense characters show as "efbfbd", while they should be "c2a7" (the... (2 Replies)
Discussion started by: jossojjos
2 Replies

6. Shell Programming and Scripting

Convert hexadecimal value in decimal value

hi all, this is my script: #! /bin/sh minutes=$( { i2cget -f -y 0 0x51 3; } 2>&1 ) minutes=${minutes:2} hour=$( { i2cget -f -y 0 0x51 4; } 2>&1 ) hour=${hour:2} day=$( { i2cget -f -y 0 0x51 5; } 2>&1 ) day=${day:2} month=$( { i2cget -f -y 0 0x51 7; } 2>&1 ) month=${month:2} ... (6 Replies)
Discussion started by: enaud
6 Replies

7. 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

8. 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

9. Programming

Hexadecimal to binary operation

Dear all, I am trying to write c-program to read the following file 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... (16 Replies)
Discussion started by: emily
16 Replies

10. UNIX for Dummies Questions & Answers

Convert hexadecimal value to decimal value

Hi All, cat filename | awk '{print $1, $2, $4, $5, $6, $7, $8, $9, $10;}' | awk 'NF > 0' OUTPUT: 2015-01-19 00:12:32 00000000fbfa0000 000000009ae5cf80 014d 015d 0017 003c 0362de20 2015-01-19 00:13:52 00000000fc820000 00000000994c6758 014c 015d 000b 003c 08670250 2015-01-19 00:14:25... (12 Replies)
Discussion started by: sam@sam
12 Replies
INET_PTON(3)                                                 Linux Programmer's Manual                                                INET_PTON(3)

NAME
inet_pton - convert IPv4 and IPv6 addresses from text to binary form SYNOPSIS
#include <arpa/inet.h> int inet_pton(int af, const char *src, void *dst); DESCRIPTION
This function converts the character string src into a network address structure in the af address family, then copies the network address structure to dst. The af argument must be either AF_INET or AF_INET6. dst is written in network byte order. The following address families are currently supported: AF_INET src points to a character string containing an IPv4 network address in dotted-decimal format, "ddd.ddd.ddd.ddd", where ddd is a dec- imal number of up to three digits in the range 0 to 255. The address is converted to a struct in_addr and copied to dst, which must be sizeof(struct in_addr) (4) bytes (32 bits) long. AF_INET6 src points to a character string containing an IPv6 network address. The address is converted to a struct in6_addr and copied to dst, which must be sizeof(struct in6_addr) (16) bytes (128 bits) long. The allowed formats for IPv6 addresses follow these rules: 1. The preferred format is x:x:x:x:x:x:x:x. This form consists of eight hexadecimal numbers, each of which expresses a 16-bit value (i.e., each x can be up to 4 hex digits). 2. A series of contiguous zero values in the preferred format can be abbreviated to ::. Only one instance of :: can occur in an address. For example, the loopback address 0:0:0:0:0:0:0:1 can be abbreviated as ::1. The wildcard address, consisting of all zeros, can be written as ::. 3. An alternate format is useful for expressing IPv4-mapped IPv6 addresses. This form is written as x:x:x:x:x:x:d.d.d.d, where the six leading xs are hexadecimal values that define the six most-significant 16-bit pieces of the address (i.e., 96 bits), and the ds express a value in dotted-decimal notation that defines the least significant 32 bits of the address. An example of such an address is ::FFFF:204.152.189.116. See RFC 2373 for further details on the representation of IPv6 addresses. RETURN VALUE
inet_pton() returns 1 on success (network address was successfully converted). 0 is returned if src does not contain a character string representing a valid network address in the specified address family. If af does not contain a valid address family, -1 is returned and errno is set to EAFNOSUPPORT. ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7). +------------+---------------+----------------+ |Interface | Attribute | Value | +------------+---------------+----------------+ |inet_pton() | Thread safety | MT-Safe locale | +------------+---------------+----------------+ CONFORMING TO
POSIX.1-2001, POSIX.1-2008. NOTES
Unlike inet_aton(3) and inet_addr(3), inet_pton() supports IPv6 addresses. On the other hand, inet_pton() accepts only IPv4 addresses in dotted-decimal notation, whereas inet_aton(3) and inet_addr(3) allow the more general numbers-and-dots notation (hexadecimal and octal num- ber formats, and formats that don't require all four bytes to be explicitly written). For an interface that handles both IPv6 addresses, and IPv4 addresses in numbers-and-dots notation, see getaddrinfo(3). BUGS
AF_INET6 does not recognize IPv4 addresses. An explicit IPv4-mapped IPv6 address must be supplied in src instead. EXAMPLE
The program below demonstrates the use of inet_pton() and inet_ntop(3). Here are some example runs: $ ./a.out i6 0:0:0:0:0:0:0:0 :: $ ./a.out i6 1:0:0:0:0:0:0:8 1::8 $ ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116 ::ffff:204.152.189.116 Program source #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { unsigned char buf[sizeof(struct in6_addr)]; int domain, s; char str[INET6_ADDRSTRLEN]; if (argc != 3) { fprintf(stderr, "Usage: %s {i4|i6|<num>} string ", argv[0]); exit(EXIT_FAILURE); } domain = (strcmp(argv[1], "i4") == 0) ? AF_INET : (strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]); s = inet_pton(domain, argv[2], buf); if (s <= 0) { if (s == 0) fprintf(stderr, "Not in presentation format"); else perror("inet_pton"); exit(EXIT_FAILURE); } if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) { perror("inet_ntop"); exit(EXIT_FAILURE); } printf("%s ", str); exit(EXIT_SUCCESS); } SEE ALSO
getaddrinfo(3), inet(3), inet_ntop(3) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. Linux 2017-09-15 INET_PTON(3)
All times are GMT -4. The time now is 06:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy