Decimal to Hex conversion

215 views
Skip to first unread message

Rosario C

unread,
Feb 20, 2015, 3:29:08 PM2/20/15
to mapi...@googlegroups.com
Hello,

Is there a way to display a number in Hex. I have a column that has decimal values, for instance DEC: 3219, but i would also like to convert the number to HEX (90F) when a user runs a tool.

Bill Thoen

unread,
Feb 20, 2015, 6:57:12 PM2/20/15
to mapi...@googlegroups.com
Is there a way to display a number in Hex. I have a column that has decimal values, for instance DEC: 3219, but i would also like to convert the number to HEX (90F) when a user runs a tool

If you want the MapBasic code to do that, it's:
Function DecToHex( byval nColor as integer ) as String
Dim i as smallint, nNybble, nVal as integer, sResult as string
    nVal = nColor
    For i = 0 to 7
        nNybble = nVal \ 16 ^ i mod 16
        If nNybble < 10 then
            String$( nNybble ) + sResult
        Else
             sResult = chr$( 65 + nNybble - 10 ) + sResult 
        End if
    Next
    DecToHex = sResult
End Function

You can invoke it like so:
DecToHex( 2319 ) which returns 0000090F
DecToHex( 255 * 256^2 + 255 * 256 + 255 ), which returns 00FFFFFF.

If you want a general routine that converts any decimal integer to hexadecimal, you'll need to handle a couple of more cases, but this will work for all possible integers that are inside the range of 0 < n < 16777215, i.e. valid colors.

Now, for how it works. First, some terms... A nybble is half a byte. Since a byte can be up to 256, in hex it requires two nybbles to make a byte, eg. 0x24 (36 decimal) where the first nybble is 2, and the second is 4. The leading 0x just means number is in hexadecimal.

So the count goes from 0 to 7 ( 8 nybbles ) and in the next line, I'm reading each nybble from low to high. Note the integer divide '\' which divides with truncation. By raising the power by 16, I'm basically walking up the number as if it were a decimal number where the power is 10. The mod operator just zeros out any extra leftovers just like a binary AND operation does.

An example illustrates this. Let's say we have a decimal number 41836. Applying the code, the first nybble is 
41836 \ 16 ^ 0 mod 16= 12
41836 \ 16 ^ 1 mod 16= 6
41836 \ 16 ^ 2 mod 16= 3
41836 \ 16 ^ 3 mod 16= 10
( the rest are all zeros, in this case)

The line that converts numbers to hex letters, chr$( 65 + nNybble - 10 ) + sResult simply takes advantage of the fact that 65 is also the letter 'A' and if the result is > 10, then we subtract the 10 and add the remainder to 65 and convert that to a letter. So translating our results from 10, 3,6,12 we get A36C, which is exactly what we want!

If you still have question feel free to ask.

Bill Thoen 
PS - I've discover also that anywhere you would use a decimal number to denote a color, you can use its hex value, preceded by a #, as in #00090F instead of 2319.

On Feb 20, 2015, at 1:29 PM, Rosario C <rcon...@gmail.com> wrote:

Hello,

Is there a way to display a number in Hex. I have a column that has decimal values, for instance DEC: 3219, but i would also like to convert the number to HEX (90F) when a user runs a tool.

