Converting to Hex

Is there a simple way, like a one liner perhaps, that will give me the 
octal/hex value of a character?  I need to look certain characters up, 
so I would like to be able to give it something like an 'a' or even 
"\n" and have it answer 141 or 012 (assuming UNIX).  Thanks for your 
time.

James

0
james
9/23/2002 4:49:09 PM
perl.beginners 29388 articles. 4 followers. Follow

3 Replies
774 Views

Similar Articles

[PageSpeed] 0
Get it on Google Play
Get it on Apple App Store

check out sprintf() and ord()

quickly for you example:
## hex
perl -e 'printf("%x", ord("\n"))'

## oct
perl -e 'printf("%o", ord("\n"))'



> -----Original Message-----
> From: James Edward Gray II [mailto:james@grayproductions.net]
> Sent: Monday, September 23, 2002 12:49 PM
> To: beginners@perl.org
> Subject: Converting to Hex
> 
> 
> Is there a simple way, like a one liner perhaps, that will 
> give me the 
> octal/hex value of a character?  I need to look certain 
> characters up, 
> so I would like to be able to give it something like an 'a' or even 
> "\n" and have it answer 141 or 012 (assuming UNIX).  Thanks for your 
> time.
> 
> James
> 
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> 

----------------------------------------------------------------------------
--------------------
The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.

0
nikola_janceski
9/23/2002 4:57:30 PM
James Edward Gray II wrote:
> 
> Is there a simple way, like a one liner perhaps, that will give me the
> octal/hex value of a character?  I need to look certain characters up,
> so I would like to be able to give it something like an 'a' or even
> "\n" and have it answer 141 or 012 (assuming UNIX).  Thanks for your
> time.

my $oct = sprintf '%o', $num;
my $hex = sprintf '%x', $num



John
-- 
use Perl;
program
fulfillment
0
krahnj
9/23/2002 5:08:55 PM
>>>>> "John" == John W Krahn <krahnj@acm.org> writes:

John> James Edward Gray II wrote:
>> 
>> Is there a simple way, like a one liner perhaps, that will give me the
>> octal/hex value of a character?  I need to look certain characters up,
>> so I would like to be able to give it something like an 'a' or even
>> "\n" and have it answer 141 or 012 (assuming UNIX).  Thanks for your
>> time.

John> my $oct = sprintf '%o', $num;
John> my $hex = sprintf '%x', $num

Not quite.  You're missing the step that converts 'a' to 97 decimal
so that the '%o' can work:

my $oct_string = sprintf '%03o', ord($character);

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
0
merlyn
9/24/2002 3:55:01 PM
Reply:

Similar Artilces:

how to convert hex string to hex bytes?
Hi I've been trying and haven't found a solution for this yet and hopefully some of you can give me a direction to solve this problem. I have a string in hex representation i.e, 64 chars 'AB23FFFE.....' How can I convert it to 32 chars String where each byte is $AB $23 $FF $FE .... ? Even if I do it manually, can I store the hex(non ASCII) to String? Thanks for looking!! edwin wrote: > I have a string in hex representation i.e, 64 chars 'AB23FFFE.....' > How can I convert it to 32 chars String where each byte is $AB $23 $FF > $FE .... ? Th...

Convert from hex
echo http%3A%2F%2Fwww.merifiles.com%2Fuploads%2Fmf_aR_Ek_Ajnabi_-_01_-_Ek_Ajnabi_%28Mama_Told_Me%29%28128Kbps%29.mp3| perl -e ' $_=<>;s/%(..)/\\x$1/xg; print "$_"; ' This outputs http\x3A\x2F\x2Fwww.merifiles.com\x2Fuploads\x2Fmf_aR_Ek_Ajnabi_-_01_-_Ek_Ajnabi_\x28Mama_Told_Me\x29\x28128Kbps\x29.mp3 Why does print not print the hex character in question? But, -bash-2.05b$ perl -e ' > print "http\x3A\x2F\x2Fwww.merifiles.com\x2Fuploads\x2Fmf_aR_Ek_Ajnabi_-_01_-_Ek_Ajnabi_\x28Mama_Told_Me\x29\x28128Kbps\x29.mp3" > ' http:/...

