Oppure

Loading
19/12/12 18:37
Dante.cpp
Salve, vorrei scrivere un piccolo script che, dati in input una serie di stringhe nella forma "4f 6a 23 d9 10" di lunghezza arbitraria, restituisca un'unica stringa in formato "Salve, vorrei scrivere un piccolo script che, dati in input una serie di stringhe nella forma "4f 6a 23 d9 10" di lunghezza arbitraria, restituisca un'unica stringa in formato "{parsed_message}x1a{parsed_message}xa1{parsed_message}x92\...", ottenuta anteponendo "{parsed_message}x" ad'ogni coppia di cifre separata da spazio, per ogni stringa immessa .
Io mi stavo approcciando cosi:
stringa = ""
byte = ""

while byte != "fine" :
	byte = raw_input("byte:")
	
	for i in range( len(byte) ):
		stringa += "\0x"+byte[i]+byte[i+1]

print stringa
x1aSalve, vorrei scrivere un piccolo script che, dati in input una serie di stringhe nella forma "4f 6a 23 d9 10" di lunghezza arbitraria, restituisca un'unica stringa in formato "{parsed_message}x1a{parsed_message}xa1{parsed_message}x92\...", ottenuta anteponendo "{parsed_message}x" ad'ogni coppia di cifre separata da spazio, per ogni stringa immessa .
Io mi stavo approcciando cosi:
stringa = ""
byte = ""

while byte != "fine" :
	byte = raw_input("byte:")
	
	for i in range( len(byte) ):
		stringa += "\0x"+byte[i]+byte[i+1]

print stringa
xa1Salve, vorrei scrivere un piccolo script che, dati in input una serie di stringhe nella forma "4f 6a 23 d9 10" di lunghezza arbitraria, restituisca un'unica stringa in formato "{parsed_message}x1a{parsed_message}xa1{parsed_message}x92\...", ottenuta anteponendo "{parsed_message}x" ad'ogni coppia di cifre separata da spazio, per ogni stringa immessa .
Io mi stavo approcciando cosi:
stringa = ""
byte = ""

while byte != "fine" :
	byte = raw_input("byte:")
	
	for i in range( len(byte) ):
		stringa += "\0x"+byte[i]+byte[i+1]

print stringa
x92\...", ottenuta anteponendo "Salve, vorrei scrivere un piccolo script che, dati in input una serie di stringhe nella forma "4f 6a 23 d9 10" di lunghezza arbitraria, restituisca un'unica stringa in formato "{parsed_message}x1a{parsed_message}xa1{parsed_message}x92\...", ottenuta anteponendo "{parsed_message}x" ad'ogni coppia di cifre separata da spazio, per ogni stringa immessa .
Io mi stavo approcciando cosi:
stringa = ""
byte = ""

while byte != "fine" :
	byte = raw_input("byte:")
	
	for i in range( len(byte) ):
		stringa += "\0x"+byte[i]+byte[i+1]

print stringa
x" ad'ogni coppia di cifre separata da spazio, per ogni stringa immessa .
Io mi stavo approcciando cosi:
stringa = ""
byte = ""

while byte != "fine" :
	byte = raw_input("byte:")
	
	for i in range( len(byte) ):
		stringa += "\0x"+byte[i]+byte[i+1]

print stringa
aaa
19/12/12 20:54
Poggi Marco
Per risolvere il problema, ti sarà utile utilizzare la finzione split().

Dalla documentazione ufficiale:
str.split([sep[, maxsplit]])
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

For example, ' 1  2   3  '.split() returns ['1', '2', '3'], and '  1  2   3  '.split(None, 1) returns ['1', '2   3  ']
Ultima modifica effettuata da Poggi Marco 19/12/12 20:59
aaa
19/12/12 23:09
Dante.cpp
Mi sto innamorando del python, in c sarebbero statti necessari 10 file di programma!!!:D
import string

stringa = ""
byte = ""

while byte != "fine":
	byte = raw_input("byte:")
	
	lista = string.split(byte)
	
	for i in range( len(lista) ):
		stringa += "\x"+lista[i]

stringa = stringa[0:len(stringa)-5]

print stringa


Grazie.
Ultima modifica effettuata da Dante.cpp 19/12/12 23:11
aaa
20/12/12 10:20
Poggi Marco
In realtà la variabile lista non serve:
byte = ""
 
while byte != "fine":
        byte = raw_input("byte:")
       
        stringa=""
       
        for i in string.split(byte):
                stringa += "\x"+i
 
stringa = stringa[0:len(stringa)-5]
 
print stringa
aaa
20/12/12 10:51
Dante.cpp
Fantastico, inoltre:
import string

stringa = ""
byte = ""

while byte != "fine":
	byte = raw_input("byte:")
	
	for i in string.split(byte):
		stringa += "\x"+i

print stringa[0:len(stringa)-5]
aaa