String to Hex format 
Author Message
 String to Hex format

Hi

If anyone knows how can i change a string var. to Hex ,in other
language i do HEX(A$) but under Clarion How???

Thxs.

JM Villasenor



Mon, 22 Mar 2004 06:25:38 GMT  
 String to Hex format
Here is some code that will possibly work for you. I think I found it on
IceTips a long time ago. Enjoy!

Note that Dec2Hex takes a LONG number parameter and returns a string. You
could easily pass the number as a string and then do a simple assinment to a
LONG variable inside of the function.

    MAP
      !-- Local functions
      GetVerInfo(STRING),BYTE
      Dec2Hex(LONG),STRING
     END

!==============================================================
Dec2Hex   FUNCTION(Value)
!==============================================================
HexNumberTableS    STRING('0123456789ABCDEF')       ! Hex numbers
HexNumberTable     STRING(1), DIM(16), OVER(HexNumberTableS)
Part               SHORT
ReturnValue        STRING(4)
BANDValue          LONG
    CODE

    BANDValue = 0Fh                         ! Set up the Band value
    LOOP WHILE (value >= 16)                ! Loop while larger than 16
      Part = BAND(Value,BANDValue)
      Value = BSHIFT(Value,-4)
      ReturnValue = CLIP(LEFT(ReturnValue)) & HexNumberTable[Part+1]
    END

    IF Value > 0
      ReturnValue = HexNumberTable[Value+1] & CLIP(LEFT(ReturnValue))
    ELSE
      ReturnValue = '0'
    END

    IF LEN(CLIP(LEFT(ReturnValue))) < 2     ! Less then two wide
      ReturnValue = '0' & CLIP(LEFT(ReturnValue))
    END

    RETURN(ReturnValue)                     ! Return the string
!==============================================================

!==============================================================
Hex2Dec FUNCTION(hString)
!==============================================================
lVal                LONG
sPos                BYTE
Correction          BYTE
hCstring            CSTRING(11)
    CODE

    hCstring = CLIP(hString)
    lVal = 0
    Correction = CHOOSE(UPPER(SUB(hCstring,-1,1)) = 'H',1,0)
    LOOP sPos = 1 TO LEN(hCstring)-Correction
      lVal = lVal*16+(INSTRING(hCstring[sPos],'0123456789ABCDEF',1,1))-1
    END

    RETURN(lVal)
!==============================================================

--
Jody Ronning - Innovative Software Creations

Web: http://www.iscreations.com


Quote:
> Hi

> If anyone knows how can i change a string var. to Hex ,in other
> language i do HEX(A$) but under Clarion How???

> Thxs.

> JM Villasenor



Mon, 22 Mar 2004 11:09:29 GMT  
 String to Hex format
FAQ'd
http://www.clarionfoundry.com/General/Number_Systems/cdth.htm
http://www.clarionfoundry.com/General/Number_Systems/chtd.htm

--
Kind regards,
James Cooke

www.ClarionFoundry.com

--

Quote:
> Here is some code that will possibly work for you. I think I found it on
> IceTips a long time ago. Enjoy!

> Note that Dec2Hex takes a LONG number parameter and returns a string. You
> could easily pass the number as a string and then do a simple assinment to
a
> LONG variable inside of the function.

>     MAP
>       !-- Local functions
>       GetVerInfo(STRING),BYTE
>       Dec2Hex(LONG),STRING
>      END

> !==============================================================
> Dec2Hex   FUNCTION(Value)
> !==============================================================
> HexNumberTableS    STRING('0123456789ABCDEF')       ! Hex numbers
> HexNumberTable     STRING(1), DIM(16), OVER(HexNumberTableS)
> Part               SHORT
> ReturnValue        STRING(4)
> BANDValue          LONG
>     CODE

>     BANDValue = 0Fh                         ! Set up the Band value
>     LOOP WHILE (value >= 16)                ! Loop while larger than 16
>       Part = BAND(Value,BANDValue)
>       Value = BSHIFT(Value,-4)
>       ReturnValue = CLIP(LEFT(ReturnValue)) & HexNumberTable[Part+1]
>     END

>     IF Value > 0
>       ReturnValue = HexNumberTable[Value+1] & CLIP(LEFT(ReturnValue))
>     ELSE
>       ReturnValue = '0'
>     END

>     IF LEN(CLIP(LEFT(ReturnValue))) < 2     ! Less then two wide
>       ReturnValue = '0' & CLIP(LEFT(ReturnValue))
>     END

>     RETURN(ReturnValue)                     ! Return the string
> !==============================================================

> !==============================================================
> Hex2Dec FUNCTION(hString)
> !==============================================================
> lVal                LONG
> sPos                BYTE
> Correction          BYTE
> hCstring            CSTRING(11)
>     CODE

>     hCstring = CLIP(hString)
>     lVal = 0
>     Correction = CHOOSE(UPPER(SUB(hCstring,-1,1)) = 'H',1,0)
>     LOOP sPos = 1 TO LEN(hCstring)-Correction
>       lVal = lVal*16+(INSTRING(hCstring[sPos],'0123456789ABCDEF',1,1))-1
>     END

