Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
0x12 (10 進数の 18) のバイト値があるとします。これを 10 進数の 12 に変換する必要があります。
byte hex = 0x12; byte dec = Byte.parseByte(String.format("%2x", hex)); System.out.println(dec); // 12
これを行うためのより良い方法はありますか (たとえば、文字列や解析を使用しない)?
これを試して:
byte dec = (byte)(hex / 16 * 10 + hex % 16);
元の入力が有効なBCDエンコーディングであると想定していることに注意してください。