swapping the bytes of an integer
swapping the bytes of an integer
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]
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]
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
- Michael Calkins
- Veteran
- Posts: 76
- Joined: Tue Apr 05, 2005 8:40 pm
- Location: Floresville, Texas
- Contact:
Some variation of this might work for you also:Regards, Michael
Code: Select all
i = CVI(RIGHT$(MKI$(i), 1) + LEFT$(MKI$(i), 1))
Bring on the Maulotaurs! oops...
I like to slay Disciples of D'Sparil...
I like to slay Disciples of D'Sparil...
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.
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.
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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
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
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
- Michael Calkins
- Veteran
- Posts: 76
- Joined: Tue Apr 05, 2005 8:40 pm
- Location: Floresville, Texas
- Contact:
and, for the heck of it, here is another:example implementation:
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
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
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
dkg: And thank you for acknowledging the replies. :-)
Regards, Michael
Bring on the Maulotaurs! oops...
I like to slay Disciples of D'Sparil...
I like to slay Disciples of D'Sparil...
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.
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
I = 27917
i = CVI(RIGHT$(MKI$(i), 1) + LEFT$(MKI$(i), 1))
PRINT i
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