# \[Backport #809\] String#ord and String#\[gs\]etbyte for 1.8

**URL:** <https://www.ruby-forum.com/t/backport-809-string-ord-and-string-gs-etbyte-for-1-8/154640>\
**Category:** Development\
**Created:** [December 2, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/backport-809-string-ord-and-string-gs-etbyte-for-1-8/154640 "2008-12-02T14:45:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shinichiro\_Hamaji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Shinichiro\_Hamaji](https://www.ruby-forum.com/u/Shinichiro_Hamaji)\
**Post date:** [December 2, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/backport-809-string-ord-and-string-gs-etbyte-for-1-8/154640/1 "2008-12-02T14:45:21Z")

</div>

Backport #809: String#ord and String#[gs]etbyte for 1.8  
[http://redmine.ruby-lang.org/issues/show/809](http://redmine.ruby-lang.org/issues/show/809)

e$B5/I\<\<Te(B: Shinichiro Hamaji  
e$B%9%F!\<%?%9e(B: Open, e$BM%@hEYe(B: Normal

Ruby1.9 e$B$G%3!\<%I$r=q$$$F$$$k;~$K!"e(B 1.8  
e$B$G$bF0$/$h$&$K$7$H$/$+$J$!!De(B  
e$B$H;W$&;~$K:G$b:$$k$N$,8D?ME\*$K$Oe(B String#[]  
e$B$N5sF0$NJQ2=$J$N$G$9$,!"e(B  
1.9 e$B$Ne(B ord, getbyte, setbyte e$B$re(B backport  
e$B$7$F$\*$/$o$1$K$O$$$1$J$$$b$s$G$7$g$&$+!#e(B

# Index: string.c

— string.c (revision 20448)  
+++ string.c (working copy)  
@@ -2473,6 +2473,51 @@

/\*

- call-seq:

- 
  - 

```
str.getbyte(index) => 0 .. 255

```

- 
  - 

- 
  - returns the _index_th byte as an integer.

- \*/  
+static VALUE  
+rb\_str\_getbyte(VALUE str, VALUE index)  
+{
- long pos = NUM2LONG(index);
- 
- if (pos \< 0)
- pos += RSTRING\_LEN(str);
- if (pos \< 0 || RSTRING\_LEN(str) \<= pos)
- return Qnil;
- 
- return INT2FIX((unsigned char)RSTRING\_PTR(str)[pos]);  
+}
- 
- 

+/\*

- 
  - call-seq:

- 
  - 

```
str.setbyte(index, int) => int

```

- 
  - 

- 
  - modifies the _index_th byte as _int_.

- \*/  
+static VALUE  
+rb\_str\_setbyte(VALUE str, VALUE index, VALUE value)  
+{
- long pos = NUM2LONG(index);
- int byte = NUM2INT(value);
- 
- rb\_str\_modify(str);
- 
- if (pos \< -RSTRING\_LEN(str) || RSTRING\_LEN(str) \<= pos)
- rb\_raise(rb\_eIndexError, “index %ld out of string”, pos);
- if (pos \< 0)
- pos += RSTRING\_LEN(str);
- 
- RSTRING\_PTR(str)[pos] = byte;
- 
- return value;  
+}
- 
- 

+/\*

- 
  - call-seq:
  - 

```
str.reverse => new_str

```

  - 
  - Returns a new string with the characters from _str_ in reverse  
order.  
@@ -4556,6 +4601,24 @@

/\*

- call-seq:

- 
  - 

```
str.ord => integer

```

- 
  - 

- 
  - Return the `Integer` ordinal of a one-character string.

- 
  - 

- 
  - 

```
"a".ord #=> 97

```

- \*/
- 

+VALUE  
+rb\_str\_ord(VALUE s)  
+{

- if (RSTRING\_LEN(s) \<= 0)
- rb\_raise(rb\_eArgError, “empty string”);
- return INT2FIX(\*RSTRING\_PTR(s) & 0xff);  
+}
- 
- 

+/\*

- 
  - call-seq:
  - 

```
str.sum(n=16) => integer

```

  - 
  - Returns a basic _n_-bit checksum of the characters in  
_str_,  
@@ -4959,6 +5022,8 @@  
rb\_define\_method(rb\_cString, “index”, rb\_str\_index\_m, -1);  
rb\_define\_method(rb\_cString, “rindex”, rb\_str\_rindex\_m, -1);  
rb\_define\_method(rb\_cString, “replace”, rb\_str\_replace, 1);

- rb\_define\_method(rb\_cString, “getbyte”, rb\_str\_getbyte, 1);

- rb\_define\_method(rb\_cString, “setbyte”, rb\_str\_setbyte, 2);

- rb\_define\_method(rb\_cString, “ord”, rb\_str\_ord, 0);

- 
- def test\_getbyte

- assert\_equal(0x82, “\xE3\x81\x82\xE3\x81\x84”.getbyte(2))

- assert\_equal(0x82, “\xE3\x81\x82\xE3\x81\x84”.getbyte(-4))

- assert\_nil(“\xE3\x81\x82\xE3\x81\x84”.getbyte(100))

- end

- 
- def test\_setbyte

- s = “\xE3\x81\x82\xE3\x81\x84”

- s.setbyte(2, 0x84)

- assert\_equal(“\xE3\x81\x84\xE3\x81\x84”, s)

- 
- s = “\xE3\x81\x82\xE3\x81\x84”

- assert\_raise(IndexError) { s.setbyte(100, 0) }

- 
- s = “\xE3\x81\x82\xE3\x81\x84”

- s.setbyte(-4, 0x84)

- assert\_equal(“\xE3\x81\x84\xE3\x81\x84”, s)

- end

- 
- def test\_ord

- assert\_equal(0xe3, “\xE3\x81\x82\xE3\x81\x84”.ord)

- assert\_raise(ArgumentError) { “”.ord }

- end  
end

---

<div class="post-metadata">

**Author:** ![Shinichiro\_Hamaji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Shinichiro\_Hamaji](https://www.ruby-forum.com/u/Shinichiro_Hamaji)\
**Post date:** [December 3, 2008, 5:46am UTC](https://www.ruby-forum.com/t/backport-809-string-ord-and-string-gs-etbyte-for-1-8/154640/2 "2008-12-03T05:46:05Z")

</div>

e$B%A%1%C%He(B #809 e$B$,99?7$5$l$^$7$?!#e(B (by Akinori MUSHA)

e$BC4Ev\<Te(B Akinori MUSHAe$B$K%;%C%He(B

e$B$"$j$,$H$&$4$6$$$^$9!#$3$l$i$be(BNEWSe$B5-:\O3$l$G$9$M!D!#e(B

String#getbyte/setbytee$B$O$3$N$^$^F~$l$h$&$H;W$$$^$9!#e(B

## String#orde$B$Oe(BInteger#orde$B$N$H$-$K5$$E$/$Y$-$G$7$?!#e(B e$B$3$A$i$Oe(Bmultibytee$BBP1~!Je(B$KCODE awaree$B!K$G$“$k$Y$-$@$H;W$&$N$G!”$=$N$h$&$KD\>$7$FF~$l$^$9!#e(B

[http://redmine.ruby-lang.org/issues/show/809](http://redmine.ruby-lang.org/issues/show/809)

---

<div class="post-metadata">

**Author:** ![Shinichiro\_Hamaji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Shinichiro\_Hamaji](https://www.ruby-forum.com/u/Shinichiro_Hamaji)\
**Post date:** [December 3, 2008, 8:27am UTC](https://www.ruby-forum.com/t/backport-809-string-ord-and-string-gs-etbyte-for-1-8/154640/3 "2008-12-03T08:27:35Z")

</div>

e$B%A%1%C%He(B #809 e$B$,99?7$5$l$^$7$?!#e(B (by Akinori MUSHA)

e$B%9%F!\<%?%9e(B Opene$B$+$ie(BClosede$B$KJQ99e(B  
e$B?JD=e(B % 0e$B$+$ie(B100e$B$KJQ99e(B

## Applied in changeset r20461.

[http://redmine.ruby-lang.org/issues/show/809](http://redmine.ruby-lang.org/issues/show/809)

---

<div class="post-metadata">

**Author:** ![Shinichiro\_Hamaji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Shinichiro\_Hamaji](https://www.ruby-forum.com/u/Shinichiro_Hamaji)\
**Post date:** [December 3, 2008, 10:04am UTC](https://www.ruby-forum.com/t/backport-809-string-ord-and-string-gs-etbyte-for-1-8/154640/4 "2008-12-03T10:04:16Z")

</div>

e$B%A%1%C%He(B #809 e$B$,99?7$5$l$^$7$?!#e(B (by Akinori MUSHA)

## getbyte/setbytee$B$O$[$$=$N$^$^F~$l$^$7$?!#e(B orde$B$Oe(B$KCODEe$BBP1~$K$7$FF~$l$^$7$?!#e(B

[http://redmine.ruby-lang.org/issues/show/809](http://redmine.ruby-lang.org/issues/show/809)

---

<div class="post-metadata">

**Author:** ![Shinichiro\_Hamaji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@Shinichiro\_Hamaji](https://www.ruby-forum.com/u/Shinichiro_Hamaji)\
**Post date:** [December 3, 2008, 4:47pm UTC](https://www.ruby-forum.com/t/backport-809-string-ord-and-string-gs-etbyte-for-1-8/154640/5 "2008-12-03T16:47:22Z")

</div>

e$BIMCO$G$9!#e(B

> getbyte/setbytee$B$O$[$$=$N$^$^F~$l$^$7$?!#e(B  
> orde$B$Oe(B$KCODEe$BBP1~$K$7$FF~$l$^$7$?!#e(B

e$B$"$j$,$H$&$4$6$$$^$9!#e(B $KCODE e$B$H$+B8:\_$rK:$l$5$C$F$$$^$7$?!De(B
