Generate hexadecimal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generate hexadecimal
# 1  
Old 01-29-2018
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

Code:
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 :

Code:
::9f
::a0
::a1
::a2
::a3
::a4
::a5
etc.

there are a billion ip between 0000:0000:0000:0000 AND ffff:ffff:ffff:ffff like:
Code:
0000:0000:0000:0000
0000:0000:0000:0095
0000:0000:0000:0096

How can I list all of them?

---------- Post updated at 04:31 PM ---------- Previous update was at 04:07 PM ----------

mm let me explain more clear
0000:0000:0000:0095 has 4 part and each part max number is 9999 so the last number will be 9999:9999:9999:9999

How can I read number from user like 100000 and then calculate and echo all number after 0000:0000:0000:0095

Something like:
Code:
0000:0000:0000:0095
0000:0000:0000:0096
....
0000:0000:0000:9999
0000:0000:0001:0000
0000:0000:0001:0001
0000:0000:0001:0002
....
etc


Last edited by rbatte1; 01-30-2018 at 05:01 AM..
# 2  
Old 01-29-2018
You could try this:

Code:
STR2=159
END2=200
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

But be aware you will run into an issue with IPs greater than "7fff:ffff:ffff:ffff" as bash uses signed 64-bit integers. The highest order bit is a sign bit and so the integer representation of these values will be a negative number:
Code:
  -1 = ffff:ffff:ffff:ffff
  -2 = ffff:ffff:ffff:fffe
...
-256 = ffff:ffff:ffff:ff00
...
-9223372036854775807 = 8000:0000:000:0001
-9223372036854775808 = 8000:0000:000:0000
 9223372036854775807 = 7fff:ffff:ffff:ffff
 9223372036854775806 = 7fff:ffff:ffff:fffe
...


Last edited by Chubler_XL; 01-29-2018 at 07:35 PM..
# 3  
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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