PDA

View Full Version : [JAVA] Da String a byte[] ???


Neo_Hackman
30-03-2011, 16:03
EDIT: Ho risolto! Semplicemente anche se i valori vengono stampati sballati i byte nell'array sono memorizzati correttamente, per cui i calcoli avvengono in maniera corretta!


Grazie cmque!!!

Potete chiudere se volete! ;)

Ciao! ;)




======================


Ciao a tutti!!

Sto sviluppando un programmino per Android e mi sono bloccato!:D

Il mio problema è convertire una stringa rappresentante dei numeri HEX in byte[].

Ecco l'esempio:

Mia stringa = 00255339DBB9

e vorrei che venisse messa così:

private byte[] STRINGA_TO_BYTE = {(byte) 0x00, (byte) 0x25, (byte) 0x53, (byte) 0x39, (byte) 0xDB, (byte) 0xB9};

Mi potete aiutare???


Grazie 1000!!:) :)




P.S: Se può essere utile attualmente uso questo metodo:

public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}


a cui gli passo come stringa in input

Mia stringa = 00255339DBB9

-se stampo a video

String STAMPA = Integer.toString(STRINGA_TO_BYTE[0]);

mi restituisce "0" (al posto che 00)

-se stampo a video

String STAMPA = Integer.toString(STRINGA_TO_BYTE[1]);

mi restituisce "25" (CORRETTO)

-se stampo a video

String STAMPA = Integer.toString(STRINGA_TO_BYTE[2]);

mi restituisce "53" (CORRETTO)

-se stampo a video

String STAMPA = Integer.toString(STRINGA_TO_BYTE[3]);

mi restituisce "39" (CORRETTO)

-se però stampo a video

String STAMPA = Integer.toString(STRINGA_TO_BYTE[4]);

mi restituisce "-37" (al posto di "DB")


-idem se stampo a video

String STAMPA = Integer.toString(STRINGA_TO_BYTE[5]);

mi restituisce "-71" (al posto di "B9")