iterate values of hex string

Discussion in 'Ruby' started by eggie5, Sep 30, 2012.

  1. eggie5

    eggie5 Guest

    Can somebody please suggest a getter way of doing this?

    hmac = "0000000000000000000000000000000000000000"
    #40 is length of hmac
    #255 is values of each byte to try
    (0..40).step(2) do |i|
    (0..255).each do |j|
    hmac[i..i+1]="%02x" % j
    p hmac
    end
    end


    It just iterates all the values of a 20 byte array as a string from left to right. e.g.:

    000000000000000000000000000000000000000
    0100000000000000000000000000000000000000
    0200000000000000000000000000000000000000
    ..
    ..
    ..
    FD00000000000000000000000000000000000000
    FE00000000000000000000000000000000000000
    FF00000000000000000000000000000000000000
    ..
    ..
    ..
    FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
     
    eggie5, Sep 30, 2012
    #1
    1. Advertising

  2. On 30.09.2012 08:49, eggie5 wrote:
    > Can somebody please suggest a getter way of doing this?


    Did you mean "better"?

    > hmac = "0000000000000000000000000000000000000000"
    > #40 is length of hmac
    > #255 is values of each byte to try
    > (0..40).step(2) do |i|
    > (0..255).each do |j|
    > hmac[i..i+1]="%02x" % j
    > p hmac
    > end
    > end
    >
    >
    > It just iterates all the values of a 20 byte array as a string from left to right. e.g.:
    >
    > 000000000000000000000000000000000000000
    > 0100000000000000000000000000000000000000
    > 0200000000000000000000000000000000000000
    > .
    > .
    > .
    > FD00000000000000000000000000000000000000
    > FE00000000000000000000000000000000000000
    > FF00000000000000000000000000000000000000
    > .
    > .
    > .
    > FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
    >


    (2**(20*8)).times {|x| printf "%040x\n", x}

    You get a different order though.

    What do you need that for?

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Sep 30, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gogo
    Replies:
    1
    Views:
    2,140
    Sudsy
    Sep 4, 2003
  2. runescience
    Replies:
    0
    Views:
    1,494
    runescience
    Feb 9, 2006
  3. Replies:
    10
    Views:
    6,394
    Neredbojias
    Aug 19, 2005
  4. Bengt Richter
    Replies:
    6
    Views:
    526
    Juha Autero
    Aug 19, 2003
  5. John
    Replies:
    4
    Views:
    956
    RedGrittyBrick
    Apr 1, 2008
Loading...

Share This Page