Shop OBEX P1 Docs P2 Docs Learn Events
ASCII to Binary Conversions? — Parallax Forums

ASCII to Binary Conversions?

BenjBenj Posts: 66
edited 2008-12-17 20:08 in Propeller 1
I have a project that requires calculating a checksum. I need to take 1-12 ASCII characters, convert them to binary, then do some addition, xor, and a few other manipulations, and then turn the result into ASCII again. Is this possible with the Prop, and if so, where should I start looking for examples of ASCII to BIN conversion?

Benj

Comments

  • SkyKitSkyKit Posts: 9
    edited 2008-12-17 14:41
    Hope you get answers from the experts. I still am in the propeller's learning curve, but to convert ASCII to BIN you only need to substrate 30 Hex from the ASCII character and the result is your BIN.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-17 15:17
    ASCII is a character set representation. Do you mean that you have a series of decimal digits and you'd like to convert them into their binary equivalent?

    If "sum" is your binary value which is initialized to zero and "char" is the ASCII code for the digit, then you could write:

    sum := sum * 10 + char - "0"

    You would need to put this in a loop that would take the ASCII codes, left to right, one at a time and run them through this statement.
    Your code would also need to check to make sure the ASCII codes are for digits only and maybe stop at the first non-digit.

    The Extended FullDuplexSerial object in the Propeller Object Exchange does this sort of thing.
  • RiJoRiRiJoRi Posts: 157
    edited 2008-12-17 16:33
    ASCII is a convention applied to numbers, whether binary, octal, decimal, or hexadecimal format. That is, the ASCII code for 'A' is $41, 65, o'101, and b'01000001.
    The checksum method I've used here at work is

            LUC_CheckSum = 0;
         /* Do the checksum -- assumes a 0-terminated string */
        while(*RUCP_Ptr)
        begin
            LUC_CheckSum += *RUCP_Ptr++;
        endwhile
        
        LUC_CheckSum ^= 0xFF;       /* Need the 2's complement */
        LUC_CheckSum++;
    
            /* Convert Checksum to ASCII */
        *RUCP_Ptr++ = lfb_xtoa((LUC_CheckSum) >> 4);
        *RUCP_Ptr++ = lfb_xtoa(LUC_CheckSum);
    
        *RUCP_Ptr = NUL; /* For the puts() */
    
    



    byte lfb_xtoa(byte PUC_x)
    begin
        PUC_x &= 0x0F;    /* make sure it's a nibble! */
        PUC_x += (byte)'0';
        if(PUC_x > (byte)'9')
            PUC_x += (byte)7;
        return(PUC_x);
    end
    
    



    While this is in C-ish, you should be able to "spin" it. "xtoa" stands for "Convert a hex digit to ASCII".

    --Rich
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-12-17 19:47
    Are you certain that you need to do any conversion at all? Every ASCII character is simply an eight-bit binary number, stored the same way, and in the same kind of memory, as any other eight-bit binary number. Most programs that calculate checksums simply calculate them on the existing eight-bit bytes, not caring whether they're used as an ASCII characters or used some other way.

    There's nothing special, or arcane, about ASCII. The ASCII standard is simply an agreement among people: "Hey, guys, when we have a character to send, let's send it as an eight-bit number. See, we can use 00110000 for the printable character "3", and 00100000 for a blank, and like that." Much of the world has said, "Cool. We'll follow that standard."

    Much of the world, but not all. There are other codes, such as EBCDIC, that assign character meanings to eight-bit numbers in a different way.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
  • BenjBenj Posts: 66
    edited 2008-12-17 20:08
    Thank you for all of the replies. To calculate the checksum I need to:

    1) Perform the 8 bit sum of all the ASCII characters sent in the data field.
    2) Fold the resulting eight bit sum into six bits by XOR'ing the two MSB's of the sum with the two LSB's of the sum such that the new D1 is the old D1 XOR D7 and the new D0 is the old D0 XOR D6.
    3) The resulting lower six bits are then masked off, producing a code range of from 00 to 3F hex.
    4) This is then added to the ASCII code for '0' (30 hex), generating the final checksum character.

    I can do it by hand, but would like to do it with a Prop. So far I have been able to do step 1. Here is my code, using "H0" as the data:

    VAR
      word x
      word y
      
    OBJ  
    
      SimNum : "Simple_Numbers"
      text : "vga_text"
      
    PUB Start
    
      x := "H" + "0"
      y := SimNum.bin(x,8) 
      
      text.start(16)
      text.str(y)
    
    



    An example from the manual:

    The following is an example of a typical exchange:
    Host sends query to get back On-Board Module version information
    Flag Data Field Checksum Terminator
    ASCII [noparse][[/noparse]$] [noparse][[/noparse]@] [noparse][[/noparse]CR]
    Hex [noparse][[/noparse]24] [noparse][[/noparse]40] [noparse][[/noparse]31] [noparse][[/noparse]0D]
    Checksum of hex 40 data field:
    Bits 76543210

    Sum = 40 = 01000000
    Bits 7 and 6_
    aligned for_
    X0R 00000001
    X0R result 01000001
    Mask D5,D0 00000001
    Addend "0" 00110000
    Final Chk 00110001 Hex 31, ASCII "1"
Sign In or Register to comment.