Good afternoon, you need to send data to the server in format 16 of the river system ASCII. For example, -1 as FFFFFFFF respectively, -2 as FFFFFFFE

What is this fff ? How to convert int a =-1 to FFFFFFFF ?

Thanks for attention

3 answers 3

This type of record is called an additional code . In Java, you can get such an entry by standard means:

 Integer.toHexString(1); // 1 Integer.toHexString(2); // 2 Integer.toHexString(-1); // ffffffff Integer.toHexString(-2); // fffffffe 

If you need to write positive numbers to the left with zeros up to 8 characters, help String.format() :

 String.format("%8s", Integer.toHexString(123)).replace(' ', '0') // 0000007b 
  • why not String.format("%08x", 123) ? - zRrr 4:02 pm
  • Because formatting modifiers flew out of my head, and it was too lazy to go into the documentation) Your option is better, of course. - fori1ton 4:16 pm

The easiest way

 int a =-1; System.out.println(Integer.toHexString(a)); 

    In java so

      String input = ... // my UTF-16 string StringBuilder sb = new StringBuilder(input.length()); for (int i = 0; i < input.length(); i++) { char ch = input.charAt(i); if (ch <= 0xFF) { sb.append(ch); } } byte[] ascii = sb.toString().getBytes("ISO-8859-1"); // aka LATIN-1 
    • takes the input as a type "-1"? - elik 2:49 pm
    • Suppose input = "-1" , what happens? - Igor
    • I need to convert -1 to ffffffff and not to byte - elik