>     RETURN(lVal)
> !==============================================================

> --
> Jody Ronning - Innovative Software Creations

> Web: http://www.iscreations.com



> > Hi

> > If anyone knows how can i change a string var. to Hex ,in other
> > language i do HEX(A$) but under Clarion How???

> > Thxs.

> > JM Villasenor



Mon, 22 Mar 2004 12:52:25 GMT  
 String to Hex format
It would REALLY be nice to be able to do something like this:


                            -Ray.


Quote:
> Hi

> If anyone knows how can i change a string var. to Hex ,in other
> language i do HEX(A$) but under Clarion How???

> Thxs.

> JM Villasenor



Mon, 22 Mar 2004 21:53:07 GMT  
 String to Hex format
      MODULE('C55RUN')
       ltoa(LONG, *CSTRING, LONG), LONG, RAW, NAME('_ltoa')
      END
This will convert a long to a hex string.


Quote:
> It would REALLY be nice to be able to do something like this:


>                             -Ray.



> > Hi

> > If anyone knows how can i change a string var. to Hex ,in other
> > language i do HEX(A$) but under Clarion How???

> > Thxs.

> > JM Villasenor



Tue, 23 Mar 2004 00:01:40 GMT  
 String to Hex format
Hi Juan,



Quote:
>If anyone knows how can i change a string var. to Hex ,in other
>language i do HEX(A$) but under Clarion How???

Here is how I do it:

 Program
 Map
   MODULE('C_API')
     SPrintF(*CSTRING,*CSTRING,LONG),C,RAW,NAME('_sprintf')
   END
 End

FormatStr CSTRING('%0lX')
cRH        Cstring(3)
cR         Long

 Code
 Cr = 1024
 sprintf(cRH,FormatStr,cR)                     ! Format the byte to HEX
 Message('Decimal Value: ' & Cr & '|Hex Value: ' & cRH)

Works fine.

Best regards,

Arnr Baldvinsson        
San Antonio, Texas, USA
www.icetips.com & www.candobidding.com

ICQ:  113314380



Tue, 23 Mar 2004 00:15:30 GMT  
 String to Hex format
I didn't want to have to borrow from other languages.  Many of Clarion's
commands may be duplicated with API or C equivalents.  Why bother using
Clarion then?  :)

                            -Ray.



Quote:
> Use the C runtime function LtoA

> fred cstring(20)
> code
>   LtoA( a_number , fred , 16)

> --
> Paul Attryde



Tue, 23 Mar 2004 00:34:22 GMT  
 String to Hex format
Hi Arnor -

That seems that it could cause some trouble. Apparently, the SPrintF()
procedure walks on whatever memory exists past the length of the
declared CSTRING. E.G., a length of 3 shouldn't be enough to display
400.

Of course, I could be wrong.

I guess it would be okay to use as long as you declare a long enough
CString to contain characters.


Quote:

>cRH        Cstring(3)

Jeff Slarve  
http://www.jssoftware.com


Tue, 23 Mar 2004 01:05:03 GMT  
 String to Hex format
I don't think I am making myself understood.  :)

The point is that there is not really a good reason to not add pictures for
binary, octal and hex display.  It should have been added to the Clarion
language many versions ago.

The C function, sprintf(), can do most, if not more, than Clarion's picture
system can, so why not get rid of Clarion's own pictures and simply use
sprintf?

The answer to this last question may help understand my point.

                            -Ray.



Quote:
> Why bother writing your own code in CW when you can call a procedure that
> does it for you? :)

> --
> Paul Attryde



Tue, 23 Mar 2004 02:47:41 GMT  
 String to Hex format
Hi Jeff,



Quote:
>I guess it would be okay to use as long as you declare a long enough
>CString to contain characters.

I think it should.  This method was orignally posted on c.l.c. by Shur
I. Carpoff back in april of 1998.  I took this from a function I've
used to convert color values in long to hex values for html.  In that
case the hex value will always be in the range of 00-FF so it fits in
the 3char cstring.  I didn't notice that it was displaying the 400h
without problem on a 3char cstring, which would probably have led to
GPF in any thing bigger than that dummy program.  The orignal code
declare it as cstring(10) which should be sufficient.  

Best regards,

Arnr Baldvinsson        
San Antonio, Texas, USA
www.icetips.com & www.candobidding.com

ICQ:  113314380



Tue, 23 Mar 2004 02:54:34 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Converting dec to hex in non-string format?

2. Bug in character string hex formatting

3. Convert a hex string to the same hex number

4. Reading a hex string of 4 hex characters

5. Hex to Bin, Bin to Hex, Hex to Dec, Dec to Hex

6. Hex String to Decimal String ocnversion.

7. Getting Ascii string from hex 'string'

8. Fast conversion of hex string to character string

9. format specifiers in format string param.

10. Hex format code?

11. Intel hex file format

12. hex format

 

 
Powered by phpBB® Forum Software