--
--
You received this message because you are subscribed to the
Google Groups "MapInfo-L" group.To post a message to this group, send
email to mapi...@googlegroups.com
To unsubscribe from this group, go to:
http://groups.google.com/group/mapinfo-l/subscribe?hl=en
For more options, information and links to MapInfo resources (searching
archives, feature requests, to visit our Wiki, visit the Welcome page at
http://groups.google.com/group/mapinfo-l?hl=en

---
You received this message because you are subscribed to the Google Groups "MapInfo-L" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapinfo-l+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rosario C

unread,
Feb 21, 2015, 10:05:49 AM2/21/15
to mapi...@googlegroups.com
Brilliant... thank you Bill!

Rosario C

unread,
Mar 1, 2015, 7:05:12 PM3/1/15
to mapi...@googlegroups.com
Bill,

I'm still having a problem, first of all thanks for the function routine, it seems as if it should work fine, but the line of code String$ (nNybble) + sResult is causing a compile error of "unrecognized command: String$",... when i comment this line out it does compile, but obviously doesn't return a proper value for a dec to hex conversion.

If you have a moment to school me on why this is err is occurring, i'd appreciate it.

Kind regards,

Rosario Conelli



On Friday, February 20, 2015 at 3:29:08 PM UTC-5, Rosario C wrote:

Bill Thoen

unread,
Mar 1, 2015, 8:36:30 PM3/1/15
to mapi...@googlegroups.com
Whoops., sorry about that. Try Str$ instead. I'm just converting the number nNybble to a character string.


--

Rosario C

unread,
Mar 2, 2015, 7:26:11 AM3/2/15
to mapi...@googlegroups.com
Bill,

sorry to say, same error, Unrecognized command Str$..

not a big deal i if dont add it  was a nice to have in print window.. if u are still determined to figure it out, let me know.

thanks again for your time.

RC

Deryck Brown

unread,
Mar 2, 2015, 7:35:52 AM3/2/15
to mapi...@googlegroups.com
The "String$" line should read

sResult = Str$(nNybble) + sResult

or alternatively (as per the "A" to "F" case in the else-part)

sResult = Chr$(48 + nNybble) + sResult

Regards,
Deryck

Thomas Bacon

unread,
Mar 2, 2015, 8:39:56 AM3/2/15
to mapi...@googlegroups.com

Hi Rosario,

 

It looks like you may have a space between Str$ and the open bracket ‘(‘? If so, remove the space so it reads Str$(nNybble) with no spaces.

 

Kind regards,

 

Tom Bacon

GIS Engineer, Mouchel

T 01444 472380 │ E thomas...@mouchel.com W www.mouchel.com

Our values: innovation │ excellence │ integrity │ responsibility



 

Mouchel Limited (Mouchel) is registered in England and Wales with registered number 01686040 at Export House, Cawsey Way, Woking, Surrey, UK, GU21 6QX.  The information in this e-mail is confidential and may be legally privileged. It is intended solely for the addressee. Access to this email by anyone else is unauthorised. Any views or opinions expressed in this e-mail may be solely those of the author and are not necessarily those of Mouchel. No contracts may be concluded on behalf of Mouchel by means of email communications. Mouchel reserves the right to monitor and intercept emails sent and received on our network. 

Thomas Bacon

unread,
Mar 2, 2015, 8:48:30 AM3/2/15
to mapi...@googlegroups.com

Ah, just looked at the original code posted, nothing to do with a space by the looks of it. You can ignore my previous message!

 

Deryck’s posted the likely answer.

 

Tom Bacon

GIS Engineer, Mouchel

T 01444 472380 │ E thomas...@mouchel.com W www.mouchel.com

Our values: innovation │ excellence │ integrity │ responsibility

 

 Image removed by sender.

Mouchel Limited (Mouchel) is registered in England and Wales with registered number 01686040 at Export House, Cawsey Way, Woking, Surrey, UK, GU21 6QX.  The information in this e-mail is confidential and may be legally privileged. It is intended solely for the addressee. Access to this email by anyone else is unauthorised. Any views or opinions expressed in this e-mail may be solely those of the author and are not necessarily those of Mouchel. No contracts may be concluded on behalf of Mouchel by means of email communications. Mouchel reserves the right to monitor and intercept emails sent and received on our network. 

--

Bill Thoen

unread,
Mar 2, 2015, 9:11:34 AM3/2/15
to mapi...@googlegroups.com
Well, that's strange. I looked at the original code and it IS Str$(nNybble) + sResult. That's a valid command too (you can check it in the help). Perhaps the problem is that there's a missing parentheses or some other syntax error in the statement just before that one. Try putting in a print "x" statement just before that one and see if you get a similar error?


Rosario C

unread,
Mar 3, 2015, 9:30:08 PM3/3/15
to mapi...@googlegroups.com
Bill and Deryck,

I used Deryck's suggestion and i got it to work finally.. 

Thank you both, and everyone else who commented.

Kind regards,

Rosario 
Reply all
Reply to author
Forward
0 new messages