String to real Hex question

Just starting out? Need help? Post your questions and find answers here.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

String to real Hex question

Post by SFSxOI »

If I do this:

OpCda = $EB
OpCdc = $75

the values of OpCda and OpCdc represent the hex values. However, if I read a hex value in from a file as a string and then want to assign a variable to it I have to use a string variable, like this:

hexvalue.s (or hexvalue$) = "the hex value in a string format", if the hex value is for example $EB then in other words : hexvalue.s = "$EB". But...I need it in a real hex value assigned to a numeric variable like this:

OpCda = $EB
OpCdc = $75

and since the hex values will change and not remain constant depending on changes in the file from which the hex values are read I just can't say that a numeric variable will always represent a certain hex value, i can't seem to get the hex values assigned to a numeric variable because of the necessity (which PB wants to do) of having to read the file in text strings. How do you get the hex values from a string assigned to a numeric variable that represents the hex value?

confused...
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

The following any use?

Code: Select all

;PUREBASIC 4 BETA 9.

;The following function takes a hexadecimal string and returns the decimal value.
;Returns 0 if there is an error.
Procedure.l ValH(hex$)
  Protected i, char, result
  hex$=UCase(hex$)
  If Left(hex$,1)<>"$" : ProcedureReturn 0 : EndIf
  For i = 1 To Len(hex$)-1
    char=Asc(Mid(hex$,Len(hex$)-i+1,1))  
    Select char
    Case '0' To '9'
      result + (char-48)*Pow(16,i-1)
    Case 'A' To 'F'  
      result + (char-55)*Pow(16,i-1)
    Default
      ProcedureReturn 0    
    EndSelect
  Next
  ProcedureReturn result
EndProcedure


Debug ValH("$ff")
You'll probably want to do something about the zero return whenever there's an error etc.

Also, if speed is an issue then you'll want to replace the Pow() command with a kind of 'running multiplier' etc.
I may look like a mule, but I'm not a complete ass.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

srod

Thanks for your response. The problem is that converting to a decimal number isn't enough because the only way I have to convert that back to a hex always results in a string, which I can't use because

opcdea.l can = $EB
but can not = "$EB"

because opcdea.l is not a string variable and is a numeric variable, so what ever I read in from the file has to retain its true meaning - if I read in a $EB (which PB can only read in as a string in the readfile thing) then I need that item to remain the true hex value of EB that I can assign a numeric variable to, like opcdea.l = $EB.

maybe i'm saying this wrong ??

maybe something like a "decimal to hex" conversion but return a hex that can be assigned to a numeric variable instead of a string variable?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I must admit that I'm struggling to understand your need here. :?

You don't want to convert to a numeric value,... but at the same time you do want to convert to a numeric.

It's probably me! :)
maybe something like a "decimal to hex" conversion but return a hex that can be assigned to a numeric variable instead of a string variable?
But a hex that can be assigned to a numeric variable has a decimal equivalent... :?
I may look like a mule, but I'm not a complete ass.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

I'm struggling to express it also, so its not you, its me :)

If I do this:

opcda = $EB

I can then write hex EB to memory by using the variable 'opcda' and it works. However, if I read the value $EB from a text file like this:

Code: Select all

UserFile$=OpenFileRequester("select a file.", "", "Text File|*.txt", 0)

If ReadFile(0,UserFile$)
  cda$=ReadString()
  CloseFile(0)
and...the value read from the file is $EB which is now represented by the string variable cda$...and then I try to write cda$ to a memory location it doesn't work and instead writes some other code. However if I write the numeric variable 'opcd' to memory it writes correctly. So this possibly indicates the value in cda$ does not really represent the hex value of EB?

OK, this comes down to cda$ is a string variable representing the hex value EB where opcda is a numeric variable also representing the hex value EB, however, obviously the cda$ is not being written to memory properly whereas the numeric variable opcda does write properly to memory.

So what I need is the ability to read the hex value EB from a string containing a mixture of test strings and hex values in a text file and write the hex values to memory as a numeric variable instead of a string. So if the line looks like this:

some.exe, some mem location, EB

I need the EB to be extracted as hex EB and not string EB.

I can read the file fine and seperate the various items on the line, and I can write to memory fine (as long as its a numeric variable) but I can't use the EB as a string variable, only as a numeric variable. like opcda = $EB and not cda$ = $EB.

so yes, I do want to write a numeric variable but the numeric variable i use needs to represent the hex value, in hex and not decimal, read from the file.

OK, i'm completly lost myself, and you... :)


srod wrote:I must admit that I'm struggling to understand your need here. :?

You don't want to convert to a numeric value,... but at the same time you do want to convert to a numeric.

It's probably me! :)
maybe something like a "decimal to hex" conversion but return a hex that can be assigned to a numeric variable instead of a string variable?
But a hex that can be assigned to a numeric variable has a decimal equivalent... :?
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hi SFSxOI

Taking a punt here: Using things like $7F in code is just a way of representing an integer value (in this case 127). It is not actually a different numeric type. That is, saying cda = $7F is the same as saying cda = 127, only using base 16 rather than base 10. Internally, PureBasic is using the same value.

Still punting: A string of "7F" in Ascii is in fact two integer values, 55 and 70 (or "7f" is 55 and 102). Viewed in a text reader, like notepad, you see 7F (or 7f). In a hex editor you see 37 46 (or 37 66).

If you already know that, forgive the presumption. Just wanting to make sure as your question implies otherwise - but that may just be my bad interpretation.


Now the file you read from, do you create it or is it created by another app (outside your control)?

If you create it, can you write the value as a number, eg, WriteLong, then ReadLong later?

If it comes from somewhere else, or if you have to represent the numeric values as strings in the file, then you will have to convert the string, as per srod's conversion.
@}--`--,-- A rose by any other name ..
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hi again,

Just read your topic "Reading multiple values from a file" and ..

.. assuming this is a follow-on from that ..

.. you are reading string representations of hex values (which in turn are just a way of representing a value) so you will need to convert the string back to a real value using something like srod's converter, or the dirty one below (which assumes the input has a valid characters).

Code: Select all

Procedure dirtyConvert(x.s)
  num.l = 0
  For i = 1 To Len(x)
    num = (num << 4) + FindString("123456789ABCDEF",UCase(Mid(x,i,1)),1)
  Next
      Debug "From $" + x + " to " + Str(num) + " (" + Hex(num) + ")"
  ProcedureReturn num
EndProcedure

w.s = "F"   : r = dirtyConvert(w)
w.s = "0F"  : r = dirtyConvert(w)
w.s = "7F"  : r = dirtyConvert(w)
w.s = "F0"  : r = dirtyConvert(w)
w.s = "FF"  : r = dirtyConvert(w)
w.s = "100" : r = dirtyConvert(w)
w.s = "10F" : r = dirtyConvert(w)
@}--`--,-- A rose by any other name ..
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Maybe this helps. For Windows only. ;)

Code: Select all

#STIF_SUPPORT_HEX = 1

Procedure HexStr2HexVal(HexString$)
  ;...Need to replace "$" with "0x" for API function to work properly
  hex$ = ReplaceString(HexString$, "$", "0x")
  myHexValue = -1
  StrToIntEx_(@hex$, #STIF_SUPPORT_HEX, @myHexValue)
  ProcedureReturn myHexValue
EndProcedure
 
cda$ = "$EB"
cdaHexVal = HexStr2HexVal(cda$)
MessageRequester("Hex String = " + cda$, "Converted To Decimal Value = " + Str(cdaHexVal))
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

OK, i'm gonna try this when I get home later...thank you all for your help and assistance :) and....for your tolerance with putting up with my confusing - and sometimes dumb - questions :)
Post Reply