Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6212691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:33:48+00:00 2026-05-24T06:33:48+00:00

for example str = ‘f01288c2’ #a hexadecimal string bin = str.to_i(16).to_s(2).rjust(str.length + (64 –

  • 0

for example

str = 'f01288c2' #a hexadecimal string
bin = str.to_i(16).to_s(2).rjust(str.length + (64 - (str.length % 64)), '0')

so the size of the binary string is always the multiply of 64.
The problem here is, the str.length is the length before it’s converted into binary. I need the length of the string after to_s(2). How do I access the return value of the to_s(2)?

Update
I wonder if there is a one chain solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:33:48+00:00Added an answer on May 24, 2026 at 6:33 am

    The one-line solution would use Kernel#tap and str.replace:

    ruby > str = 'f01288c2' #a hexadecimal string
     => "f01288c2" 
    ruby > bin = str.to_i(16).to_s(2).rjust(str.length + (64 - (str.length % 64)), '0')
     => "0000000000000000000000000000000011110000000100101000100011000010" 
    ruby > bin = str.to_i(16).to_s(2).tap { |str| str.replace str.rjust(str.length + (64 - (str.length % 64)), '0') }
     => "11110000000100101000100011000010" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, here is a string representing an expression: var str = 'total =
I have a string as const char *str = Hello, this is an example
I'm looking to search a string for example: std::string str = _Data_End__Begin_Data_End; |_________| |_____________|
I have this string for example: str = my name is john#doe oh.yeh; the
is it possible to get selection from defined string? For example: str = <li>Some
So I would like to know what's inside the string for example: var str
Example: string str = I am going to reverse myself.; string strrev = I
I could only do this with String, for example: String str = ""; for
I have a string with xml content, for example: String str = <history><message from=Alice
For example I have some random string: str = 26723462345 And I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.