#🔒 need help to convert number in base 16 to nummber in decimal pls
145 messages · Page 1 of 1 (latest)
@amber umbra
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
so
def conversion_base16(x):
assert type(x) is int and x>=0, "x doit être un entier"
resultat = ""
symbole ="0123456789ABCDEF"
while x !=0:
r=x%16
x = x//16
resultat = symbole[r]+ resultat
return resultat
Hey @amber umbra!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
i already have this
print('Hello, world!')
def conversion_base16(x):
assert type(x) is int and x>=0, "x doit être un entier"
resultat = ""
symbole ="0123456789ABCDEF"
while x !=0:
r=x%16
x = x//16
resultat = symbole[r]+ resultat
return resultat
i can convert decimal in base 16 but not thie inverse
You can convert a base 10 int into a string and apply the same method
You can also use the int class that takes a base attribute
yes but my teach doesnt want
i have to use "foré
for"
Alright
Then this
how?
You can convert anything to a string useing str
yeah i know
So basically you can revert all operations you do now
for the moment i have this
def add_hexa(b1,b2):
symbole="0123456789ABCDEF"
if len(b1)>len(b2):
n = len(b1)
else:
n = len(b2)
for i in range(n-1,-1,-1):
Use 16 instead of 10, 10 instead of 16
And the symbols for base 10
Why are you summing them ?
Weren't we talking about converting from base 16 to base 10 ?
symbole="0123456789?
Yes
def conversion_base10(x):
assert type(x) is int and x>=0, "x doit être un entier"
resultat = ""
symbole ="0123456789ABCDEF"
while x !=0:
r=x%10
x = x//10
resultat = symbole[r]+ resultat
return resultat
Hey @amber umbra!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
this?
Well you'll have an error verifying the type of x
But try it
That's the best way to tell
If your input x is in base 16 likely it's a string
so i put "x"?
ok
But you can't use the division rhing to compute the digits
wait
Bc you are using a str
You need to figure out another way
Based on the value of each letter and it's position, for example
Ok
MODS: we will talk french from now as it will be easier. Don't worry about moderation, I'll report if there a re problems
j'ai penseé a utiliser un for i in range
Non t'es bien parti
Ah oui
En gros
Tu itère sur le string qui represente ton nombre
Et tu fais attention a la position du chiffre que tu traite et a sa valeur
!e
x = "AEB3"
for i in range(len(x)):
print(x)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | AEB3
002 | AEB3
003 | AEB3
004 | AEB3
ok
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | A
002 | E
003 | B
004 | 3
Tu as l'idée
mhhh
Tu peut obtenir les symbols comme ca
ouais
Donc avec la position (que tu obtiens avec i)
Tu peux obtenir la valeur de chaque symbol
C'est comme faire
37 = 3*10 + 7
Sauf que c'est en base 16
B7 = B*16 + 7
chaque symbol représente une valeur multiplié par 16 puissance qlqchose
Comme en base 10 en fait
236 c'est 2*10^2 + 3*10^1 + 6
comme je lui dit que F=15
comment je peux faire?
avec symbole[...]?
j'essaie mais cpas simple
j'ai pas encore fait ça en classe
Tu peux faire un gros bloc de if/elif
le prof veut pas
que du "for"
def add_hexa(b1,b2):
symbole="0123456789ABCDEF"
if len(b1)>len(b2):
n = len(b1)
else:
n = len(b2)
for i in range(n-1,-1,-1):
symbole[i] = i
16**i*n[-i]
Hey @amber umbra!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
@quaint anchor
t'en pense quoi
print('Hello, world!')
print('Hello, world!')
def add_hexa(b1,b2):
symbole="0123456789ABCDEF"
if len(b1)>len(b2):
n = len(b1)
else:
n = len(b2)
for i in range(n-1,-1,-1):
symbole[i] = i
16**i*n[-i]
Je comprends pas pq tu me parle de somme d'hexa mtn ?
Mas plus simple de convertir en base 10, De sommer et de reconvertir en base 16 ?
je dois mettre un nombre en base 16 dans le programme
ça marche pas
raceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<module1>", line 9, in add_hexa
TypeError: 'str' object does not support item assignment
@quaint anchor
while p>len(symbole):
p +=1
len(symbole)=p
j'ai fait un truc comme ça pour approprier chaque nombre et lettre a son vrai truc
c bien?
jsp quoi return
ah nan c nul
def add_hexa(b1,b2):
resultat=""
symbole="0123456789ABCDEF"
if len(b1)>len(b2):
n = len(b1)
else:
n = len(b2)
for i in range(0,n,-1):
symbole[i]==i
resultat=16**i*n[-i]+resultat
return resultat
Hey @amber umbra!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
Prends le temps de réfléchir à ce que tu veux faire
Tu veux assigner A Ã 10, B Ã 12 etc
Je comprends même pas ce que tu veux faire
ouais c nul
def add_hexa(b1,b2):
resultat=""
symbole="0123456789ABCDEF"
if len(b1)>len(b2):
n = len(b1)
else:
n = len(b2)
for i in range(0,n,-1):
symbole[i]==i
resultat=16**i*n[-i]+resultat
return resultat
Hey @amber umbra!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
Fais une pause
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.