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

Need help trying to store a value into byte array buffer

Don MDon M Posts: 1,632
edited 2021-03-03 01:47 in Propeller 1

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

Var

byte Buffer[10]

Pub Main | a, b, c

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

Buffer[4] := 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[4] := $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

  • lardomlardom Posts: 1,659

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

  • Don MDon M Posts: 1,632

    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[4] := $50 and then read that location from the Buffer it shows 50.

  • Mike GreenMike Green Posts: 23,028
    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

  • Don MDon M Posts: 1,632
    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[4] making the changes you described?

  • Don MDon M Posts: 1,632

    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[4] and places a 30 (which is the second number of 50) into Buffer[5]. 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[4]. I want to store it as a hex number not a string in 1 location only.

    Any help appreciated.

  • Don MDon M Posts: 1,632

    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[4] := c

    Now when I look at Buffer[4] it shows a 50.

  • Don MDon M Posts: 1,632

    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[4] := 80" is equivalent to "Buffer[4] := $50". Also, "Buffer[4] := 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".

  • JonnyMacJonnyMac Posts: 7,590

    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)
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 22,982
    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

  • Don MDon M Posts: 1,632

    @"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[4] := 80" is equivalent to "Buffer[4] := $50". Also, "Buffer[4] := 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.