Add `NumberConverter`

This commit is contained in:
wtfsck 2022-06-20 21:39:01 +02:00
parent dc569ce982
commit 556d34ee75
1 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,95 @@
// SPDX-License-Identifier: MIT
// Copyright (C) 2018-present iced project and contributors
package com.github.icedland.iced.x86;
public final class NumberConverter {
public static long toUInt64(String value) {
int radix = 10;
if (value.startsWith("0x")) {
value = value.substring(2);
radix = 16;
}
try {
return Long.parseUnsignedLong(value, radix);
}
catch (NumberFormatException ex) {
throw new UnsupportedOperationException(String.format("Invalid number: '%s'", value), ex);
}
}
public static long toInt64(String value) {
String unsigned_value = value;
long mult;
if (unsigned_value.startsWith("-")) {
mult = -1;
unsigned_value = unsigned_value.substring(1);
}
else
mult = 1;
int radix = 10;
if (unsigned_value.startsWith("0x")) {
unsigned_value = unsigned_value.substring(2);
radix = 16;
}
try {
return Long.parseLong(value, radix) * mult;
}
catch (NumberFormatException ex) {
throw new UnsupportedOperationException(String.format("Invalid number: '%s'", value), ex);
}
}
public static int toUInt32(String value) {
long v = toUInt64(value);
if (Long.compareUnsigned(v, 0xFFFF_FFFF) <= 0)
return (int)v;
throw new UnsupportedOperationException(String.format("Invalid number: '%s'", value));
}
public static int toInt32(String value) {
long v = toInt64(value);
if (-0x8000_0000 <= v && v <= 0x7FFF_FFFF)
return (int)v;
throw new UnsupportedOperationException(String.format("Invalid number: '%s'", value));
}
public static short toUInt16(String value) {
long v = toUInt64(value);
if (Long.compareUnsigned(v, 0xFFFF) <= 0)
return (short)v;
throw new UnsupportedOperationException(String.format("Invalid number: '%s'", value));
}
public static short toInt16(String value) {
long v = toInt64(value);
if (-0x8000 <= v && v <= 0x7FFF)
return (short)v;
throw new UnsupportedOperationException(String.format("Invalid number: '%s'", value));
}
public static byte toUInt8(String value) {
long v = toUInt64(value);
if (Long.compareUnsigned(v, 0xFF) <= 0)
return (byte)v;
throw new UnsupportedOperationException(String.format("Invalid number: '%s'", value));
}
public static byte toInt8(String value) {
long v = toInt64(value);
if (-0x80 <= v && v <= 0x7F)
return (byte)v;
throw new UnsupportedOperationException(String.format("Invalid number: '%s'", value));
}
public static boolean toBoolean(String value) {
switch (value) {
case "true":
return true;
case "false":
return false;
default:
throw new UnsupportedOperationException(String.format("Invalid boolean: '%s'", value));
}
}
}