Home AMX User Forum AMX General Discussion

Value from a single byte

Maybe i'm overlooking something, but i searched the Netlinx helpfiles and searched this forum, but couldn't find the answer for my 'problem'

From a file i parse a line and in this line is one! character representing an index, this character can be 0-9 and A-Z for calculating i need the value of this character pa. 0=48/ 6=54/A=65/P=80 etc etc
For the numbers i could use ATOI, but for the letters this returns a 0

I can remember from my old ZX81 days that there was something like ASC$(character) even that other brand with the C has a command called byte which returns the value from one byte.
For now i made a stupid lookup table with 36 entrys which do the job, but i'm pretty sure that there must be a more elegant way to do this.

something like this:
INTEGER Byte_Value
CHAR My_Byte[1]

My_Byte = "'A'"
Byte_Value = some_command(My_Byte)

Byte_Value is now 65

Kind Regards

Mark

Comments

  • RogerSRogerS Posts: 9
    You can just use the integer value directly from the char array:


    char my_byte[] = 'A'
    integer my_byteAsDec = 0


    my_byteAsDec = my_byte[1]

    This will give the my_byteAsDec the value of 65

    Tested with both numbers and letters


    Is this what you were looking for?
  • AvexAvex Posts: 4
    Thanks for the reply, but when i do that i get a compile warning...

    WARNING: D:\Jingle_PC_control.axi(401): C10570: Converting a string to a [INTEGER]
    D:\Studio_Program.axs - 0 error(s), 1 warning(s)

    If it works it should be OK, but can it be done without the warning?

    Grtzz Mark
  • RogerSRogerS Posts: 9
    If you try to convert the whole string to integer you get the warning.


    I'm not getting the warning when i convert only 1 char in the string:



    my_byteAsDec = my_byte[1] //this is ok. This will only convert the 1 char to integer

    my_byteAsDec = my_byte //this will give warning. this tries to convert the whole array.
  • AvexAvex Posts: 4
    I should read better :-)

    The [1] behind the byte is what i overlooked, i already did something like :
    character = MID_STRING(cLine_Read,24,1) so i presumed this one byte was enough...

    Thanks for the answer, it cleared a lot!

    Grtzz Mark
  • DHawthorneDHawthorne Posts: 4,584
    The MID_STRING function returns a string,even if only of length 1. The direct array index only returns the array element at that location. There's a bit more overhead in storing a string, which is why the compiler won't let you do that.
  • AvexAvex Posts: 4
    @DHawthorne

    That explains my compiler 'nagging' :-) working with different systems with their small but big influence
    differences can sometimes be a real pain in the **s

    Many thanks for the answers.

    Grtzz Mark
  • nicholasjamesnicholasjames Posts: 154
    And don't forget about TYPE_CAST.

    Any of the 8 intrinsic data types can be assigned to any other type without the compiler yelling as long as you are not assigning a larger type to a smaller, or assigning a signed type to an unsigned type.
    INTEGER = CHAR; // Okay
    CHAR = INTEGER; // Not okay
    
    FLOAT = SINTEGER; // Okay
    LONG = SINTEGER; // Not Okay
    

    However, you can force NetLinx to ignore this by using TYPE_CAST.
    INTEGER = CHAR; // Okay
    CHAR = TYPE_CAST(INTEGER); // Okay Now
    
    FLOAT = SINTEGER; // Okay
    LONG = TYPE_CAST(SINTEGER); // Okay Now
    
  • DHawthorneDHawthorne Posts: 4,584
    Well, it still won't yell at you if it can be converted at all, but you might lose some data. In fact, I learned it as a a method of rounding numbers in my C++ days: add 0.5 to a float, type cast it to an integer, and you got your rounded number.
  • nicholasjamesnicholasjames Posts: 154
    DHawthorne wrote: »
    Well, it still won't yell at you if it can be converted at all

    I'm sorry, but what do you mean "if it can be converted at all"? Unless you're including run-time math. Dividing one integer by another may technically result in a floating point value, but the compiler won't catch it since the state of the variables are unknown.
  • DHawthorneDHawthorne Posts: 4,584
    I'm sorry, but what do you mean "if it can be converted at all"? Unless you're including run-time math. Dividing one integer by another may technically result in a floating point value, but the compiler won't catch it since the state of the variables are unknown.

    I was just hedging my bets. It may never complain for all I know, but neither have I tried type-casting an elaborate structure to a char, for example.
Sign In or Register to comment.