Announcement

Collapse
No announcement yet.

How to TCP SEND HEX?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to TCP SEND HEX?

    This has worked great for years:
    Code:
    TCP SEND FILE_NUMBER,  "this is the data to send"
    How do you send a bunch of HEX values?
    Code:
    TCP SEND FILE_NUMBER,  CHR$(VAL("&h41"))   'This sends the letter A
    I guess you have to loop through the individual HEX values and convert them to a CHR$ ??











  • #2
    Hi David, here are some alternatives.
    Use: MyBunchOfHEXValues = Bin2Hex("this is the data to send")

    1. The PB way
    Code:
    FUNCTION Bin2Hex(inData AS STRING) AS STRING
      LOCAL RetStr AS STRING, p AS BYTE PTR
      LOCAL a, b AS BYTE, i AS LONG
      p = STRPTR(inData)
      FOR i = 0 TO (LEN(inData)-1)
        a = @p[i]                                         'Will hold the first HEX char
        b = a                                             'Will hold the second HEX char
        SHIFT RIGHT a,4                                   'Make first HEX char
        b = b AND 15                                      'Make second one
        a = a + IIF(a < 10, 48, 55)                       'Make ascii 0-9 or A-F of them
        b = b + IIF(b < 10, 48, 55)
        RetStr += CHR$(a) + CHR$(b)                       'Using a @PTR, did not improve much,
      NEXT i                                              ' it just made the code more unclear
      FUNCTION = RetStr                                   ' then the one below.
    
    END FUNCTION
    
    '- Or this one
    
    FUNCTION Bin2Hex(inData AS STRING) AS STRING
       LOCAL RetStr AS STRING, i AS LONG, p AS BYTE PTR
       p = STRPTR(inData)
       FOR i = 0 TO (LEN(inData)-1)
          RetStr += HEX$(@p[i], 2)
       NEXT i
       FUNCTION = RetStr
    END FUNCTION
    2. With API call
    Code:
    FUNCTION Bin2Hex(sBin AS STRING) AS STRING  'Convert binary to uppercase hex string
       LOCAL nLength AS LONG, sHex AS STRING
       nLength = LEN(sBin) *2 + 1                         '+ 1 to accomodate a null terminator
       sHex    = STRING$(nLength, 0)
       CryptBinaryToString(BYVAL STRPTR(sBin), LEN(sBin), %CRYPT_STRING_HEXRAW + %CRYPT_STRING_NOCRLF, BYVAL STRPTR(sHex), nLength)
       FUNCTION = LEFT$(sHex, nLength) 
    END FUNCTION
    3. ASM alternative
    Code:
    FUNCTION Bin2Hex(BYVAL sBin AS STRING) AS STRING 
    '#REGISTER NONE
      LOCAL Buffer AS STRING, sBinLen AS LONG
      sBinLen = LEN(sBin)
      Buffer  = SPACE$(sBinLen * 2)
    
      !mov ecx,sBinLen      'Length of binary string to convert (sBin)
      !cmp ecx,0            'If sBin is empty (sBinLen = 0)
      !jz NOdata            'then end the function and return nothing.
      !mov esi,sBin         'Binary string to convert
      !mov edi,Buffer       'String buffer for the result (HEX code to return)
    
    LOOPsBin:
      !push ecx             'Save outer loop counter
      !mov ah,[esi]         'Binary to convert
      !mov dx,ax            'Copy of number to convert (AX will be used for calculation)
      !mov cx,&H0204        'ch=2 for loop counter and cl=4 for rotation of bits
    LOOPHex:
      !rol dx,cl            'Move next nibble (= hexa digit) in DX into b0-b3 position
      !mov al,dl            'Copy DX b0-b3 into AL
      !and al,&H0F          'Clear other bits (AL = value 0-15)
      !cmp al,&H0A          'If AL < 10, (digit 0-9)
      !jl Digit             ' Make a ascii digit
      !add al,&H37          'Else make a ascii HEX character (A-F)
      !jmp HexEnd
    Digit:
      !add al,&H30          'Make ascii digit (0-9)
    HexEnd:
      !mov [edi],al         'Write the HEX code into Buffer
      !inc edi              'Move Buffer string pointer to write next HEX code char
      !dec ch               'Decrement inner loop counter
      !jnz LOOPHex          '- Inner loop for two hex digits
    
      !inc esi              'Move binary string pointer to read next char to convert
      !pop ecx              'Get saved outer loop counter
      !loop LOOPsBin        '- Outer loop
      FUNCTION = Buffer
    NOdata:
    END FUNCTION
    /Mikael

    Comment


    • #3
      Thanks Mikael! Very cool.

      Comment

      Working...
      X