Monday, November 24, 2008

Converting Bangla (Bengali) digits to ascii ones

ruby 1.8 doesn't support unicode. This creates problem when I need to parse Bangla digits in a text where each Bangla digit is represented as three bytes in utf-8.

I used a simple approach to satisfiy my need - 1. take the the last byte of each bangla digit, and 2. subtract by last byte of bangla digit '0'. Converting the whole number is even simpler. Here is my code -

  def self.translate_number_from_bangla(p_bn_number)
en_number = 0
length = p_bn_number.length
for i in 1..length/3 do
cur_digit = p_bn_number[3*i-1].to_i - 166
if cur_digit >= 0 && cur_digit <= 9
en_number *= 10
en_number += cur_digit
end
end
return en_number
end

1 comment:

priosathi said...

Thank you dada. It helps me.