Skip to content

Commit 620db70

Browse files
feat: Hex to Long conversion
Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
1 parent a56934d commit 620db70

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/main/java/net/sf/jsqlparser/expression/HexValue.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,20 @@ public HexValue withValue(String value) {
4646
public String toString() {
4747
return value;
4848
}
49+
50+
public String getDigits() {
51+
return value.toUpperCase().startsWith("0X")
52+
? value.substring(2)
53+
: value.substring(2, value.length()-1);
54+
}
55+
56+
public Long getLong() {
57+
return Long.parseLong(
58+
getDigits()
59+
, 16);
60+
}
61+
62+
public LongValue getLongValue() {
63+
return new LongValue(getLong());
64+
}
4965
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package net.sf.jsqlparser.expression;
2+
3+
import net.sf.jsqlparser.JSQLParserException;
4+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
5+
import net.sf.jsqlparser.statement.select.PlainSelect;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class HexValueTest {
12+
13+
@Test
14+
void testHexCode() throws JSQLParserException {
15+
String sqlString = "SELECT 0xF001, X'00A1'";
16+
PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(sqlString);
17+
18+
HexValue hex1 = (HexValue) select.getSelectItem(0).getExpression();
19+
HexValue hex2 = (HexValue) select.getSelectItem(1).getExpression();
20+
21+
Assertions.assertEquals("F001", hex1.getDigits());
22+
Assertions.assertEquals(61441, hex1.getLong());
23+
Assertions.assertEquals(61441, hex1.getLongValue().getValue());
24+
25+
Assertions.assertEquals("00A1", hex2.getDigits());
26+
Assertions.assertEquals(161, hex2.getLong());
27+
Assertions.assertEquals(161, hex2.getLongValue().getValue());
28+
}
29+
}

0 commit comments

Comments
 (0)