Click to See Complete Forum and Search --> : perl: convert dec to hex and append to string
YaRness
01-24-2001, 11:27 AM
i have no idea how to do this. i was using printf to do it to a file, but i just wanna convert a decimal value to a hex string and stick it in another string. not sure how to go about that.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
YaRness
01-24-2001, 11:33 AM
nevermind, sprintf working now, for some reason it was printing the hex to output, and appending "1" to my string... i don't remember how i had it coded, so don't ask :P prolly just had some comma or something goofy in the wrong place.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
You could use the pack or unpack functions. They would take care of that for you. But if you got it working then use whatever you want. 'pack' and 'unpack' are pretty cool though.
YaRness
01-24-2001, 09:17 PM
i have not for the life of me been able to figure out pack and unpack from the man files yet. every time i look, it's to do the same thing, convert dec to hex, and then i always remember/relearn that i can use printf http://www.linuxnewbie.org/ubb/biggrin.gif
if you wanna give some simple examples (like, say, turning a decimal value into a hex string....), then by all means please do. i'm sure others would be interested as well.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
I am just starting to really understand the ins and outs of 'pack/unpack' so I'm not really sure how to explain it. Here are some examples that work though.
#! /usr/bin/perl
# packs a 32 bit binary string into its ASCII value
$foo = pack "B32", "01010000011001010111001001101100";
print $foo, "\n";
# Unpacks 'Perl' into it 32 bit binary value
$foo = unpack "B32", "Perl";
print $foo, "\n";
# unpacks '12' into its Hex value
$foo = unpack "H*", 12;
print $foo, "\n";
# packs a hex string into its string value
$foo = pack "H*", "3132";
print $foo, "\n";
Hope that helps. I'd be glad to explain whatever else I can.
YaRness
01-25-2001, 03:14 PM
that seems to translate from ascii to character representation or vice versa... if that can be used to change the value of decimal 10 into the character "A" (which represents 10 in hex), it's not immediately clear how one would do that.
anyway, printf (or sprintf) give so many formatting options and translations that it'd be a waste to use anything else.
your example, however, made it quite a bit clearer what pack and unpack are used for, and i appreciate that a lot. it's no wonder they aren't used much though.. the only thing off the top of my head i can think that would be useful for is sorting strings.. .but perl already has operators for doing most of that without translating in and out of ASCII (which i remember doing sometimes in school to sort junk, or figure out if a string contained letters or numbers, etc etc).
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
That's pretty true. The only reason I wanted to learn it all was so I could a JAPH. I wrote a different one where I generated my own 5 bit binary-type system. I don't really want to do that again so I figured I use 'pack/unpack'. I don't really use 'sprintf/printf' at all so i don't really know how they work. Could you elaborate on them?
-kel