Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Changing a Hex Number into a string

Author
Message
Mew151
18
Years of Service
User Offline
Joined: 1st Jun 2008
Location: Glitch city
Posted: 10th Apr 2010 05:42
No, I don't want to change the value 0xff to "0xff".

I want to change the value of, say 0xff to the ascii character 255.
And I want to change the value of 0x414243 to "ABC"

I've made a function, but it always produces the wrong output if the value is a power of 2.

Here's some code I've sort of patched together really quickly:



BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 10th Apr 2010 11:25 Edited at: 11th Apr 2010 05:40
You have included a function 'Hex2Dec' that isn't in DBPro.
Can you include that function also?

Anyway, here is one way to do it.

EDIT:I posted a better/simpler version below!
Mew151
18
Years of Service
User Offline
Joined: 1st Jun 2008
Location: Glitch city
Posted: 10th Apr 2010 20:05
Looking at it, I believe that function just changes hex to decimal.
I want to change a group of hexadecimal bytes into a string of ASCII characters.

Sorry if I was unclear, but I want to know why my function fails at powers of two. The Hex2Dec code just changes hex to decimal.

BillR
23
Years of Service
User Offline
Joined: 19th Mar 2003
Location: United States
Posted: 11th Apr 2010 05:34 Edited at: 12th Apr 2010 03:58
Did you get my piece of code to work for you?

Edit:removed the newer version, it had a math error
I'm away from home, will work on it in a few days
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 11th Apr 2010 07:40
It's not really clear what you're asking...
0xA1, whether you call it 176 decimal, or 260 octal, is still stored as 10110000.

In your function, (lets say the number passed in is 176) you're changing 176 to "0xA1" and THEN to decimal values, then to characters, adding them onto a string. I don't get it. I'm just gonna guess that you want to change a string representation of a hexadecimal value into a string made up of the ASCII characters represented by that hexadecimal value. Seems very pointless (because you would only need to do so if your string input was hexadecimal).


Mew151
18
Years of Service
User Offline
Joined: 1st Jun 2008
Location: Glitch city
Posted: 11th Apr 2010 21:23
What I'm trying to do is change a number, for example 8248, into a string.

First I try to change 8248 into a hexadecimal string, "2038"
Then I separate a byte from the string, "20"
I change the string back into a number, 32.
Then I change that into a character. " "
I do the same with the other bytes in string, until they're all gone.

The final output is " 8"

But the process fails when I use powers of two.

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 12th Apr 2010 04:55
Aahhhh, okay, then that's a lot easier

What's going on here is bitwise operations. A dword is a series of four bytes (so arg has four bytes, or 32 bits, in it). inDBPro, % before a series of 0's and 1's signifies that it is binary.

The >> and << operators are the bitwise shift operators, which shift all bits to the left or right (so %1011101>>3 = %0001011).

The and operator is a boolean and operation, basically, each corresponding bit in each number is "anded", if both bits are 1, then the new bit is 1. (ex: %111000111 and %000111111 = %000000111)

Basically, lets take the number 8248, in binary:
%010000000111000
to seperate it into two bytes:
left_byte=
(%00100000 00111000 and
%11111111 00000000)>> 8
now left_byte = %00100000, and from there, its a straight conversion using chr$

bitwise operators are awesome. Forum search or google if you want to know more about them.


Mew151
18
Years of Service
User Offline
Joined: 1st Jun 2008
Location: Glitch city
Posted: 12th Apr 2010 04:58 Edited at: 12th Apr 2010 05:02
Thanks. I'll try it.

EDIT:
It still fails on powers of two for some reason.

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 12th Apr 2010 06:30
can you elaborate? That code turns each byte of a number into its corresponding char...


Mew151
18
Years of Service
User Offline
Joined: 1st Jun 2008
Location: Glitch city
Posted: 12th Apr 2010 06:33
For some reason it fails on powers of sixteen. (Not two, my bad.)

For example, 256 returns something else.

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 12th Apr 2010 06:56
it doesn't fail. Check out http://www.asciitable.com/.

toString(1) returns SOH
toString(16) returns DLE, an escape/exception character
toString(256) returns SOH and null.
tostring(4096) returns space, null.

Everything is correct...


Login to post a reply

Server time is: 2026-07-26 12:32:43
Your offset time is: 2026-07-26 12:32:43