Converting string to Hex
I'm trying to convert a single character, into a Hex value, and compare. Here's my code: dim int as integer int = Integer.Parse(messages.Substring(x, 1), System.Globalization.NumberStyles.HexNumber) If int = &H0 Or int = &HC Then 'Do stuff end if Unfortunately, this causes an error with the character I'm trying to catch. Does anyone have any ideas? int = Integer.Parse(messages.Substring("x", 1), System.Globalization.NumberStyles.HexNumber) View My Blog Download My URL Rewriter and Reverse ProxyOnly $9.95/month, ASP.NET, 2GB & SQL 2005...

How to convert decimal to hex?
Hi all, Please tell me how to convert a decimal number like 255 into its corresponding hex value. For example I want a result of "ff" for a decimal of 255. I've tried playing with the functions ord, hex, pack, ... but with no result. Thank you. Teddy's Center: http://teddy.fcc.ro/ Mail: orasnita@home.ro $HEX = sprintf "%1x", $DEC; for more, perldoc -f sprintf Rgds, Connie ----- Original Message ----- From: "Octavian Rasnita" <orasnita@home.ro> To: <beginners-cgi@perl.org> Sent: Friday, August 09, 2002 3:11 ...

converting HEX to numbers
I need to call Win API methods from PB and I have an example of how they do it in VB. So I am converting the code to PB. However, there are variable declarations that I need help with: HOW CAN I CONVENRT THESE HEX NUMBERS INTO REAL NUMBERS TO DECLARE IN PB? e.g. &H20 is equal to what in PB? constant integer DT_RIGHT = &H2 constant integer DT_SINGLELINE = &H20 constant integer DT_TABSTOP = &H80 constant integer DT_TOP = &H0 constant integer DT_VCENTER = &H4 constant integer DT_WORDBREAK = &H10 constant integer PS_ENDCAP_FLAT = &H200 ...

convert binary to hex
--0016e64ca8a6352578047a573c11 Content-Type: text/plain; charset=ISO-8859-1 Hi ,all : I'd like to convert binary file intohex , but when I use perl's built in functions I keep getting extra bits on the end. cat bin_file 00000000000000000000100001100010 00010000010100100001100001000010 My perl script is the following : #!/usr/bin/perl use strict; while (<>){ my $line = $_; my $hex = unpack ("H*", pack("B*", $line)); print "$hex\n"; } But the result is : 0x0000086200 0x1052184...

converting character to hex
In vb.net, I would like to convert a character to hex and decimal (both) and then examine values. How is this done? Thanks See the Microsoft.VisualBasic.Conversion namespace for .Hex().  To convert from Hex to decimal I believe you just put a & in front of the hex value and either the Val() or Str() will return the decimal value. may i know how to get the data from hexadecimal format using visual basic.net ..i have the data strored in hexadecimal format in databse. I want retrie the data from the database ..can any body help me please... Thanks Sumna...

Convert ASCII/HEX
This is driving me insane: When e export a stream (like login-script) with ICE or NLIST, I only get ASCII code. Anyone any Idea how I can get tis converted to TEXT?? __Leo , > When e export a stream (like login-script) with ICE or NLIST, I only get > ASCII code. Anyone any Idea how I can get tis converted to TEXT?? > NLIST should show it as text. What syntax are you using? - Anders Gustafsson, Engineer, CNE6, ASE NSC Volunteer Sysop Pedago, The Aaland Islands (N60 E20) Novell does not monitor these forums officially. Enhancement requests for all Novell products...

Converting HEX to ASCII
I am using the "photo" attribute to store the location the jpg of our users (this is what eGuide users). When I try to read this attribute out with IDM 3.0, it returns a HEX value. How can I get it in the ASCII value. I have the same type of issue trying to read the Personal Login Script.....Any ideas? Actually it is probably returning a base64 encoded value rather than a hex value - this is because the attribute is a octet string attribute which may or may not represent characters (usually it is the actual picture data). An example of how to decode base64 from a p...

