# Constructing a string from hex value

**URL:** <https://www.ruby-forum.com/t/constructing-a-string-from-hex-value/67750>\
**Category:** Ruby\
**Created:** [September 6, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/constructing-a-string-from-hex-value/67750 "2006-09-06T16:06:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srdjan\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@Srdjan\_M](https://www.ruby-forum.com/u/Srdjan_M)\
**Post date:** [September 6, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/constructing-a-string-from-hex-value/67750/1 "2006-09-06T16:06:07Z")

</div>

hi,

How do I construct a string out of hex values?  
In other words I want to give exact hex value for each byte in the  
string. So I would have soemthing like

0x2a, 0x7e, 0x0a, 0x7e

If I go through Fixnum I get my string to contain numbers 48, 115, 10,  
115 but I do not want that but rather a string of ASCII(48),  
ACII(115),…

Thanks

srdjan

---

<div class="post-metadata">

**Author:** ![Srdjan\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@Srdjan\_M](https://www.ruby-forum.com/u/Srdjan_M)\
**Post date:** [September 6, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/constructing-a-string-from-hex-value/67750/2 "2006-09-06T16:30:08Z")

</div>

Srdjan M. wrote:

> hi,
> 
> How do I construct a string out of hex values?  
> In other words I want to give exact hex value for each byte in the  
> string. So I would have soemthing like
> 
> 0x2a, 0x7e, 0x0a, 0x7e
> 
> If I go through Fixnum I get my string to contain numbers 48, 115, 10,  
> 115 but I do not want that but rather a string of ASCII(48),  
> ACII(115),…
> 
> Thanks
> 
> srdjan

irb(main):001:0\> p = “\x2a\x7e\x0a\x7e”  
=\> “\*~\n~”  
irb(main):002:0\>

---

<div class="post-metadata">

**Author:** ![Srdjan\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@Srdjan\_M](https://www.ruby-forum.com/u/Srdjan_M)\
**Post date:** [September 6, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/constructing-a-string-from-hex-value/67750/3 "2006-09-06T16:32:12Z")

</div>

Srdjan M. wrote:

> 0x2a, 0x7e, 0x0a, 0x7e

[0x2a, 0x7e, 0x0a, 0x7e].pack ‘C\*’

---

<div class="post-metadata">

**Author:** ![Srdjan\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@Srdjan\_M](https://www.ruby-forum.com/u/Srdjan_M)\
**Post date:** [September 6, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/constructing-a-string-from-hex-value/67750/4 "2006-09-06T16:44:14Z")

</div>

“Srdjan M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> How do I construct a string out of hex values?  
> In other words I want to give exact hex value for each byte in the  
> string. So I would have soemthing like
> 
> 0x2a, 0x7e, 0x0a, 0x7e
> 
> If I go through Fixnum I get my string to contain numbers 48, 115, 10,  
> 115 but I do not want that but rather a string of ASCII(48),  
> ACII(115),…

irb(main):001:0\> a = [0x2a, 0x7e, 0x0a, 0x7e]  
=\> [42, 126, 10, 126]  
irb(main):002:0\> a.map{|x|x.chr}.join  
=\> “\*~\n~”

If all you have is the string representation of the hex values (say,  
if you’re reading hex values from a file), you have to do just a tiny  
bit more to first convert the hex values into integers:

irb(main):001:0\> a = %w[2a 7e 0a 7e]  
=\> [“2a”, “7e”, “0a”, “7e”]  
irb(main):002:0\> a.map{|x|x.hex.chr}.join  
=\> “\*~\n~”

But only just barely more.

---

<div class="post-metadata">

**Author:** ![Srdjan\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@Srdjan\_M](https://www.ruby-forum.com/u/Srdjan_M)\
**Post date:** [September 6, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/constructing-a-string-from-hex-value/67750/5 "2006-09-06T17:08:16Z")

</div>

thanks a lot for you help

---

<div class="post-metadata">

**Author:** ![Srdjan\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@Srdjan\_M](https://www.ruby-forum.com/u/Srdjan_M)\
**Post date:** [September 6, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/constructing-a-string-from-hex-value/67750/6 "2006-09-06T17:20:18Z")

</div>

On Wed, 6 Sep 2006, Srdjan M. wrote:

> ACII(115),…
> 
> Thanks
> 
> srdjan

the append operator of String does what you want with Fixnums. as  
others have  
pointed out you can use pack to initially contruct it. but you can also  
continue to build it up with a simple ‘\<\<’

harp:~ \> cat a.rb  
s = [0x2a, 0x7e, 0x0a, 0x7e].inject(’’){|s,c| s \<\< c}

p s[0]  
p 0x2a

harp:~ \> ruby a.rb  
42  
42

regards.

-a
