• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Identify whether the string is a actual string or hexadecimal.

 
shan raj
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have one string variable temp which holds either String or hexadecimal string.
for e.g:

The next code should identify whether its a hexadecimal or string. Based on the identification I need to call another method.

Could some one suggest me the way to do this.

Thanks in Advance.
Shan
 
shan raj
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that logic and it is not working.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which code/logic you tried ? Where was the problem ?
 
shan raj
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is



Expected output is "Is Hex String= true". But it says false and throws an NumberFormatException
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because the number is too large to fit into an int. Long.parseLong may work but can run into the same problem. BigInteger is also an option.

I'd follow the advise of the first reply there - use a Pattern to check. See java.util.regex.Pattern.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OR if you have an extensive use of Hex, decimal, binary, etc then try using
Apache Commons CodecHex class

 
Stefan Brandenberger
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shan raj wrote:The code is



Expected output is "Is Hex String= true". But it says false and throws an NumberFormatException


Hi shan

If you want to do it that way, you have to check each digit, not the whole string at once.

Stefan
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any cases where a string migt look like a hexadecimal number but be intended to be a string? (A place I worked at ran in to that. Think of things like "beef" or "cafe".)
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or cafebabe of course
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic