• Post Reply Bookmark Topic Watch Topic
  • New Topic

parse hex string  RSS feed

 
takaya treschi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an hex string that i have to interpretate according to a structure.
E.g 01332211223344 with structure byte,short,int.
In this case i would like to obtain :
byte is the conversion of 01
Short the conversion of 3322 and int the 11223344. Are there some java library to do such processing?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Since there is no way to tell which part of the string is which, you need to extract the various parts, and interpret them individually.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need any special library to do this. You can do it using methods in the String class, and other classes such as Byte, Short and Integer.

I would do it like this: Use the substring() method of the String class to split the string "01332211223344" into the three parts: "01", "3322", "11223344". Then use methods from the classes java.lang.Byte, java.lang.Short and java.lang.Integer to parse these three parts into a byte, short and int.

Lookup the class java.lang.String and the other classes in the Java API documentation to find out what methods you can use.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco freschi wrote:byte is the conversion of 01...

Just one additional thing that may be worth noting:

bytes in Java are signed, which means that if you convert a string like "FE", you will get -127, NOT 254, as you might have been expecting.

Winston
 
takaya treschi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is correct if you have only one string, but imagine you receive many strings with the same format, and you want always extract the same structure. It could be nice to save the structure in same place and then apply it to the string ....a sort of codec.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again

I might take the opposite tack and shoot whoever sends that sort of data
 
takaya treschi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it is a solution,but i was looking for a less violent solution 😀 . for example i was thinking to use the lambda expressions.... But still I have some problems. Someone can help me please?
Thanks
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you go and try to use a lambda expression, you should try to figure out how it is you actually want to handle this problem with regular Java code. Lambda expressions are not some kind of magic bullet. Yes, they can simplify code so that it reads a lot better and code that uses them can be a lot more generic and flexible but I don't see that there's anything special in the problem that you stated that would make me instantly think,"Yeah, a lambda expression would be sweet for this."
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
takaya treschi wrote:Yes it is correct if you have only one string, but imagine you receive many strings with the same format, and you want always extract the same structure. It could be nice to save the structure in same place and then apply it to the string ....a sort of codec.

You could just put the code for parsing this in a method, and then call the method when you need it, instead of copying and pasting the parsing code all over the place.

Why are you thinking about lambda expressions?
 
takaya treschi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to implement what you suggested and more or less it works, anyway it is not really flexible so everytime the structure changes I have to go through the code. I saw a library called preon. Has someone use it? Do you have a simple example?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how you can avoid changing the code if the structure changes, unless you use a format like XML where the individual items are kept separate from one another. Of course, XML may be overkill, something like "foo=01,bar=3322,baz=11223344" is easily parsed with standard JRE classes, and is flexible about which fields are there or not there, or if there are more fields than the code knows about.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
takaya treschi wrote:I tried to implement what you suggested and more or less it works, anyway it is not really flexible so everytime the structure changes I have to go through the code. I saw a library called preon. Has someone use it?

Fraid not, but I doubt whether there's any "magic bullet" here.

If you want to parse "some string" or indeed, "some stream" in a way that you only know about at runtime, then you need some way to pass the rules for parsing that String/stream before you start parsing it. And if you then need to create Java objects from it, then you're into the realms of reflection, or some framework that uses it, or possibly dependency injection, behind the scenes - and they are generally NOT simple (and not necessarily particularly fast either).

My suggestion: Back up and explain WHAT you want to do, not HOW you want to do it, because we seem to have strayed quite a way from your original question.

Are there lots of different "possible stream formats" or just 2 or 3? Does it really need to be "intuitive", or will a few static definitions do the trick? That will have a huge bearing on the look - and complexity - of your solution. And remember: the more complex it is, the greater the chance for it to go wrong.

My rule of thumb: Avoid reflection (or runtime decisions) if you possibly can - at least if you plan on using Java. For one thing, they make things very hard to test: Do you know the "corner cases" for a reflective framework? I sure don't; and I've been at this lark a long time.

There are also many other languages that are better suited to reflection (and lambdas), so an alternative might be to look at one of them.

Java is a statically-typed language, so any solution that relies on huge amounts of "dynamism" is probably not a good fit.

My 2¢.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!