laziness, impatience, and hubris PerlMonks

### Dec to Hex (NON-ASCII mode )

by MathewM (Initiate)
 on Dec 09, 2010 at 08:56 UTC Need Help??
MathewM has asked for the wisdom of the Perl Monks concerning the following question:

I converted 100(Dec) into 64(Hex) using the following way, sprintf("%X", 100) However, I found 64 is 36 34(ASCII text) rather than 0110 0100(\x64). I want to know how to get 0110 0100(\x64) . Thanks.

Replies are listed 'Best First'.
Re: Dec to Hex (NON-ASCII mode )
by samarzone (Pilgrim) on Dec 09, 2010 at 09:10 UTC

sprintf returns a string so if you try to get ascii value of returned string, you get value of character "6" and "4" rather than that of 6 and 4.

Not completely sure what do you want to do but if you want to get a binary representation of 100(dec) in a string you can use "%b"

perl -le '\$_ = sprintf("%b", 100);print'

Output:

1100100
Re: Dec to Hex (NON-ASCII mode )
by Anonymous Monk on Dec 09, 2010 at 09:15 UTC
The following are equivalent:
• 100 decimal
• 01100100 binary
So, are you trying to get the binary representation of 100?
```unpack 'B*', pack 'c', 100;
01100100

Also:

```>perl -wMstrict -le
"print qq{'\x64'}, pack 'C', 100;
"
'd'd
Re: Dec to Hex (NON-ASCII mode )
by MishaMoose (Scribe) on Dec 09, 2010 at 14:40 UTC

I think you have a perceptual issue. 100(dec) 64(hex) and 0110 0100(bin) are all character representations of the same thing . The value stored in the variable you are displaying. Even when you 'convert' it to binary what you will get is the ascii characters "01100100". 100(dec) is stored in memory as the actual bits 01100100 but if you look at that value it is actually 'd' in ASCII.

The value is not the representation.

Hope this helps some

Misha/Michael - Russian student, grognard, bemused observer of humanity and self professed programmer with delusions of relevance
Re: Dec to Hex (NON-ASCII mode )
by ikegami (Pope) on Dec 09, 2010 at 18:13 UTC

Your terminology is very confusing, but it sounds to me like you want the numerical value 6416 aka 10010 from the string "64". For that, you can use hex.

```\$ perl -E'say 0x64 == hex("64") ?"yes":"no"'
yes

In case I misunderstood, here are some other related conversions:

 numerical value 6416 from string "64" hex("64") numerical value 6416 from string "100" "100" packed byte 6416 from numerical value 6416 chr(0x64) packed byte 6416 from string "64" chr(hex("64")) packed byte 6416 from string "100" chr("100") string "64" from numerical value 6416 sprintf("%X", 0x64)

pack 'C' would work in lieu of chr.

Update: I just noticed your followup. It seems that you want the packed byte 6416 (aka "d" in ASCII), but you didn't specify from what.

```\$ perl -E'say "\x64" eq chr(hex("64")) ?"yes":"no"'
yes

\$ perl -E'say "\x64" eq chr(0x64) ?"yes":"no"'
yes

That could also be written

```\$ perl -E'say "\x64" eq pack("C", hex("64")) ?"yes":"no"'
yes

\$ perl -E'say "\x64" eq pack("C", 0x64) ?"yes":"no"'
yes
Re: Dec to Hex (NON-ASCII mode )
by MathewM (Initiate) on Dec 09, 2010 at 09:26 UTC
I want to get \x64 rather than a string '6''4'. thanks
Then use chr. Or some variety of pack.
Re: Dec to Hex (NON-ASCII mode )
by rovf (Priest) on Dec 09, 2010 at 13:16 UTC

On one hand, I can understand your frustration/aggravation of seeing someone post the same question on multiple sites.

On the other hand, I don't seem to recall that PM had a requirement (or expected etiquette) that an individual is not allowed to post a question on this site as well as other sites (with or without explicit declaration of such "crossposting"). Also, I was unable to locate such documentation on this site that states this policy.

If you don't mind, could you please help point me to the documentation that states this policy so that I, an ignorant fool, can become enlightened?

It's not an agreement, it's common sense (plus politeness). And the point is not to forbid crossposting, but to explicitly point out the cross-post:

If you crosspost on sites A and B, and you already get a good answer on site A, persons reading only site B will not be aware that you already received an answer on A, and will try to unnecessarily answer your question again. You, kind of, waste the effort of people trying to help you.

--
Ronald Fischer <ynnor@mm.st>

Create A New User
Node Status?
node history
Node Type: perlquestion [id://876207]
Approved by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2017-08-23 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (345 votes). Check out past polls.

Notices?