Need help trying to store a value into byte array buffer — Parallax Forums

# Need help trying to store a value into byte array buffer

edited 2021-03-03 01:47

I know I've been down this road before but I can't remember how it's supposed to work.

Var

byte Buffer

Pub Main | a, b, c

a := 400
b := 5
c := a / b ' Which is 80 decimal

Buffer := c ' Want to store the hex value of c here

I want to store the HEX value of c which should be 50 (\$50 ?) in the Buffer at location 4 but I can't seem to get it through my head how to do it.

What are these variables such as a := 400? Is 400 a string, integer or decimal number? How do you convert it to a HEX value to be stored in my byte array Buffer?

Using the terminal if I send c using terminal.hex(c, 2) it will print 50. If I use terminal.dec(c) it will print 80.
If I manually use Buffer := \$50 that works. I can read the Buffer and it shows 50

I know this should be such basic stuff but I am befuddled with this.

BTW I tried using [code] [/code] and it didn't format. I thought there used to be a Code button to insert code with but I don't see it.

Thanks. I feel stupid...

## Comments

• Are you asking how to store 16-bit value in a byte variable?

• No. It's an 8 bit value. \$50 hex is less than \$FF which is 1 byte. As I mentioned if I do this Buffer := \$50 and then read that location from the Buffer it shows 50.

• edited 2021-03-03 03:03

400 is a number written as a decimal value. \$50 is a number written as a hexadecimal value. "20" is a two character (two byte) string containing the digits "2" and "0". Their numeric values (as ASCII characters) are 50 and 48 respectively (written as decimal values).

There are many routines that can take a numeric value and convert it to a character string representing the numeric value as a hexadecimal value (if that's what you want).

From FULLDUPLEXSERIAL.SPIN

PUB hex(value, digits)
'' Print a hexadecimal number
value <<= (8 - digits) << 2
repeat digits
tx(lookupz((value <-= 4) & \$F : "0".."9", "A".."F"))

This calls tx() for each hexadecimal digit in the supplied value (specified by digits). You can substitute Buffer[x++] for tx() where x is the starting index into Buffer where you want the string to begin. Note: tx( needs to be indented

• edited 2021-03-03 03:02

So using this would convert the decimal value (80) to a hex value (\$50) to be stored at Buffer making the changes you described?

• So I tried Mike's suggestion and it stores the ASCII decimal equivalent. It places a 35 (which is the first number of 50) into Buffer and places a 30 (which is the second number of 50) into Buffer. So that is not what I was looking for.

Maybe I wasn't clear enough. I am wanting to place the result of this equation 400 / 5 which equals 80 (decimal) but place the hex equivalent of 80 which is 50 into 1 Buffer slot which is Buffer. I want to store it as a hex number not a string in 1 location only.

Any help appreciated.

• Ok so I figured it out.

a := 400
b := 5
c := a / b ' Which is 80 decimal

I added this:

c := c /16
c := c << 4

Buffer := c

Now when I look at Buffer it shows a 50.

• I also think I'll need to use // division to get any remainders until they are 0 then OR them together to create my HEX number.

• I think you are confusing how a number is stored versus how it is displayed when you print it. The expression "Buffer := 80" is equivalent to "Buffer := \$50". Also, "Buffer := 400/5" will result in the same value. Physically, it is stored in RAM as 8 individual bits with a binary value of %01010000.

This value can be visualized in different ways. As you stated, terminal.hex() will print out 50, and terminal.dec() will print 80. If you used terminal.tx() the value would be interpreted as an ASCII character, and you would get the letter "P".

• I want to store the HEX value of c which should be 50 (\$50 ?) in the Buffer at location 4 but I can't seem to get it through my head how to do it.

Everything is stored in binary. If you want to see the value in hex, use a hex output modifier when looking at it. Examples

```  term.dec(x)
term.bin(x)
term.hex(x)
```
• edited 2021-03-03 18:41

There's no such thing as a "hex value", "decimal value", or "binary value." A value is a value, period. The Propeller's internal representation of a value is binary. A value can be displayed as hexadecimal, octal, decimal, binary, etc. But the value itself is just a number, completely divorced from its representation. So the value stored in memory for 80 or \$50 is exactly the same number.

-Phil

• @"Phil Pilgrim (PhiPi)" said:
There's no such thing as a "hex value", "decimal value", or "binary value." A value is a value, period. The Propeller's internal representation of a value is binary. A value can be displayed as hexadecimal, octal, decimal, binary, etc. But the value itself is just a number, completely divorced from its representation. So the value stored in memory for 80 or \$50 is exactly the same number.

-Phil

@"Dave Hein" said:
I think you are confusing how a number is stored versus how it is displayed when you print it. The expression "Buffer := 80" is equivalent to "Buffer := \$50". Also, "Buffer := 400/5" will result in the same value. Physically, it is stored in RAM as 8 individual bits with a binary value of %01010000.

This value can be visualized in different ways. As you stated, terminal.hex() will print out 50, and terminal.dec() will print 80. If you used terminal.tx() the value would be interpreted as an ASCII character, and you would get the letter "P".

Thanks guys. I have it in my head now. I don't know what was going on last night. Got worked up over nothing. It works as stated.

Sign In or Register to comment.