0
matt1215

Programming gurus in the house?

Recommended Posts

Very cool!! :)I wrote a program that generates a binary file, containing strategically placed bytes to program a machine. One of the values is a large number and I'm not sure how to split it into 2 bytes.

What looks like 7FFF in hex (or 32767 as input into a form) needs to look like FF 7F when viewed in the hex-editor.

I've already programmed byte placement, dumping inputs into a byte-array and looping thru a case statement. Problem is how do I get 7F from 7FXX (or 127 from 32512)? :|

Ideas?

Share this post


Link to post
Share on other sites
I'm not ashamed to say my first programming language was BASIC, as in Beginner's All-purpose Symbolic Instruction Code. Circa 1980. I was 10. I never cared for Micro$oft's visual version of it, although I've written more than a few apps in it when my customers request it.
Trapped on the surface of a sphere. XKCD

Share this post


Link to post
Share on other sites
Quote

:o
eek - I like my windows programming, thankyouverymuch.
I haven't touched binary/hex manipulation since my second semester in school..



LOL, I'd much rather program for Windows, but an updated program was requested that doesn't run on DOS. :o Think I figured out a solution, provided my understanding of RoundDown() and hex are correct.


Input = TXTInput.Text
SmallInput = Input/256
Bigbyte = RoundDown (SmallInput)
SmallByte = (SmallInput – Bigbyte) * 256


Find out tomorrow when I plug that into the compiler. :P

Share this post


Link to post
Share on other sites
Quote

result = pattern >> 8

Sounds like you want to do an arithmetic (sign extended) right shift of 8 bits.



ah... what fun...

To get the two parts of a 16-bit integer and force write it as either "big-endian" or "little-endian" you can isolate the 8msbs as described above (right bit-shift by 8) and the 8lsbs by masking the input appropriately. I forget what the bit-wise logical AND operator is in visual basic, but in C...

foo = bar & 255;

Be sure and pay attention to the most signifigant bit too, whether you're using signed integers or unsigned integers on your target hardware makes a big difference as to its meaning.

/edited to add:

Quote


Input = TXTInput.Text
SmallInput = Input/256
Bigbyte = RoundDown (SmallInput)
SmallByte = (SmallInput – Bigbyte) * 256



That'll prolly work. I'm hesitant cause my immediate thought when I see that is "floating point errors", but I may just be paranoid.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

0