Module: Encoding

Included in:
String
Defined in:
lib/Win32/Encoding.rb

Overview


This mixin brings utf8 and ucs2 encoding to the class String

Constant Summary collapse

ANSI =

constsFor: “String Encoding IDs”

'ANSI'
UTF8 =
'UTF8'
UCS2 =
'UCS2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#encodingObject

Returns the value of attribute encoding.



15
16
17
# File 'lib/Win32/Encoding.rb', line 15

def encoding
  @encoding
end

Instance Method Details

#initialize(p_encoding = ANSI) ⇒ Object


class methodsFor: “initialization”



25
26
27
# File 'lib/Win32/Encoding.rb', line 25

def initialize(p_encoding = ANSI)
	@encoding = p_encoding
end

#to_hexObject



57
58
59
60
61
62
63
# File 'lib/Win32/Encoding.rb', line 57

def to_hex
	str = ''
	self.unpack('C*').each do |byte|
		str += " %02X" % byte
	end
	return str[1 .. -1]
end

#to_ucs2(p_bZeroTerminate = false) ⇒ Object


methodsFor: “encoding”



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/Win32/Encoding.rb', line 31

def to_ucs2(p_bZeroTerminate = false)
	return self if defined? @encoding and UCS2 == @encoding
	array_enc  = self.unpack('U*')
	array_ucs2 = []
	array_enc.each do |byte|
		array_ucs2 << (byte & 0xFF) << (byte >> 8)
	end
	strRet  = array_ucs2.pack('C*')
	strRet += "\000\000" if p_bZeroTerminate
	strRet.encoding = UCS2
	return strRet
end

#to_utf8(p_bZeroTerminate = false) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/Win32/Encoding.rb', line 44

def to_utf8(p_bZeroTerminate = false)
	return self if defined? @encoding and (UTF8 == @encoding or ANSI == @encoding)
	array_enc  = self.unpack('C*')
	array_utf8 = []
	0.step(array_enc.size-1, 2) do |i| 
		array_utf8 << (array_enc.at(i) + array_enc.at(i+1) * 0x100)
	end
	strRet  = array_utf8.pack('U*')
	strRet += "\000" if p_bZeroTerminate
	strRet.encoding = UTF8
	return strRet
end