0

I need to do something like this, in ruby:

    private byte[] ResourceHashToByteArray(string hex)
    {
        int NumberChars = hash.Length;
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2) bytes[i / 2] = Convert.ToByte(hash.Substring(i, 2), 16);
        return bytes;
    }

What would be the optimum/most common/best practice way of doing this in Ruby?

I have tried the following methods to no avail:

Digest::MD5.digest(hex)
String.unpack(hex)
String.bytes
10
  • 2
    If you don't know the answer, just move on. Don't down vote for no reason ;-). Commented Jan 14, 2016 at 23:28
  • 2
    Just because someone didn't comment, that doesn't mean they don't have a reason for down-voting your question. I will point out that, assuming Ruby has a byte array type at all, creating one in that language should be very straightforward. That you have no Ruby code at all in your question (which is really not a C# question at all and so should not have the c# tag) strongly suggests you failed to do any research on the question yourself. Commented Jan 14, 2016 at 23:53
  • 1
    Stack Overflow isn't a "port my code for me" site; it's a place where people who have made an honest effort to solve their own problem and still are having trouble can get help. If you feel you fall into that category, then fix your question by providing a good minimal reproducible example that shows what you've tried, with a clear explanation of what specifically you're having trouble with. Don't just ask people to write your code for you. Commented Jan 14, 2016 at 23:53
  • Also, what do you mean by "optimum"? That's the same as saying "best"--it really doesn't tell us much. Commented Jan 14, 2016 at 23:55
  • 1
    @PeterDuniho Thank you for your passionate reply and you make a valid point. However, just downvoting a post without mentioning the slightest clue as to why does not help me nor this community any further. It only leads to more questions. You are correct in that I should have provided my Ruby code. I will update my question. I am glad my comment got this conversation started :-). Commented Jan 15, 2016 at 8:27

1 Answer 1

4

This is literally similar, but if you're using it for, say, binary file writing, not that useful:

'deadbeef'.scan(/../).map(&:hex)
[222, 173, 190, 239]

This is similar in spirit, since in Ruby strings in Encoding:ASCII-8BIT are what you'd normally use for a byte array:

'deadbeef'.scan(/../).map(&:hex).pack('C*')
"\xDE\xAD\xBE\xEF"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I will give this a go later today.
This works. Thank you very much! Learned some new Ruby stuff today :-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.