Hallo,
ich möchte einen String in einen hexadezimalen String konvertieren (das klappt auch) und anschließend in einen Long umwandeln.
Das klappt allerdings nicht.
Also die Methode HextoLong funktioniert nur für Zahlen, aber nicht für Hexadezimale Strings.
Wo liegt der Fehler?
ich möchte einen String in einen hexadezimalen String konvertieren (das klappt auch) und anschließend in einen Long umwandeln.
Das klappt allerdings nicht.
Java:
public static String stringToHex(String base)
{
StringBuffer buffer = new StringBuffer();
int intValue;
for(int x = 0; x < base.length(); x++)
{
int cursor = 0;
intValue = base.charAt(x);
String binaryChar = new String(Integer.toBinaryString(base.charAt(x)));
for(int i = 0; i < binaryChar.length(); i++)
{
if(binaryChar.charAt(i) == '1')
{
cursor += 1;
}
}
if((cursor % 2) > 0)
{
intValue += 128;
}
buffer.append(Integer.toHexString(intValue) + " ");
}
return buffer.toString();
}
public static long HextoLong(String s) {
long num = 0;
try{
BufferedReader bf = new BufferedReader(new StringReader(s.substring(0,16)));
String hex = bf.readLine();
num = Long.parseLong(hex,16);
}
catch (IOException e) {
e.printStackTrace();
}
return num;
}
public static void main(String[] args)
{
String s = "The cat in the hat";
System.out.println(s);
System.out.println(stringToHex(s));
long m = HextoLong(stringToHex(s));
System.out.println(m);
}
Also die Methode HextoLong funktioniert nur für Zahlen, aber nicht für Hexadezimale Strings.
Wo liegt der Fehler?