Page 1 of 1

swapping the bytes of an integer

Posted: Thu Apr 03, 2008 7:45 am
by dkg
I do appologise if this is answered elsewhere, I did try the forum search first.

Is there a quick, elegant way of switching MSB with LFB in an integer?
eg &h6D0D (27917) to &h076D(3437)

I though of something like this, but it seems a little long winded.
[code]
DIM a AS INTEGER
open "somefile" for binary as #1
get #1 , ,a
ahex$=hex$(a)
b=VAL("&H"+right$(ahex$,1)+left$(ahex$,1))
[/code]
I then do something with b and need to convert it back for writing to the file again. Thanks in advance for any help.
[/code]

Posted: Thu Apr 03, 2008 8:38 am
by TmEE
This should be efficient...

Code: Select all

ADDR& = VARPTR(the_integer%)
A% = PEEK(ADDR&)
B% = PEEK(ADDR& + 1)
POKE ADDR&, B%
POKE ADDR& + 1, A%

Posted: Thu Apr 03, 2008 8:53 am
by dkg
That's exactly what I needed. Many thanks for your quick response.

Posted: Thu Apr 03, 2008 12:55 pm
by Michael Calkins
Some variation of this might work for you also:

Code: Select all

i = CVI(RIGHT$(MKI$(i), 1) + LEFT$(MKI$(i), 1))
Regards, Michael

Posted: Thu Apr 03, 2008 4:21 pm
by dkg
I appreciate your response Michael. I've been playing around with Ted's suggestion and, after a few bugs when using the code in a function, have reached my goal.
Despite having accoumplished what I needed, I will experiment with your code too. With the goal of familiarising myself with features of the language that I wasn't taught in my college computer studies course (CVI MKI$.....)

Many thanks to the both of you for your help.

Posted: Thu Apr 03, 2008 9:52 pm
by burger2227
CVI converts a string value to an Integer

MKI$ converts an Integer to a string.

They are often used together. There are other CV and MK functions for other types of number values.

Ted

Posted: Fri Apr 04, 2008 4:00 am
by dkg
Sorry TmEE. I rushed my last reply. I meant to say "I've been playing around with TmEE's suggestion". It works a dream.

Michael, thankyou for opening my eyes to the CVI/MKI$ functions, in this case it's not quite what I wanted though.

Again I thank you all for your helfpull and friendly posts.

Posted: Fri Apr 04, 2008 5:46 am
by TmEE
np, I'm glad I could help :)

Posted: Sat Apr 05, 2008 12:53 am
by Michael Calkins
and, for the heck of it, here is another:

Code: Select all

;public domain, 2008 Michael Calkins
;call with parameter type: SEG i% (far pointer to integer)
cpu 386
org 0x0

push bp
mov bp,sp
push ds
mov dx,[bp+0x8] ;segment
mov ds,dx
mov bx,[bp+0x6] ;offset
mov ax,[bx]
xchg al,ah
mov [bx],ax
pop ds
pop bp
retf 0x4
example implementation:

Code: Select all

'public domain, 2008 Michael Calkins
DEFINT A-Z
CONST l = 23
DIM c AS STRING * l
DIM t AS STRING
DIM i AS INTEGER

t = "5589E51E8B56088EDA8B5E068B0786C489071F5DCA0400"

IF LEN(t) <> (l * 2) THEN SYSTEM
FOR i = 0 TO l - 1
MID$(c, i + 1, 1) = CHR$(VAL("&h" + MID$(t, i * 2 + 1, 2)))
NEXT i

DO
 INPUT i
 DEF SEG = VARSEG(c)
 CALL absolute(SEG i, VARPTR(c))        'pass far pointer
 PRINT "&h"; HEX$(i)
LOOP WHILE i
TmEE: I'm a little curious about using POKE without DEF SEG... Is it already in the correct segment?

dkg: And thank you for acknowledging the replies. :-)

Regards, Michael

Posted: Sat Apr 05, 2008 10:01 am
by dkg
Thankyou for your efforts. Your last post is very much out of my legue (for now at least).

It's reasuring to know that advanced help is available, should I ever need it.

As always, thankyou for your time.

Posted: Sat Apr 05, 2008 10:54 am
by TmEE
Michael Calkins wrote:TmEE: I'm a little curious about using POKE without DEF SEG... Is it already in the correct segment?
Variables are in the same segment as the code is, usually... if there's any issues, just set up DEF SEG.

Posted: Sat Apr 05, 2008 8:03 pm
by Ralph
Michael Cakins' one liner really did the trick! When I added one line at the beginning, and one at the end, it showed the answer as 3437, the correct value.

Code: Select all

I = 27917
i = CVI(RIGHT$(MKI$(i), 1) + LEFT$(MKI$(i), 1))
PRINT i
TmEE's code, when I added the line at the beginning,
theinteger%=27917
and, at the end,
PRINT theinteger
it showed the answer as 3437. also the correct answer.

Code: Select all

theinteger% = 27917
ADDR& = VARPTR(theinteger%) 
A% = PEEK(ADDR&) 
B% = PEEK(ADDR& + 1) 
POKE ADDR&, B% 
POKE ADDR& + 1, A%
PRINT theinteger%

Posted: Wed Apr 09, 2008 7:27 pm
by mestrinho
yet another very simple way of doing it :)

i = 27917
i = (i MOD 256) * 256 + i \ 256
print i

Posted: Wed Apr 09, 2008 7:48 pm
by TmEE
tiny optimization ;)

i% = 27917
i% + (i% AND 255) * 256 + i% \ 256
PRINT i%