swapping the bytes of an integer

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
dkg
Newbie
Posts: 8
Joined: Mon Mar 31, 2008 2:29 am

swapping the bytes of an integer

Post 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]
TmEE
Veteran
Posts: 97
Joined: Mon Mar 17, 2008 11:14 am
Location: Estonia, Rapla
Contact:

Post 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%
Mida sa loed ? Nagunii aru ei saa :P
dkg
Newbie
Posts: 8
Joined: Mon Mar 31, 2008 2:29 am

Post by dkg »

That's exactly what I needed. Many thanks for your quick response.
User avatar
Michael Calkins
Veteran
Posts: 76
Joined: Tue Apr 05, 2005 8:40 pm
Location: Floresville, Texas
Contact:

Post 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
Bring on the Maulotaurs! oops...
I like to slay Disciples of D'Sparil...
dkg
Newbie
Posts: 8
Joined: Mon Mar 31, 2008 2:29 am

Post 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.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
dkg
Newbie
Posts: 8
Joined: Mon Mar 31, 2008 2:29 am

Post 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.
TmEE
Veteran
Posts: 97
Joined: Mon Mar 17, 2008 11:14 am
Location: Estonia, Rapla
Contact:

Post by TmEE »

np, I'm glad I could help :)
Mida sa loed ? Nagunii aru ei saa :P
User avatar
Michael Calkins
Veteran
Posts: 76
Joined: Tue Apr 05, 2005 8:40 pm
Location: Floresville, Texas
Contact:

Post 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
Bring on the Maulotaurs! oops...
I like to slay Disciples of D'Sparil...
dkg
Newbie
Posts: 8
Joined: Mon Mar 31, 2008 2:29 am

Post 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.
TmEE
Veteran
Posts: 97
Joined: Mon Mar 17, 2008 11:14 am
Location: Estonia, Rapla
Contact:

Post 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.
Mida sa loed ? Nagunii aru ei saa :P
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post 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%
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
mestrinho
Coder
Posts: 12
Joined: Mon Mar 31, 2008 10:44 am

Post by mestrinho »

yet another very simple way of doing it :)

i = 27917
i = (i MOD 256) * 256 + i \ 256
print i
TmEE
Veteran
Posts: 97
Joined: Mon Mar 17, 2008 11:14 am
Location: Estonia, Rapla
Contact:

Post by TmEE »

tiny optimization ;)

i% = 27917
i% + (i% AND 255) * 256 + i% \ 256
PRINT i%
Mida sa loed ? Nagunii aru ei saa :P
Post Reply