#🔒 need help to convert number in base 16 to nummber in decimal pls

145 messages · Page 1 of 1 (latest)

amber umbra
#

pls help

cerulean willowBOT
#

@amber umbra

Python help channel opened

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.

amber umbra
#

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

cerulean willowBOT
#

Hey @amber umbra!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
amber umbra
#

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

quaint anchor
#

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

amber umbra
#

i have to use "foré

#

for"

quaint anchor
#

Alright

amber umbra
#

how?

quaint anchor
#

You can convert anything to a string useing str

amber umbra
#

yeah i know

quaint anchor
#

So basically you can revert all operations you do now

amber umbra
#

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

quaint anchor
#

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 ?

amber umbra
quaint anchor
#

Yes

amber umbra
# quaint anchor 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

cerulean willowBOT
#

Hey @amber umbra!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
amber umbra
#

this?

quaint anchor
#

Well you'll have an error verifying the type of x

#

But try it

#

That's the best way to tell

amber umbra
#

yes

#

x have an error

quaint anchor
#

If your input x is in base 16 likely it's a string

amber umbra
#

so i put "x"?

quaint anchor
#

No

#

You check if x is a str

#

Not a int

amber umbra
#

ok

quaint anchor
#

But you can't use the division rhing to compute the digits

amber umbra
#

wait

quaint anchor
#

Bc you are using a str

amber umbra
#

mhhh

#

wait

quaint anchor
#

You need to figure out another way

#

Based on the value of each letter and it's position, for example

amber umbra
#

come dm pls

#

can i?

quaint anchor
#

No

#

Stay there

#

Si tu veux passer en français on peut

amber umbra
#

ah

#

ok

#

c mieux

quaint anchor
#

Ok

amber umbra
#

mais dcp je dois faire comment?

#

j'abadonne ce programme?

quaint anchor
#

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

amber umbra
#

j'ai penseé a utiliser un for i in range

quaint anchor
#

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)

cerulean willowBOT
amber umbra
#

ok

quaint anchor
#

Mince att

#

!e

x = "AEB3"
for i in range(len(x)):
 print(x[i])

cerulean willowBOT
quaint anchor
#

Tu as l'idée

amber umbra
#

mhhh

quaint anchor
#

Tu peut obtenir les symbols comme ca

amber umbra
#

ouais

quaint anchor
#

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

amber umbra
#

mhh

#

ok je vais tester

quaint anchor
#

B7 = B*16 + 7

amber umbra
#

ahhhhhh

#

c mieux comme ça

#

donc faudrai aussi chnger la puissance

quaint anchor
#

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

amber umbra
#

oui je vois

#

att

#

euh

amber umbra
#

comment je peux faire?

#

avec symbole[...]?

quaint anchor
#

Je te laisse y reflechir

#

Les dictionnaires ca te dit quelque chose ?

amber umbra
#

j'essaie mais cpas simple

quaint anchor
#

Tu peux aussi utiliser la position dans une liste

#

Par example

amber umbra
#

j'ai pas encore fait ça en classe

quaint anchor
#

Tu peux faire un gros bloc de if/elif

amber umbra
#

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]

cerulean willowBOT
#

Hey @amber umbra!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
amber umbra
#

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

quaint anchor
#

Je comprends pas pq tu me parle de somme d'hexa mtn ?

amber umbra
#

ah ouii

#

lstb

#

c ancien

quaint anchor
#

Mas plus simple de convertir en base 10, De sommer et de reconvertir en base 16 ?

amber umbra
#

enft le but c'est d'additionner en base 16

#

dcp je convertir en base 10

amber umbra
quaint anchor
#

Oui bah fait

#

Tu ecris la fonction et tu la test

amber umbra
#

ç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

cerulean willowBOT
#

Hey @amber umbra!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
amber umbra
#

qu'est ce qui marche pas?

#

HELPPPPPPPPPPPPPP

quaint anchor
#

Prends le temps de réfléchir à ce que tu veux faire

#

Tu veux assigner A à 10, B à 12 etc

quaint anchor
amber umbra
#

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

cerulean willowBOT
#

Hey @amber umbra!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
amber umbra
#

j'ai fais ça mais ça marche pas

#

ça ma saouler

quaint anchor
#

Fais une pause

cerulean willowBOT
#
Python help channel closed for inactivity

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.