# Help with HEX to integer

Junior MemberPosts: 8
I'm relatively new at Netlinx, trying to get up to speed and have a problem where I'm hoping someone might be able to steer me in the right direction. I'm trying to use HEXTOI in a part of a function; I'll exclude the rest for clarity, as this is definitely the part that's broken.

STACK_VAR CHAR cHours[4]; // part of a reply in hex - I need to convert to an integer somehow
STACK_VAR LONG lSeconds; // the variable that I want to load the integer into

lSeconds = HEXTOI(cHours)

In debugger I can see I have a hexidecimal string in my cHours array, but I never seeing anything at all in lSeconds, so I re-read the documentation on HEXTOI, and it does state 'a hexadecimal formatted string'. This is where I believe I've gone wrong, but I'm not sure where to go with it - the example in Netlinx help shows Num = HEXTOI('126EC') // Num = 75500, my problem I'm guessing is that my array holds an actual hex string, as opposed to "an ASCII string containing the hexadecimal representation of a number".

I guess I'm not grasping some essential bit. I played around and just hard-coded a fake reply in place of cHours, formatted in single quotes and that got a result. I cannot figure out how to convert my hex string into ASCII characters- I've beat this to death. Does anybody know a way or method to convert a HEX string in an array into something that HEXTOI can use, or maybe HEXTOI won't work on an array, but that doesn't make sense - I just can't see it, perhaps I'm so fixated on doing this this one way that I'm blind to another method. Anyhow, I'd be greatly obliged if anyone has any ideas.
Thanks,

• Code Monkey Posts: 315
HEXTOI doesn't actually convert hex to integer. Hex is already an integer. You can just pass the hex value to an integer an be done with it.

HEXTOI converts ascii representations of hex to integers.
• Formerly Caffeinated Programmer Posts: 1,917
I think you might be converting when you don't have to. Can you provide actual examples of the data coming in and how you want to deal with it?

Jeff
• Registered User Posts: 106
If your array is an array of hex bytes, such as:

cHours = "\$01, \$02, \$03, \$04"

then the first thing is to determine whether this is high byte first or last. Assuming high byte first, then you can convert this to a number by doing something like the following: (written out long-hand for clarity)

lSeconds = 1*(256*256*256) + 2*(256*256) + 3*(256) + 4 (parathesis are not necessarily required)

Of course, a better way, would be to handle this in a short FOR or While loop parsing a byte at a time and then adding it to an incremental sum, multiplying the sum by 256 before adding the next byte. Same process as adding digits in a 3 or 4 digit number using base 10.

Is this what you are after?

Sheldon Samuels
• Junior Member Posts: 320
ipssheldon wrote: »
lSeconds = 1*(256*256*256) + 2*(256*256) + 3*(256) + 4 (parathesis are not necessarily required)

Of course, a better way, would be to handle this in a short FOR or While loop parsing a byte at a time and then adding it to an incremental sum, multiplying the sum by 256 before adding the next byte. Same process as adding digits in a 3 or 4 digit number using base 10.

Base 16, being a power of 2, gives you a much more efficient way to combine bytes:

bytes[1]<<24 | bytes[2]<<16 | bytes[3]<<8 | bytes[4]

Or if it's little-endian:

bytes[1] | bytes[2]<<8 | bytes[3]<<16 | bytes[4]<<24

No multiplication or for loops required.
• Registered User Posts: 106
That would seem to be more efficient, although I think it is important to understand the math behind the shifting. For each shift left, you are multiplying by 2. So shifting left 8 bits is the same as multiplying by 256. Ultimately, it would depend on how the compiler handles each shift vs a multiplication calculation.

The important issue is to realize how to convert the individual digits to a whole number. Individual coding style might influence how to ultimately handle the translation. If you were writing a function to handle any number of digits, then you would shift 8 and add the next digit, which would probably be very similar to multiplying by 256 and adding the next digit.

Just my opinion. Lots of ways to skin a cat.
• Junior Member Posts: 320
You are correct that shifting left is equivalent to multiplying by two, but inside a processor it is much simpler to move bits to the left than it is to multiply, which frequently takes many cycles to complete.

However, there doesn't seem to be much speed difference on AMX. I get an average difference of 176 msec between the two methods -- over 100,000 iterations. The lack of difference here may be due to the bytecode overhead, so that each operation takes approximately the same amount of time regardless of what it's actually doing, since the overhead is large in comparison to the actual operation time.
• Junior Member Posts: 8
Help with HEX to integer

