Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: Hex Question(s)

by deadlock (Initiate)
on Mar 27, 2008 at 17:13 UTC ( #676802=note: print w/replies, xml ) Need Help??


in reply to Re^3: Hex Question(s)
in thread Hex Question(s)

Okay, that makes sense.

At this point it probably would make sense to expand on the problem that I'm trying to solve. Basically, what I need to do is take my hex string, split it into pairs and prepend those pairs with \x. Then I concatenate them all together, pack them and stick them into a DHCP packet. It looks something like this:
$theInt = 1501299200; $theHex = sprintf("%lx", $theInt); $hexPairs = split(/(\w{1,2})/, $theHex); while $val (@hexPairs) { if (!$thisPair eq "" ) { $finalHex .= "\x".$thisPair; } } $bytes .= pack("C/a*", $finalHex);
The resulting hex stuff should look like this: \x59\x7c\x02\x00 and if I replace $finalHex above with "\x59\x7c\x02\x00" then my code works fine (but that's not an acceptable solution because the initial integer isn't always going to be 1501299200).

I appreciate that this may not make a hell of a lot of sense or that it may be diffcult to follow what I'm trying to do so I am grateful for any help that you can give me.

Maybe I'm just overcomplicating things?

Replies are listed 'Best First'.
Re^5: Hex Question(s)
by Corion (Pope) on Mar 27, 2008 at 17:16 UTC

    Well, your description is quite roundabout for the effect you want to achieve. Basically, you want to build a byte string consisting of arbitrary characters.

    For building such stuff, Perl has pack, which builds a string out of various kinds of values.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://676802]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2020-02-28 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (127 votes). Check out past polls.

    Notices?