1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
31
Rep Power
2

Hi All,

I have an array of numbers, which are in hex.
Now I want to add 4000(it is also in hex) to each and every number of that array ans store them back in to an array.
I don't want to print the addition but I want to store the answer in hex only.

Like,

if array = {1,2,300,a.b}

2. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Jun 2012
Location
Paris area, France
Posts
846
Rep Power
496
What do you mean when you say that your array of numbers is inhex? If they are actual number, you don't care whether their representation is hex, decimal, octal or binary, do you? So the question is: where to these "hex" numbers come from, what type of precessing have they undergone previously?
3. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Jun 2012
Location
Paris area, France
Posts
846
Rep Power
496
Hi,

my previous answer was a bit short and quick, because I did not have time to elaborate.

If your array contains results of arithmetic calculations, then the elements are stored as numbers (internal binary format). You don't need to do anything special to make calculations on them. Same thing if they were declared as hex numbers with a syntax such as "my \$num = 0x42;".

You can see that in the following session under the perl debugger:

Code:
DB<1>  \$c = 0x42;

DB<2> print \$c;
66
DB<3> printf "%d", \$c;
66
DB<4> printf "%x", \$c;
42
DB<5> printf 0x4000
16384
DB<6>  printf "%x", \$c + 0x4000
4042
DB<7> printf "%d",  \$c + 0x4000
16450

You may have a problem, however, if your array elements are string representations of hex numbers (which is the case if they were read from a file). They you need to convert those strings into actual numbers, using the hex or oct functions.

Code:
DB<9> \$e = "0x42"

DB<10> print 0x1 + \$e
1
DB<12>  print 0x1 + oct \$e
67
DB<13> print hex \$e
66
DB<14> print 1 + hex \$e
67
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
31
Rep Power
2
Hi Laurent_R
My question is:

Suppose I have an array of strings
@array1=(ab,cd,ef,gh,ij,kl,mn,op,qr,st,uv,wx,yz);

Now, starting from zero I want to increment hexadecimal number at the end of each member and want to store it in another array.

Like this,
@array2=(ab_0,cd_1,ef_2,gh_2,ij_3,kl_4,mn_5,op_6,qr_7,st_8,uv_9,wx_a,yz_b);

Originally Posted by Laurent_R
Hi,

my previous answer was a bit short and quick, because I did not have time to elaborate.

If your array contains results of arithmetic calculations, then the elements are stored as numbers (internal binary format). You don't need to do anything special to make calculations on them. Same thing if they were declared as hex numbers with a syntax such as "my \$num = 0x42;".

You can see that in the following session under the perl debugger:

Code:
DB<1>  \$c = 0x42;

DB<2> print \$c;
66
DB<3> printf "%d", \$c;
66
DB<4> printf "%x", \$c;
42
DB<5> printf 0x4000
16384
DB<6>  printf "%x", \$c + 0x4000
4042
DB<7> printf "%d",  \$c + 0x4000
16450

You may have a problem, however, if your array elements are string representations of hex numbers (which is the case if they were read from a file). They you need to convert those strings into actual numbers, using the hex or oct functions.

Code:
DB<9> \$e = "0x42"

DB<10> print 0x1 + \$e
1
DB<12>  print 0x1 + oct \$e
67
DB<13> print hex \$e
66
DB<14> print 1 + hex \$e
67
5. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Jun 2012
Location
Paris area, France
Posts
846
Rep Power
496
Hi,

Code:
@array1=(ab,cd,ef,gh,ij,kl,mn,op,qr,st,uv,wx,yz);
This is not a valid array of strings, nor a valid array of anything, nor are the things in it valid hexadecimal numbers. We can't help you if we do not know exactly what you have in the array.

Presumably, you will have to convert hexadecimal string representation of numlbers into actual numbers, add whatever you want to add, and convert back to hex string representations if that's what you need in your result array.

But again, show us exactly what you have in your input array, that is a necessary condition to enable me to tell you how to proceed with the conversion.
6. No Profile Picture
Permanently Banned
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
5
Rep Power
0

#### Hexdecimal

7. Originally Posted by zeeshanaayan07
there are plenty of information/answers if you search for this on Google.
8. I didn't notice at first that the original question was kind of old, but a possible solution:

Code:
#!/usr/bin/perl
use strict;
use warnings;

use Data::Dumper;

my @array1= qw/ab cd ef gh ij kl mn op qr st uv wx yz/;

my \$n = 0;
my @array2 = map {\$_ . sprintf("_%x",\$n++)} @array1;

print Dumper \@array2;
Last edited by keath; June 5th, 2013 at 09:10 AM. Reason: clarified the qualification
9. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Jun 2012
Location
Paris area, France
Posts
846
Rep Power
496
Originally Posted by zeeshanaayan07