Thanks for the response guys, you've given me a bit to chew on - here's an example of a reply from the device: "\$23,\$8C,\$00,\$04,\$31,\$2A,\$00,\$00,\$0E". The 1st 4 bytes (\$23,\$8C,\$00,\$04) is for 'Lamp Hours', while bytes 5-8 (\$31,\$2A,\$00,\$00) would be the actual lamp hours data, (which is actually seconds with the values reversed). \$0E is a checksum..

I have a line prior to the code I posted that goes:
cHours = "cLampHours[8],cLampHours[7],cLampHours[6],cLampHours[5]"
that gets the bytes I want and plugs them into cHours in the correct order - I can see cHours in debugger with the value I'm looking to get. Taking the bytes from cHours (0,0,2A,31) in this example, 2A31 in hex is 10801 in decimal (my seconds figure that I'm after), which I'm then wanting to divide by 3600 to give me 3.002 hours. So that's what got me off on this goose chase - this may all stem from my being a math idiot, so I don't doubt that there's another way to do this, I tried removing HEXTOI and using lSeconds = cHours just to see what would happen - of course I get a warning for 'Converting a string to a [LONG]'. It ran but didn't do anything for me. Like I said-math idiot, I was assuming there would be a straightforward way to get an integer value that I could do the math on (maybe I could do the division in hex - hm, I have no idea how to divide in hex, so I'm off googling as I write this to see if I can get any ideas from that). The next leap for me would be doing it in code, still doesn't change that at the end of it all I still need to have an integer value I'm looking for to send lamp hours to a TP, which brings me full circle to just wanting to convert the hex to an integer, where I'd be done by now if there was a way. There's got to be a way.
Again, thanks for the help guys!
____________________________________________________________________________
"A child of five would understand this. Send someone to fetch a child of five." - Graucho Marx
• Junior Member Posts: 671
The master doesn't know the difference between hex and decimal integers. To divide two numbers, just divide. It doesn't make any difference how the human readable display (hex or decimal) is presented.