How to convert decimal to Hex?
Usually it is customary to include body in your posts but here we go. There are infinitely many ways to do this but the two I have used are decimal to hex or decimal to binary to hex. THe main advantage of the latter is that it is extremely easy to go from binary to hex and from decimal to binary. First you need to know how many bytes are in the number you are dealing with. Each byte is two hexidecmal digits (digits isn't really the right word but you know what I mean). So for a two byte or 16 bit number there are 4 hex digits. hexidecimal digits go from 0 to 15 wi...

Convert Hex to Char
Dear all, i tried to search on tis forum for converting a hex value to Char, but seems like i cant found any related. can any1 direct me o give me some hint?let say user type in 343536 in my text box, once they start the button subroutine, they should get 4 5 6 in char. Best Regardslife's Ng What's the formula for converting HEX to char? To me it just seems to scratch chars delimited by some particular number (3 in this case).Welcome to my SQL/ASPNET forum for Chinese http://51up.org/bbs/forumdisplay.php?fid=38 what yo mean by Hex, is it  means hexadecimal, if so how yo...

Convert Hex to Unicode...
Hi... Does anyone know a simple way to convert hex back to the original chinese/arabic etc. language? I found a sample asking to do something like &#90AE, &#7BB1; but I dont really understand it... It will be easier to convert Arabic than Chinese because the Chinese alphabet is more than two thousand characters.  The link below contains the ASCII lookup table which includes hex values.  I am sorry I cannot help you with the actual conversion.  Hope this helps. http://www.lookuptables.com/Kind regards,Gift Peddie...

Convert Hex To Int
I'm wondering if there is any easy way to convert a hex value to an int. I have a 3rd party component, that when used with ASP.net will only accept color values as an int. For example:#FFFFFF = 16777215#000000 = 0 Hopefully I explained what I am looking for clearly. Thanks for any help,Russ Merry Christmas! <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) {   Label1.Text = HexToIn...

How to convert MemoryStream string to Hex?
I'm doing some encryption and, normally would return a baes 64 string but it's causing issues with URLs, so we are going to use hex strings instead....but I'm having an issue... Instead of:Return Convert.ToBase64String(ms.GetBuffer(), 0, CInt(ms.Length)) I'd like to return a hex string... I need a pointer here....thanks..... Dim myNumber as integer = 'Get your Number from the buffer Dim HexString as string = myNumber.tostring("X")Bryan SampicaAuction Site StarterKit |My Blog The tough part is the "get your number from the buffer" part.... That's what I don't get/know. I'm trying to...

Web resources about - Converting to Hex - perl.beginners

Facebook Begins Converting Users To HTTPS
Are you willing to sacrifice a little bit of speed for a lot more safety? Facebook is asking that very question with its announcement that it ...

Facebook No Longer Converting Groups Into Pages
Back when Facebook first launched Facebook Pages, many businesses and brands who had built up substantial audiences in their Facebook Groups ...

Vert - simply converting for iPhone, iPad, and iPod touch on the iTunes App Store
Get Vert - simply converting on the App Store. See screenshots and ratings, and read customer reviews.

Converting SIM Card to Micro SIM Card - Flickr - Photo Sharing!
Place new Micro SIM into the iPhone SIM card tray

Ayaan Hirsi Ali on Converting Muslims to Christianity - YouTube
Complete video at: http://fora.tv/2010/07/29/Nomad_From_Islam_to_America_with_Ayaan_Hirsi_Ali Ayaan Hirsi Ali explains her support of missionary ...

Click go fears of converting print files
Is there a way to convert a print queue item to a .RTF or .PDF file? I like to save or email them. - The Sydney Morning Herald

Sudanese woman ordered to hang under sharia law for converting to Christianity gives birth
Khartoum, Sudan: A Christian Sudanese woman sentenced to hang for apostasy has given birth in jail, a Western diplomat said on Tuesday.

Imams warn against radicalism to Aboriginal inmates converting to Islam
The prison system has enlisted the help of ASIO to crack down on radicalisation behind bars amid revelations that Aboriginals are converting ...

Converting the world's companies one by one - The Science Show - ABC Radio National (Australian Broadcasting ...
Image: Trucks carrying logs make their way up a road in Jambi, Indonesia. A vast area of the Sumatran forest, and orangutan habitat, is being ...

Rothesay building new arena, converting existing rink to fieldhouse
The Town of Rothesay plans to build a new arena and convert the existing one into a fieldhouse.

Resources last updated: 1/12/2016 7:48:45 AM