[Gambas-user] Reading hex from a string socket read

ron ronstk at ...239...
Thu Feb 19 16:46:27 CET 2004


On Thursday 19 February 2004 15:01, adam krell wrote:
> Hi,
>
> I am using the socket component to make a tcp
> connnection and read some data.  The data needs to be
> decoded from hex.  I'm doing that with this command:
>
> Dim TwoByteCode AS Integer
>
> TwoByteCode = Val(Str$(Asc(Mid$(Buffer,1,1))) &
> Str$(Asc(Mid$(Buffer,2,1))))
>
> Is there a more efficient way to do this?  If I am
> decoding a long string of data this can really slow
> down the program.
>
> Thanks,
>
> Adam
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail SpamGuard - Read only the mail you want.
> http://antispam.yahoo.com/tools
>
>
> -------------------------------------------------------
> SF.Net is sponsored by: Speed Start Your Linux Apps Now.
> Build and deploy apps & Web services for Linux with
> a free DVD software kit from IBM. Click Now!
> http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user

' convert one character to the Hexadecimal value
function AscHex(sChr as string) as integer
	return instr("0123456789ABCDEFG",ucase$(sString))-1
end

' same but for a two charcter string
function AscByte(cChrs as string) as integer
 return AscHex(left$(cChrs))*16 +AscHex(right$(cChrs))
end

' or this way
function AscByte(cChrs as string) as integer
dim r as integer
 r = instr("0123456789ABCDEFG",ucase$(sString))-1) * 16
 r = instr("0123456789ABCDEFG",ucase$(sString))-1
 return r
end

' do it for a n character string
' be carefull the value may exeed the integer range
' if the input string is to long
function HexVal(sHex as string) as integer
 dim x as integer
 dim h as integer
 for x = 1 to len(cStr)
  h = h + AscHex(mid$(cStr,x,1))*16	
 next
return H
end

function OtherWay(sHex as string) as integer
dim r as integer
dim c as integer
  r = 0
  c = asc(cChr)
  if c>9 then c = c - 6  '<--- c=c-6 gives error
  r = (r * 16) + c

The hint is using instr("0123456789ABCDEF")-1 for get a value
and mid$("0123456789ABCDEF", X + 1, 1) to get the hex character for X

Ron












More information about the User mailing list