Skip to content

Commit

Permalink
feat: Hex to Long conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
  • Loading branch information
manticore-projects committed Apr 2, 2024
1 parent a56934d commit 620db70
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/HexValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,20 @@ public HexValue withValue(String value) {
public String toString() {
return value;
}

public String getDigits() {
return value.toUpperCase().startsWith("0X")
? value.substring(2)
: value.substring(2, value.length()-1);
}

public Long getLong() {
return Long.parseLong(
getDigits()
, 16);
}

public LongValue getLongValue() {
return new LongValue(getLong());
}
}
29 changes: 29 additions & 0 deletions src/test/java/net/sf/jsqlparser/expression/HexValueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.sf.jsqlparser.expression;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.PlainSelect;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class HexValueTest {

@Test
void testHexCode() throws JSQLParserException {
String sqlString = "SELECT 0xF001, X'00A1'";
PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(sqlString);

HexValue hex1 = (HexValue) select.getSelectItem(0).getExpression();
HexValue hex2 = (HexValue) select.getSelectItem(1).getExpression();

Assertions.assertEquals("F001", hex1.getDigits());
Assertions.assertEquals(61441, hex1.getLong());
Assertions.assertEquals(61441, hex1.getLongValue().getValue());

Assertions.assertEquals("00A1", hex2.getDigits());
Assertions.assertEquals(161, hex2.getLong());
Assertions.assertEquals(161, hex2.getLongValue().getValue());
}
}

0 comments on commit 620db70

Please sign in to comment.