Also, the netlinx language does not have a real character data type (I don't know of a language that does -- suppose there is one). What we see as a character is actually a one byte integer. Special rules about how to manipulate and display these integers is what gives us the illusion that it is actually a character variable.

This means that you can actually use and manipulate character (and string) data as integers, though the compiler may prevent you from doing some things and may complain about others.

The example of the protocol you describe looks familiar to me, for some reason. What projector are you controlling?

This is how someone approaching this problem for a reason unrelated to a projector might do it(same as jweather's answer):
```else if(left_string(sString,4) = "\$23,\$8C,\$00,\$04")
// response to Lamp Hours  query
{
lLampSeconds = sString[5] +  (sString[6] << 8)  +  (sString[7] << 16)+  (sString[8] << 24)
nLampHours = type_cast(lLampSeconds / 3600)
}
```
• Code Monkey Posts: 315
Hedberg wrote: »
The example of the protocol you describe looks familiar to me, for some reason. What projector are you controlling?

Was that a joke?
• Junior Member Posts: 671
Whether a joke, I won't comment. Let's just say that it's no laughing matter.
• Junior Member Posts: 2,159
here's an example of a reply from the device: "\$23,\$8C,\$00,\$04,\$31,\$2A,\$00,\$00,\$0E". The 1st 4 bytes (\$23,\$8C,\$00,\$04) is for 'Lamp Hours', while bytes 5-8 (\$31,\$2A,\$00,\$00) would be the actual lamp hours data, (which is actually seconds with the values reversed). \$0E is a checksum..
Hedberg wrote:
The example of the protocol you describe looks familiar to me, for some reason. What projector are you controlling?

I?m curious as to what model that projector is also.
It sure is a remarkable coincidence that the protocol and the example is byte for byte the same as the ACE test. Weird huh?
• Junior Member Posts: 671
Joe Hebert wrote: »
I?m curious as to what model that projector is also.
It sure is a remarkable coincidence that the protocol and the example is byte for byte the same as the ACE test. Weird huh?

Yeah, it's weird. Several different issues involved and I'm not real sure how I feel about any particular one of them.
• X Member Posts: 4,368
Well IMHO if someone is going to do the dumb test and needs help with certain code related items that's what the forum is for. It doesn't matter if it's for the ACE hole test or not as long as the questions are limited to specific problems.
• Junior Member Posts: 671
I don't disagree with your attitude at all. As applied to this specific thread, I'll not comment more.
• Senior Member - 4000+ posts Posts: 4,177
vining wrote: »
Well IMHO if someone is going to do the dumb test and needs help with certain code related items that's what the forum is for. It doesn't matter if it's for the ACE hole test or not as long as the questions are limited to specific problems.

In an normal setting you'd ask the instructor for help or clarification. this is like asking someone in the class.

However, the original poster didn't say anything about A.C.E. exam. If she/he had said, "there's this problem on the ACE exam I'm having difficulty with..." that might have been better, I suppose.

• X Member Posts: 4,368
ericmedley wrote:
In an normal setting you'd ask the instructor for help or clarification. this is like asking someone in the class.
I've often found that asking someone in class for assistance is sometimes better for both parties involved. The asker gets the answer from a potentially different perspective, probably more to his own and the person answering the question re-affirms his understanding of the question or realizes he's f'in confused too and then they both confront the instructor. Plus we (forum members) are a lot more responsive than our instructors (AMX training departement) and we keep longer hours.

However most folks and instructors would frown if this was done during a test but is this really a relevant test and isn't the ultimate goal to increase ones knowledge.

To me the only legitimate purpose this test has is to get folks you haven't been coding on a regular basis to clear the cob webs from their brain and put their certification to practice. So for me, more power to him. Of course that's coming from a future flunky.
• Formerly Caffeinated Programmer Posts: 1,917
I don't think there is any impropriety in the original post. The post was not looking for copy and paste code, simply asking for assistance in clarifying a difficulty. Should it make any difference if the problem relates to a test or an actual job? People on this forum have complained time and time again that the test doesn't reflect real-world situations, but it seems that this is the ultimate emulation of a real-world situation. How many times has this forum provided support or clarification for programmers dealing with a new or quirky device implementation? I know it has helped me more than once.

As long as the goal of the post is learning and bettering one's ability to code and complete reliable implementations of automation and control, we all win. The more qualified and competent programmers present in our sector, the better our overall image becomes. I would much rather have to compete with another company and the services/products they offer than to compete with notion that automation is over priced and unreliable if it works at all.

Just my 3.5 cents,
Jeff
• Posts: 0
So many things...

Hi, All.

Nothing wrong with learning from the good folks here in the forums. Ideally, a specific exam question should have been clarified with the instructor. The general idea is to continue learning after class, and it is common for the trainers to have ongoing dialog with students. If there's no response, please forward to [email protected] and / or to me [email protected].

Thanks,
Paul
• Junior Member Posts: 8
My 2 cents

As I got back to see if there were any other responses, I wanted to clarify that yes-this is in response to the Programmer 2 ACE exam, at the time I was not given to believe that I was doing anything underhanded by putting out a call for help, this seems to be the place for it. Especially as this is after the fact - I've already sent in my final last month and know that after many hours of frustration in trying to get it to work, the code I used for my Lamp Hours (among other things) will fail. However, that did not stop me from wanting to try to gain an understanding of where I was going wrong and how to actually make it work after the fact. Yes, I could have contacted my instructor but I tend to want to leave them alone if at all possible - it's just the way I am, bothering other people with little stuff is a last resort for me - automatically take the approach that they've got enough on their plate as it is. The forum seems tailor-made for this sort of thing, with talented and experienced programmers from all walks, offering different viewpoints and solutions to noobs such as myself. As it happens I work with guy who's a friggin' genius at this stuff who is my day-to-day mentor, but most days he's got his hands so full, and that leaves me nowhere else to go.

As much as I enjoyed the class and the instructor was great, there were a Great Many things in the final exam that are several layers deeper than the whats and hows that were covered in class, but I keep my whining to a minimum as I believe I'm having a harder time with this than most.

I apologize to the community at large if there was any wrongdoing - this just gives me a bad feeling about posting anything ever again. Thanks sincerely for the help though, hoping I can return the favor someday.
• Junior Member Posts: 320
Don't worry about it... it would have been better to say upfront "hey, I'm having problems with this part of the exam". I don't think anyone would have had a problem with that, even if you hadn't already sent in your exam. It just gives people a funny feeling to discover they've been answering exam questions inadvertently.

If people realized it was the exam initially, you might have gotten a slightly different response in the sense that people would say "try thinking about it this way" rather than "here's the code to do it". Regardless, please continue asking questions, there's nothing wrong with that. In one sense, we're all helping each other with homework questions here...
• Junior Member Posts: 671
[...]

I apologize to the community at large if there was any wrongdoing - this just gives me a bad feeling about posting anything ever again. Thanks sincerely for the help though, hoping I can return the favor someday.
Any accusations leveled against you have been very, very mild. You must be a very sensitive person.

______________________________
Harry Herald
Senior Emperor of the Known Universe
Audio Video Systems Integrators Corporatiion, Inc, PC, LLC, DDS
• Code Monkey Posts: 315
As far as I'm concerned the whole point of the practical is to demonstrate that you can do the code asked for. If you need to consult the forum, or the AMX help file, or the book you got in class, or your skype buddy, then that's what you need to do.

At the same time people are well within their rights to make fun of you for asking a question about the practical basically word for word.

Don't worry about posting here though.
• Senior Member - 4000+ posts Posts: 4,177
TonyAngelo wrote: »
At the same time people are well within their rights to make fun of you for asking a question about the practical basically word for word.

I guess that's my point. Just come out and say, "I'm working on this problem in the practical exam and I'm having trouble..." or whatever.
• Junior Member Posts: 671
TonyAngelo wrote: »
As far as I'm concerned the whole point of the practical is to demonstrate that you can do the code asked for. If you need to consult the forum, or the AMX help file, or the book you got in class, or your skype buddy, then that's what you need to do.

At the same time people are well within their rights to make fun of you for asking a question about the practical basically word for word.

Don't worry about posting here though.

It's not asking for help about an issue with the practical that's got me chuckling, it's asking the question as if it's related to some other programming task -- like a real job. Particularly asking the question in a forum filled with folks familiar with the exact problem -- interpreting an imaginary response from an imaginary projector in order to satisfy a test requirement. Sort of reminds me of a manager I used to have who would go through my trash after work looking for ideas that he could pass off as his own and for work that he could take credit for.
• Formerly Caffeinated Programmer Posts: 1,917
Hedberg wrote: »
Sort of reminds me of a manager I used to have who would go through my trash after work looking for ideas that he could pass off as his own and for work that he could take credit for.

Not to take the thread off topic.... well, actually that is my intent and I will push on regardless

I could have soooo much fun with this situation. Did you start coming up with really good sounding ideas that were completely ridiculous? Then again, this could get scary if the ideas are accepted and actually attempted. Even scarier would be them actually working! I feel a movie script in the making!

Jeff
• X Member Posts: 4,368
Larry Lawrence wrote:
I apologize to the community at large if there was any wrongdoing - this just gives me a bad feeling about posting anything ever again.
I wouldn't worry about it at all. Heck, I flat out ask on the forum if anyone wanted to sell a copy of their practical in its entirety. Of course I was joking and put the telephone number for AMX in my post although I doubt most folks noticed that or even gave a rat's a\$\$. I did receive a few PM's though............ Unfortunately I one of those anal w/ OCD types that has a hard time with other folks code. The me, me, me, me syndrome.
• Just another dude Posts: 2,905
Joe Hebert wrote: »
I?m curious as to what model that projector is also.
It sure is a remarkable coincidence that the protocol and the example is byte for byte the same as the ACE test. Weird huh?
I've actually stumbled across a code block written in C (no, not Cre\$tron, but C) for this exact protocol believe it or not. It's got parsing and everything . . . so I'd say it's a remarkable coincidence that the test writers came up with an already known protocol.

• Junior Member Posts: 114
Help with AMX Code for a Project or Exam

I too am in the final phases of completing my AMX Programming Exam. For a relatively new programmer I found it to be quite a challenge since I use a combination of 3rd party programmers, AMX and myself to complete projects.

I often found myself going to AMX Forums as well as AMX for help. In all cases I was given some guidance on how to accomplish the tasks at hand. I can also agree with many that program AMX full time that the exam is very time consuming . For me it was a great refresher. I'm sure that not all of my code will pass scrutiny but in the end I will be able to do a better job.

The "Senior Members" are an excellent source of help and provide great mentoring for us folks who do not program full time. To them I say "Thank You". I'd suggest to AMX that if someone achieves "Senior Status" on the forum or can show after two or three years of exams 100% proficiency they should have a lifetime exemption from the exam.

David Groves ACE Professional
DG Sales and Service
Colleyville, Texas 76034
• Junior Member Posts: 35
Funny how the ACE Test is utilizing NEC projector codes and Lutron Homeworks Lighting controller protocols.