LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2018, 05:18 AM   #1
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Rep: Reputation: 18
Question Perl: string to hex number


Hello,

Code:
here I've run into another issue: 

   my @cmd_array = split (/ /,$cmd);
   my $rid = $cmd_array[$#cmd_array];
   chomp ($rid);
   my $rid_str_hex = sprintf "0x%02x", hex $rid;
So I'm getting output from one command and that output is a string:

Ex: 5, 7f, 4a ....

so,

1st issue, I have to convert that string to a hex number =>5 to become 0x05, 7f to be 0x7f...

2nd issue, I have to increment that number by one: lets say 4a+1 = 4b
and assign the result to a variable..

Any ideas howto handle this ?
 
Old 02-23-2018, 07:41 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Just convert the number using hex like you did on sprintf
Code:
my $number = hex((split(/ /, $cmd))[-1]);
printf "number = 0x%02x\n", $number;

$number++;
printf "number = 0x%02x\n", $number;
 
1 members found this post helpful.
Old 02-24-2018, 05:50 AM   #3
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by keefaz View Post
Just convert the number using hex like you did on sprintf
Code:
my $number = hex((split(/ /, $cmd))[-1]);
printf "number = 0x%02x\n", $number;

$number++;
printf "number = 0x%02x\n", $number;
Much thanks !!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
hex string to char[] retroCheck Programming 3 05-15-2015 04:41 PM
How to get hex offset of string otaku2k2 Linux - Software 6 01-30-2012 06:06 AM
Perl - regular expression to match variable number of digits after string kcleveland Programming 4 12-08-2011 04:46 AM
convert number (not hex) into Decimal number drManhattan Programming 10 10-15-2011 08:53 PM
Hex output of a hex/ascii input string mlewis Programming 35 04-10-2008 12:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration