Reading a hex file?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Reading a hex file?

Leo M. Cabrera-2
Hello everyone! (again)

I got a question. Given a hexadecimal number (ie. 0x13), how can I convert that to the string "13"? I use print() and it gives me a random ASCII character.

Is this possible? Thanks in advance.


Reply | Threaded
Open this post in threaded view
|

Re: Reading a hex file?

Leo M. Cabrera-2
PS: The hex number is in string (character) format. Sorry about that.

L-28C wrote:
Hello everyone! (again)

I got a question. Given a hexadecimal number (ie. 0x13), how can I convert that to the string "13"? I use print() and it gives me a random ASCII character.

Is this possible? Thanks in advance.




Reply | Threaded
Open this post in threaded view
|

Re: Reading a hex file?

Sam Roberts-2
On Thu, Mar 22, 2007 at 08:29:00PM -0400, L-28C wrote:
> PS: The hex number is in string (character) format. Sorry about that.

string.sub() to get rid of the leading "0x"?

> >I got a question. Given a hexadecimal number (ie. 0x13), how can I 
> >convert that to the string "13"? I use print() and it gives me a random 
> >ASCII character.

Reply | Threaded
Open this post in threaded view
|

Re: Reading a hex file?

Leo M. Cabrera
Sam Roberts wrote:
On Thu, Mar 22, 2007 at 08:29:00PM -0400, L-28C wrote:
PS: The hex number is in string (character) format. Sorry about that.

string.sub() to get rid of the leading "0x"?
No; I meant the character itself, not the '0x13' string representation. The 13th ASCII character, device control 3. It's printing that instead of the number '13'.

Get what I'm saying? Thanks.
I got a question. Given a hexadecimal number (ie. 0x13), how can I convert that to the string "13"? I use print() and it gives me a random ASCII character.



Reply | Threaded
Open this post in threaded view
|

Re: Reading a hex file?

Romulo Bahiense
Leo M. Cabrera wrote:
Sam Roberts wrote:
On Thu, Mar 22, 2007 at 08:29:00PM -0400, L-28C wrote:
PS: The hex number is in string (character) format. Sorry about that.

string.sub() to get rid of the leading "0x"?
No; I meant the character itself, not the '0x13' string representation. The 13th ASCII character, device control 3. It's printing that instead of the number '13'.

Get what I'm saying? Thanks.
I got a question. Given a hexadecimal number (ie. 0x13), how can I convert that to the string "13"? I use print() and it gives me a random ASCII character.

-- Convert a hexadecimal string to a number ("13" -> 19, "FF" -> 255)
n = tonumber( "13", 16 )

-- Convert a ASCII code (number) to a char (13 -> "\r", 10 -> "\n")
c = string.char( n )

-- Convert a char to it's ASCII code ("\r" -> 13, "\n" -> 10 )
print( string.byte( c ) )

> I use print() and it gives me a random ASCII character
Random?

--rb

Reply | Threaded
Open this post in threaded view
|

Re: Reading a hex file?

Samuel Chi
In reply to this post by Leo M. Cabrera
>i=18
>=string.format('0x%x', i)
0x12

 
2007/3/23, Leo M. Cabrera <[hidden email]>:
Sam Roberts wrote:
> On Thu, Mar 22, 2007 at 08:29:00PM -0400, L-28C wrote:
>
>> PS: The hex number is in string (character) format. Sorry about that.
>>
>
> string.sub() to get rid of the leading "0x"?
>
No; I meant the character itself, not the '0x13' string representation.
The 13th ASCII character, device control 3. It's printing that instead
of the number '13'.

Get what I'm saying? Thanks.
>
>>> I got a question. Given a hexadecimal number (ie. 0x13), how can I
>>> convert that to the string "13"? I use print() and it gives me a random
>>> ASCII character.
>>>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Reading a hex file?

Leo M. Cabrera
In reply to this post by Romulo Bahiense
-- Convert a char to it's ASCII code ("\r" -> 13, "\n" -> 10 )
print( string.byte( c ) )
Thank you very much; that did the job.

> I use print() and it gives me a random ASCII character
Random?
Random in the sense of output I did not appreciate.



Romulo Bahiense wrote:
Leo M. Cabrera wrote:
Sam Roberts wrote:
On Thu, Mar 22, 2007 at 08:29:00PM -0400, L-28C wrote:
PS: The hex number is in string (character) format. Sorry about that.

string.sub() to get rid of the leading "0x"?
No; I meant the character itself, not the '0x13' string representation. The 13th ASCII character, device control 3. It's printing that instead of the number '13'.

Get what I'm saying? Thanks.
I got a question. Given a hexadecimal number (ie. 0x13), how can I convert that to the string "13"? I use print() and it gives me a random ASCII character.

-- Convert a hexadecimal string to a number ("13" -> 19, "FF" -> 255)
n = tonumber( "13", 16 )

-- Convert a ASCII code (number) to a char (13 -> "\r", 10 -> "\n")
c = string.char( n )

-- Convert a char to it's ASCII code ("\r" -> 13, "\n" -> 10 )
print( string.byte( c ) )

> I use print() and it gives me a random ASCII character
Random?

--rb