# BASH hexa en decimal signé

**URL:** <https://www.debian-fr.org/t/bash-hexa-en-decimal-signe/57691>\
**Category:** Programmation\
**Created:** [Février 21, 2016, 2:52 UTC](https://www.debian-fr.org/t/bash-hexa-en-decimal-signe/57691 "2016-02-21T14:52:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lrgtk](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/l/67e7ee/32.png) [@lrgtk](https://www.debian-fr.org/u/lrgtk)\
**Post date:** [Février 21, 2016, 2:52 UTC](https://www.debian-fr.org/t/bash-hexa-en-decimal-signe/57691/1 "2016-02-21T14:52:27Z")

</div>

bonsoir  
je cherche a convertir un hex en decimal signé  
$mavariable=0xFA31  
j’ai essayer hexdump, let et le resultat est : 64049 valeur non-signée au-lieu de -1487 valeur signée

une idée de commande  
merci

---

<div class="post-metadata">

**Author:** ![PengouinPdt](https://www.debian-fr.org/user_avatar/www.debian-fr.org/pengouinpdt/32/3978_2.png) [@PengouinPdt](https://www.debian-fr.org/u/PengouinPdt)\
**Post date:** [Février 21, 2016, 2:52 UTC](https://www.debian-fr.org/t/bash-hexa-en-decimal-signe/57691/2 "2016-02-21T14:52:27Z")

</div>

printf

[http://wiki.bash-hackers.org/commands/builtin/printf](http://wiki.bash-hackers.org/commands/builtin/printf)  
[Un début de piste](http://www.adras.com/printf-and-conversion-of-negative-numbers-Hex2Dec.t11111-135.html) !

---

<div class="post-metadata">

**Author:** ![lrgtk](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/l/67e7ee/32.png) [@lrgtk](https://www.debian-fr.org/u/lrgtk)\
**Post date:** [Février 21, 2016, 2:52 UTC](https://www.debian-fr.org/t/bash-hexa-en-decimal-signe/57691/3 "2016-02-21T14:52:27Z")

</div>

un oubli de ma part  
j’ai deja fait un printf %d ou %i avec et sans +  
toujours un resultat non-signé

---

<div class="post-metadata">

**Author:** ![PengouinPdt](https://www.debian-fr.org/user_avatar/www.debian-fr.org/pengouinpdt/32/3978_2.png) [@PengouinPdt](https://www.debian-fr.org/u/PengouinPdt)\
**Post date:** [Février 21, 2016, 2:52 UTC](https://www.debian-fr.org/t/bash-hexa-en-decimal-signe/57691/4 "2016-02-21T14:52:27Z")

</div>

Je te propose cette solution :

```auto
:~$ echo "obase=10;ibase=16; (FA31-1)-FFFF" | bc
# fonctionne aussi dans sa forme abrégée :
:~$ echo "ibase=16; (FA31-1)-FFFF" | bc
```

J’explique :  
Ton problème m’intéressant - moi qui suis ignare en beaucoup de choses, et n’aies pas une grande culture, surtout mathématique informatique - après quelques recherches, je suis tombé sur ce site : [http://sandbox.mc.edu/~bennet/cs110/tc/tctod.html](http://sandbox.mc.edu/~bennet/cs110/tc/tctod.html)

Ce qui a attiré mon attention est cette explication :

> [@](#):
>
> These are examples of converting an eight-bit two’s complement number to decimal. To do this, you first check if the number is negative or positive by looking at the sign bit. If it is positive, simply convert it to decimal. If it is negative, make it positive by inverting the bits and adding one. Then, convert the result to decimal. The negative of this number is the value of the original binary.

Je traduis ce que je me suis permis de mettre en gras :

> [@](#):
>
> Si (le nombre) est négatif, il faut le rendre positif en inversant le binaire et en ajoutant un. Alors, convertir le résultat en décimal.

Avec, mon esprit logique et mes réminiscences du binaire, je me suis dit, si c’est bon pour le binaire, ça doit forcément fonctionner en Hexa - comprenant que la valeur inverse de ton chiffre hexa s’obtient grâce à ‹&nbsp;- FFFF&nbsp;› …  
le reste était de comprendre comment l’écrire en Bash.

J’ai testé avec d’autres valeurs, et comparé avec ce [convertisseur PHP](http://www.patricklecoq.fr/convert/cnv_bin.php) - le résultat est juste !

Bien sûr, je peux avoir compris de travers 😛  
Pour ce qui est de l’usage avec printf, je te laisse mariner 😉

---

<div class="post-metadata">

**Author:** ![PengouinPdt](https://www.debian-fr.org/user_avatar/www.debian-fr.org/pengouinpdt/32/3978_2.png) [@PengouinPdt](https://www.debian-fr.org/u/PengouinPdt)\
**Post date:** [Février 21, 2016, 2:52 UTC](https://www.debian-fr.org/t/bash-hexa-en-decimal-signe/57691/5 "2016-02-21T14:52:29Z")

</div>

La formule la plus juste apparaît être :

```auto
 echo "obase=10;ibase=16; -((FFFF - FA31) +1)" | bc
```

Ce [cours](http://philippe.berger2.free.fr/automatique/cours/numeration/numeration.htm) sur la numérotation me paraît bien… et particulièrement la “représentation des nombres négatifs” 😉

Je ne pense pas avoir compris assurément tout le propos… mais je retiens particulièrement cette notion de complément vrai - qui me parait importante.

---

<div class="post-metadata">

**Author:** ![PengouinPdt](https://www.debian-fr.org/user_avatar/www.debian-fr.org/pengouinpdt/32/3978_2.png) [@PengouinPdt](https://www.debian-fr.org/u/PengouinPdt)\
**Post date:** [Février 21, 2016, 2:55 UTC](https://www.debian-fr.org/t/bash-hexa-en-decimal-signe/57691/6 "2016-02-21T14:55:55Z")

</div>

Pour finir, un petit script bash, utilisant la notion de complement a 2 en hexa, utilisant printf :

```auto
#!/bin/bash

##
# call as: ./hexa2negativedec <hexa> # (WITHOUT '0x')
## 

# print to upper-case to writing correctly hexa
arg=$(printf "%X" "0x$1")

for i in $(seq 0 $((${#arg} - 1))); do
    calc[$i]=$(echo "ibase=16;obase=10; F - ${arg:$i:1}" | bc)
done

# interpret as string
hexa=$(printf "%s" ${calc[@]})
# two complements hexa
cplv2=$(echo "ibase=16;obase=10; ${hexa} +1" |bc)
# print signed dec from twos complement hexa
res=$(printf "%d\n" "-0x${cplv2}")
echo $res

# possible to resume as:
cplv2b=$(printf "%d\n" "-0x$(echo "ibase=16;obase=10; $(printf "%s" ${calc[@]}) +1" |bc)")
echo $cplv2b
```

---

<div class="post-metadata">

**Author:** ![lrgtk](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/l/67e7ee/32.png) [@lrgtk](https://www.debian-fr.org/u/lrgtk)\
**Post date:** [Février 21, 2016, 2:55 UTC](https://www.debian-fr.org/t/bash-hexa-en-decimal-signe/57691/7 "2016-02-21T14:55:59Z")

</div>

merci à tous

j’ai trouver “ascii2bunary”

---

<div class="post-metadata">

**Author:** ![JayGr](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@JayGr](https://www.debian-fr.org/u/JayGr)\
**Post date:** [Février 21, 2016, 2:55 UTC](https://www.debian-fr.org/t/bash-hexa-en-decimal-signe/57691/8 "2016-02-21T14:55:59Z")

</div>

A mon avis c’est binary pas bunary. 😉.

---

<div class="post-metadata">

**Author:** ![PengouinPdt](https://www.debian-fr.org/user_avatar/www.debian-fr.org/pengouinpdt/32/3978_2.png) [@PengouinPdt](https://www.debian-fr.org/u/PengouinPdt)\
**Post date:** [Février 21, 2016, 2:56 UTC](https://www.debian-fr.org/t/bash-hexa-en-decimal-signe/57691/9 "2016-02-21T14:56:02Z")

</div>

Oui, c’est le package ‘ascii2binary’ …

Au moins, je n’aurais pas perdu de temps, pour ma propre culture. ![:violin:](https://www.debian-fr.org/uploads/default/original/1X/85d6243bd20b916686675d7e8163eace3d737942.gif "Violin")
