8bits signed integer

Hello guys,

I need little help. It’s any way to declare 8bits signed integer in
ruby?

For expamle:

test =
“\x75\x63\x32\x21\x01\xA9\x94\x94\x94\x94\x94\x94\x94\x94\x94\x0a\x85”

result = 0

for i in 0…test.length - 1
result += (test[i].unpack(‘c’)[0])
end

puts result.to_s(16)

I need result in HEX. Yeah, I know result in example will be wrong,
because correct result is 0x98

Thanks!

different result for me in case of 8 bit two’s complement

test =
“\x75\x63\x32\x21\x01\xA9\x94\x94\x94\x94\x94\x94\x94\x94\x94\x0a\x85”

result = 0
for i in 0…test.length - 1
x = test[i].unpack(‘c’)[0]
result += x > 127 ? -256 + x : x
end

p result > 0 ? “0x#{result.to_s(16)}” : “-0x#{(result.to_s(16)[1…-1])}”

Joey Thompson wrote in post #1048415:

Hello guys,

I need little help. It’s any way to declare 8bits signed integer in
ruby?

Hi,

If you use Windows, there is a type “char” in RTensor
(http://tensor.heliohost.org/rtensor/api/Tsr/Tnsr.html).

Regards,

Bill

Joey Thompson wrote in post #1048511:

Giampiero Z. wrote in post #1048458:

different result for me in case of 8 bit two’s complement
Result for 8bit signed int. is from
0x00 - 0xFF (-128 to 127)

You can’t have it both ways.

You either want your answer signed or unsigned. Pick one and stick with
it. Also,
accept that your example will under/overflow 8 bits.

Adam Ms. wrote in post #1048554:

Joey Thompson wrote in post #1048511:

Giampiero Z. wrote in post #1048458:

different result for me in case of 8 bit two’s complement
Result for 8bit signed int. is from
0x00 - 0xFF (-128 to 127)

You can’t have it both ways.

You either want your answer signed or unsigned. Pick one and stick with
it. Also,
accept that your example will under/overflow 8 bits.

Maybe I wrote wrong.

result will be:
in dec: from -128 to 127
in hex: from 0x00 to 0xFF

I know about under/overflow 8 bits, I neeed it.

I need correct result in DEC (-128 to 127), convert to HEX is easy.

Giampiero Z. wrote in post #1048458:

different result for me in case of 8 bit two’s complement

test =
“\x75\x63\x32\x21\x01\xA9\x94\x94\x94\x94\x94\x94\x94\x94\x94\x0a\x85”

result = 0
for i in 0…test.length - 1
x = test[i].unpack(‘c’)[0]
result += x > 127 ? -256 + x : x
end

p result > 0 ? “0x#{result.to_s(16)}” : “-0x#{(result.to_s(16)[1…-1])}”

Thanks for answer, but something is wrong. I tested your sample and the
result is -0x368, so it’s wrong. Result for 8bit signed int. is from
0x00 - 0xFF (-128 to 127)

result = 0
test.each_byte do |x|
result += x > 127 ? -256 + x : x
result %= 256
end

p “0x#{result.to_s(16)}”

test =
“\x75\x63\x32\x21\x01\xA9\x94\x94\x94\x94\x94\x94\x94\x94\x94\x0a\x85”

result = 0
for i in 0…test.length - 1
x = test[i].unpack(‘c’)[0]
result += x > 127 ? -256 + x : x
result %= 256
end

p result > 0 ? “0x#{result.to_s(16)}” : “-0x#{(result.to_s(16)[1…-1])}”

result = 0
test.each_byte do |x|
result += x
result %= 256
end

p “0x#{result.to_s(16)}”

my first post was only a suggestion; it were to be elaborated a bit;
further improvements are possible, of course

Adam Ms. wrote in post #1048658:

Decimal:

-128, -127, …, -1, 0, 1, …, 126, 127

Hex:

0x80, 0x81, …, 0xff, 0x00, 0x01, …, 0x7e, 0x7f

I know about that, I only wrote this, because result must be 1byte

Joey Thompson wrote in post #1048592:

Adam Ms. wrote in post #1048554:
Maybe I wrote wrong.

result will be:
in dec: from -128 to 127
in hex: from 0x00 to 0xFF

Your ranges are not identical.

Start with binary and then follow through:

10000000, 10000001, …, 11111111, 00000000, 00000001, …, 01111110,
01111111

Decimal:

-128, -127, …, -1, 0, 1, …, 126, 127

Hex:

0x80, 0x81, …, 0xff, 0x00, 0x01, …, 0x7e, 0x7f

Giampiero Z. wrote in post #1048643:

result = 0
test.each_byte do |x|
result += x
result %= 256
end

p “0x#{result.to_s(16)}”

my first post was only a suggestion; it were to be elaborated a bit;
further improvements are possible, of course

Awesome! I think that is working ok.

hex_arr = []
0.upto(255) do |n|
unsigned_int = “#{n.to_s.rjust(3,” “)}”
signed_int = “#{(n > 127 ? -256 + n : n).to_s.rjust(4,” “)}”
hex_num = “0x#{(n.to_s(16)).rjust(2,“0”).upcase}”
binary_num = “#{(n.to_s(2)).rjust(8,“0”)}”
hex_arr << [unsigned_int, signed_int, hex_num, binary_num]
end
[128,129,255,0,1,126,127].each { |i| p hex_arr[i] }
p hex_arr.index {|x| x[1] == “-128”}

produces

[“128”, “-128”, “0x80”, “10000000”]
[“129”, “-127”, “0x81”, “10000001”]
[“255”, " -1", “0xFF”, “11111111”]
[" 0", " 0", “0x00”, “00000000”]
[" 1", " 1", “0x01”, “00000001”]
[“126”, " 126", “0x7E”, “01111110”]
[“127”, " 127", “0x7F”, “01111111”]
128

oops, I did not see your reply;

however, my previous last answer solved the problem, in that it gives
back the right value 0x98

oops, I did not see your reply;

however, my previous last answer solved the problem, in that it gives
back the right value 0x98

Thanks a lot!

Joey Thompson wrote in post #1048592:

I need correct result in DEC (-128 to 127), convert to HEX is easy.

Hi,

On Windows:

require ‘RTensor’
test =
“\x75\x63\x32\x21\x01\xA9\x94\x94\x94\x94\x94\x94\x94\x94\x94\x0a\x85”
t = Tsr.t([1, test.length], :char)
(1…test.length).each{|i| t[i] = test[i-1].unpack(‘c’)[0]}
p t.sum[1]

=> -104

and

p (0x98 - 256 == t.sum[1])
=> true

Regards,

Bill

You only need to do the modulo-256 once at the end.

test =
“\x75\x63\x32\x21\x01\xA9\x94\x94\x94\x94\x94\x94\x94\x94\x94\x0a\x85”

puts (test.bytes.inject(:+) % 256).to_s(16)
#=> 98