# Ruby equivalent of Integer.toHexstring(int) method in java

**URL:** <https://www.ruby-forum.com/t/ruby-equivalent-of-integer-tohexstring-int-method-in-java/148209>\
**Category:** Ruby\
**Created:** [September 10, 2008, 2:32pm UTC](https://www.ruby-forum.com/t/ruby-equivalent-of-integer-tohexstring-int-method-in-java/148209 "2008-09-10T14:32:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anil\_kandimalla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@anil\_kandimalla](https://www.ruby-forum.com/u/anil_kandimalla)\
**Post date:** [September 10, 2008, 2:32pm UTC](https://www.ruby-forum.com/t/ruby-equivalent-of-integer-tohexstring-int-method-in-java/148209/1 "2008-09-10T14:32:19Z")

</div>

Hi all  
Hi guys i am new to ruby,i am searching for a ruby method which  
gives the same output as given by Integer.toHexstring(int),but unable  
to find any methods.

Is there is a ruby method or can i call this java method from a ruby  
file and passing the parameter and get the output from this java  
method.

Any help is greatly appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![anil\_kandimalla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@anil\_kandimalla](https://www.ruby-forum.com/u/anil_kandimalla)\
**Post date:** [September 10, 2008, 2:51pm UTC](https://www.ruby-forum.com/t/ruby-equivalent-of-integer-tohexstring-int-method-in-java/148209/2 "2008-09-10T14:51:07Z")

</div>

irb(main):005:0\> “%x” % 45  
=\> “2d”

irb(main):006:0\> “%X” % 45  
=\> “2D”

irb(main):007:0\> “%4X” % 45  
=\> “Â 2D”

irb(main):008:0\> “%04X” % 45  
=\> “002D”

---

<div class="post-metadata">

**Author:** ![anil\_kandimalla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@anil\_kandimalla](https://www.ruby-forum.com/u/anil_kandimalla)\
**Post date:** [September 10, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/ruby-equivalent-of-integer-tohexstring-int-method-in-java/148209/3 "2008-09-10T15:01:28Z")

</div>

anil kandimalla wrote:

> Hi all  
> Hi guys i am new to ruby,i am searching for a ruby method which  
> gives the same output as given by Integer.toHexstring(int),but unable  
> to find any methods.
> 
> Is there is a ruby method or can i call this java method from a ruby  
> file and passing the parameter and get the output from this java  
> method.
> 
> Any help is greatly appreciated.
> 
> Thanks

if you don’t like the ruby way , you could call that method directly  
from jruby 🙂

---

<div class="post-metadata">

**Author:** ![anil\_kandimalla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@anil\_kandimalla](https://www.ruby-forum.com/u/anil_kandimalla)\
**Post date:** [September 10, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/ruby-equivalent-of-integer-tohexstring-int-method-in-java/148209/4 "2008-09-10T15:22:51Z")

</div>

2008/9/10 anil kandimalla [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> Hi guys i am new to ruby,i am searching for a ruby method which
> 
> ```
> 
> gives the same output as given by Integer.toHexstring(int),but unable  
> to find any methods.
> 
> Is there is a ruby method or can i call this java method from a ruby  
> file and passing the parameter and get the output from this java  
> method.

irb(main):001:0\> 123.to\_s 16  
=\> “7b”  
irb(main):003:0\> “%x” % 123  
=\> “7b”  
irb(main):004:0\> sprintf “%x”, 123  
=\> “7b”  
irb(main):005:0\> printf “%x\n”, 123  
7b  
=\> nil

Cheers

robert
