# How to scan a hex string?

**URL:** <https://www.ruby-forum.com/t/how-to-scan-a-hex-string/238597>\
**Category:** Ruby\
**Created:** [January 19, 2014, 8:51am UTC](https://www.ruby-forum.com/t/how-to-scan-a-hex-string/238597 "2014-01-19T08:51:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [January 19, 2014, 8:51am UTC](https://www.ruby-forum.com/t/how-to-scan-a-hex-string/238597/1 "2014-01-19T08:51:32Z")

</div>

" sss\1\2abc\xAA sss ".scan(/\1\2(.\*?)\xAA/)[1]

[11] pry(main)\> s= " sss\1\2abc\xAAsss "  
=\> “\u0001\u0002abc\xAA”  
[12] pry(main)\> s.scan(/\001\002(._?)\xAA/)  
SyntaxError: (eval):2: invalid multibyte escape: /\001\002(._?)\xAA/

how to scan that abc string ?

it is a hex string in a device’s serial communication .

---

<div class="post-metadata">

**Author:** ![sevk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sevk](https://www.ruby-forum.com/u/sevk)\
**Post date:** [January 19, 2014, 12:39pm UTC](https://www.ruby-forum.com/t/how-to-scan-a-hex-string/238597/2 "2014-01-19T12:39:13Z")

</div>

It if is a hex string, then it is not a string, you are just looking at  
it as a string 🙂 You ‘might’ handle it as a string, but in this case  
you mix Unicode and non-Unicode characters in it (if we try to treat  
each byte or multi-byte as a character).

So even if I force the encoding of UTF-8, it will fail (\xaa is not a  
valid Unicode character)

> " sss\1\2abc\xAA sss ".force\_encoding(Encoding::UTF\_8).valid\_encoding?  
> =\> false

If you don’t need to recognize multi-byte characters then deep dive into  
the byte representation and search there:

> " sss\1\2abc\xAA sss ".unpack(“H\*”)  
> =\> [“207373730102616263aa2073737320”]

> " sss\1\2abc\xAA sss ".unpack(“H\*”)[0].scan(/0102(.\*)aa/)  
> =\> [[“616263”]]

You can turn the result easily to character string:

> “616263”.scan(/../).map{|x| x.to\_i(16)}.pack(“c\*”)  
> =\> “abc”

---

<div class="post-metadata">

**Author:** ![sevk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sevk](https://www.ruby-forum.com/u/sevk)\
**Post date:** [January 20, 2014, 6:08am UTC](https://www.ruby-forum.com/t/how-to-scan-a-hex-string/238597/3 "2014-01-20T06:08:52Z")

</div>

yes , it is char\* , not string .

thank you .

I’ll try unpack(“H\*”) and map.pack(“c\*”) .

---

<div class="post-metadata">

**Author:** ![sevk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sevk](https://www.ruby-forum.com/u/sevk)\
**Post date:** [February 17, 2014, 12:57pm UTC](https://www.ruby-forum.com/t/how-to-scan-a-hex-string/238597/4 "2014-02-17T12:57:45Z")

</div>

Földes László wrote in post #1133627:

> > " sss\1\2abc\xAA sss ".force\_encoding(Encoding::UTF\_8).valid\_encoding?  
> > =\> false
> 
> If you don’t need to recognize multi-byte characters then deep dive into  
> the byte representation and search there:
> 
> > " sss\1\2abc\xAA sss ".unpack(“H\*”)  
> > =\> [“207373730102616263aa2073737320”]

one question from out of curiosity - How did you got to know, by looking  
at the final Array or string, that “616263” is actually “abc” ?

---

<div class="post-metadata">

**Author:** ![sevk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sevk](https://www.ruby-forum.com/u/sevk)\
**Post date:** [February 17, 2014, 1:03pm UTC](https://www.ruby-forum.com/t/how-to-scan-a-hex-string/238597/5 "2014-02-17T13:03:35Z")

</div>

Arup R. wrote in post #1136919:

> one question from out of curiosity - How did you got to know, by looking  
> at the final Array or string, that “616263” is actually “abc” ?

Practice. After a few years of hexdumping and byte scanning, values like  
31,32,33 and 41,42,43 and 61,62,63 start to leap out at you. You also  
get used to scanning for 20 and 0A (or 0D0A).

---

<div class="post-metadata">

**Author:** ![sevk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sevk](https://www.ruby-forum.com/u/sevk)\
**Post date:** [January 21, 2014, 3:10am UTC](https://www.ruby-forum.com/t/how-to-scan-a-hex-string/238597/6 "2014-01-21T03:10:32Z")

</div>

thanks. I found a better way :

s = " sss\1\2abcd\xAA sss "  
s.force\_encoding(‘ASCII-8BIT’)

p s.scan(Regexp.new ‘\001\002\004\xAA’, nil , ‘n’)[0]

---

<div class="post-metadata">

**Author:** ![sevk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sevk](https://www.ruby-forum.com/u/sevk)\
**Post date:** [March 26, 2014, 8:28am UTC](https://www.ruby-forum.com/t/how-to-scan-a-hex-string/238597/7 "2014-03-26T08:28:06Z")

</div>

p s.scan(Regexp.new ‘\001\002\004\xAA’, nil , ‘n’)[0]  
p s.scan(Regexp.new ‘\001\002\004\xAA’, ‘m’ , ‘n’)[0]

Mutileline is better if contain 0D 0A

---

<div class="post-metadata">

**Author:** ![sevk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sevk](https://www.ruby-forum.com/u/sevk)\
**Post date:** [March 26, 2014, 10:38am UTC](https://www.ruby-forum.com/t/how-to-scan-a-hex-string/238597/8 "2014-03-26T10:38:58Z")

</div>

Matthew K. wrote in post #1136920:

> Arup R. wrote in post #1136919:
> 
> > one question from out of curiosity - How did you got to know, by looking  
> > at the final Array or string, that “616263” is actually “abc” ?
> 
> Practice. After a few years of hexdumping and byte scanning, values like  
> 31,32,33 and 41,42,43 and 61,62,63 start to leap out at you. You also  
> get used to scanning for 20 and 0A (or 0D0A).

Exactly 🙂
