Announcement

Collapse
No announcement yet.

Extracting hex numeric from strings?

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

  • Extracting hex numeric from strings?

    I want to pull big-endian hex data out of a small wav file loaded into a string. The following works (needs to be expanded for more bytes), but just seems clumsy and indirect. Can anyone "pointer" me in a better direction?

    Code:
     TEMP4H = HEX$(ASC(MID$(TempStr$, 26, 1)))     'high byte sample rate
     TEMP4L = HEX$(ASC(MID$(TempStr$, 25, 1)))     'low byte sample rate
     TEMP5 = VAL("&H" & TEMP4H & TEMP4L)
    Thanks,
    Conrad

  • #2
    TEMP5& = CVL(TempStr$,25)
    or
    TEMP5??? = CVDWD(TempStr$, 25)

    Best regards,

    Bob Zale
    PowerBASIC Inc.

    Comment


    • #3
      You may also consider doing it as a WORD data type:
      Code:
      #COMPILE EXE
      #DIM ALL
      
      FUNCTION PBMAIN () AS LONG
          LOCAL TEMP4H,TEMP4L,tempStr AS STRING
          LOCAL temp5 AS WORD
          
          tempStr = "dfuitfguiti67y9o7yoigyiesdjuwj3we7373yuweshdehdehedeue"
      
          TEMP4H = HEX$(ASC(MID$(TempStr$, 26, 1)))     'high byte sample rate
          TEMP4L = HEX$(ASC(MID$(TempStr$, 25, 1)))     'low byte sample rate
          TEMP5 = VAL("&H" & TEMP4H & TEMP4L)
           ? STR$(TEMP5)
          TEMP5 = CVWRD(TempStr$, 25)
           ? STR$(TEMP5)
      END FUNCTION

      Comment


      • #4
        Thanks-much better! Sometimes I compare this forum to "pushing the easy button" in the office supply ads

        Comment


        • #5
          One more word Conrad,

          The next line is wrong,
          TEMP4L = HEX$(ASC(MID$(TempStr, 25, 1)))

          you need to have
          TEMP4L = HEX$(ASC(MID$(TempStr, 25, 1)), 2)

          becose, with the manipulation you do,
          this byte in hex must be represented by 2 characters, (0x00 to 0xFF)
          and you will need the leading zero if the byte value is under 0x10.
          For example: 0x01 0x02 got to become 0x0102 and not 0x0012.
          Last edited by Pierre Bellisle; 26 Mar 2008, 02:40 PM.

          Comment


          • #6
            Also,
            you may have a look at "DIM AT"
            who can make life easier and faster in this case...

            Code:
            [font=courier new][size=2]
             [color=blue]LOCAL[/color] TempStr   [color=blue]AS STRING[/color]
             [color=blue]LOCAL[/color] WordCount [color=blue]AS LONG[/color]
             [color=blue]LOCAL[/color] Looper    [color=blue]AS LONG[/color] 
             
             WordCount = [color=blue]LEN[/color](TempStr) \ [color=red]2[/color]
             [color=blue]DIM[/color] wordArray([color=red]1[/color] [color=blue]TO[/color] WordCount) [color=blue]AS WORD AT STRPTR[/color](TempStr)
             [color=blue]FOR[/color] Looper = [color=red]1[/color] [color=blue]TO[/color] WordCount
               [color=blue]ROTATE LEFT[/color] wordArray(Looper), [color=red]8[/color] [color=green]'If conversion from Little Endian to Big Endian is needed.[/color]
               [color=blue]MSGBOX HEX$[/color](wordArray(Looper), [color=red]4[/color])
             [color=blue]NEXT[/color]
            [/size][/font]
            Last edited by Pierre Bellisle; 28 Mar 2008, 04:23 PM.

            Comment


            • #7
              Thanks Pierre, I knew about the multiple bytes and just hadn't worried about it for the question. Didn't know about "DIM AT", so I'll look at that as well. It seems like my programming comes in bursts, and I tend to forget everything in between ;-(

              Comment


              • #8
                I think this is a perfect case for a Union
                Code:
                Union BtoLEnd
                    b(1) as Byte
                    i as Integer 
                End Union
                
                Function etc
                Local Sp as Dword
                local BE as BtoLend
                    Dim TempByte(1 to Len(TempString$)) as Byte at StrPtr(TempString$)
                    BE.b(1) = TempByte(25)
                    BE.b(0) = TempByte(26)
                    ' the little ended integer is now in BE.i

                Comment

                Working...
                X