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

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2

    Addition of Hexadecimal numbers.


    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}
    answer should be = {4001,4002.4300,400a,400b}

    Please help me, if possible.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    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?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Hi Laurent_R
    Thanks for the reply.
    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);

    Can you please help me?


    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    Hi,

    please explain better what you have in your array.

    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.
  10. #6
  11. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Hexdecimal


    Explain please what is Hexadecimal number
  12. #7
  13. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,647
    Rep Power
    1945
    Originally Posted by zeeshanaayan07
    Explain please what is Hexadecimal number
    there are plenty of information/answers if you search for this on Google.
  14. #8
  15. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,259
    Rep Power
    1810
    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
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    Originally Posted by zeeshanaayan07
    Explain please what is Hexadecimal number
    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 ...

IMN logo majestic logo threadwatch logo seochat tools logo