Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
May 21st, 2013, 01:56 AM
 bonny1989
Contributing User

Join Date: Apr 2013
Posts: 31
Time spent in forums: 3 h 49 m 38 sec
Reputation Power: 1

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
May 21st, 2013, 02:30 AM
 Laurent_R
Contributing User

Join Date: Jun 2012
Posts: 715
Time spent in forums: 6 Days 11 h 45 m 55 sec
Reputation Power: 474
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
May 21st, 2013, 04:19 AM
 Laurent_R
Contributing User

Join Date: Jun 2012
Posts: 715
Time spent in forums: 6 Days 11 h 45 m 55 sec
Reputation Power: 474
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
May 29th, 2013, 11:10 AM
 bonny1989
Contributing User

Join Date: Apr 2013
Posts: 31
Time spent in forums: 3 h 49 m 38 sec
Reputation Power: 1
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);

Quote:
 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
May 29th, 2013, 11:49 AM
 Laurent_R
Contributing User

Join Date: Jun 2012
Posts: 715
Time spent in forums: 6 Days 11 h 45 m 55 sec
Reputation Power: 474
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
June 5th, 2013, 07:09 AM
 zeeshanaayan07
Permanently Banned

Join Date: May 2013
Posts: 5
Time spent in forums: 1 h 14 m 30 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Hexdecimal

#7
June 5th, 2013, 07:32 AM
 MrFujin
Lord of the Dance

Join Date: Oct 2003
Posts: 3,353
Time spent in forums: 2 Months 2 Weeks 4 Days 20 h 14 m 4 sec
Reputation Power: 1828
Quote:

there are plenty of information/answers if you search for this on Google.

#8
June 5th, 2013, 09:01 AM
 keath
!~ /m\$/

Join Date: May 2004
Location: Reno, NV
Posts: 4,174
Time spent in forums: 2 Weeks 4 Days 15 h 14 m 15 sec
Reputation Power: 1809
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 10:10 AM. Reason: clarified the qualification

#9
June 5th, 2013, 10:36 AM
 Laurent_R
Contributing User

Join Date: Jun 2012
Posts: 715
Time spent in forums: 6 Days 11 h 45 m 55 sec
Reputation Power: 474
Quote:

It is a number represented in base 16 (instead of the base 10 that we use more commonly in our life). You need 16 digits (from 0 to 15) to represent them. The digits are usually: 0 1 2 3 4 5 6 7 8 9 A B C D E F.

The numbers 0 to 35 (in decimal) are represented as follows in hex: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21, 22 ...

 Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Addition of Hexadecimal numbers.