Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : INPUT$ problems
Author | Message | ||||
BobDevries Senior Member Joined: 08/06/2011 Location: AustraliaPosts: 266 |
Hi all, I wonder if someone could shed some light on my problem. I'm using the following code: open "chiptest.bin" for input as #1 do while not(eof(#1)) a$ = input$(2,#1) print val(a$); loop close #1 The above prints values completely unrelated to what is in the file, which is (in HEX): 61 77 62 45 71 01 83....... you get the drift. What am I doing wrong? according to the manual (yes, I did read it first!) INPUT$ should read from the file (#1 in this case) the number of bytes(characters) of its first argument, and return that in the variable. Instead, I get a bunch of 0's, an occasional FFFFFF51, but nothing remotely related to the input file. Regards, Bob Devries Dalby, QLD, Australia |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
Not sure if this is your answer but you are reading two characters into a$ and then trying to convert using VAL. I would read one character at a time. |
||||
bigmik Guru Joined: 20/06/2011 Location: AustraliaPosts: 2914 |
Gday Bob, Val `should' print the numeric VALUE of a string `number' ie. if a$="12" then val(a$) will give 12 but if a$="fe" then that is not a number (in decimal speak) so it will return 0 try ASC(a$) instead... you might need to only read in 1 chr at a time instead of 2 depending on how they are stored in the file. Regards, Mick Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<< |
||||
djuqa Guru Joined: 23/11/2011 Location: AustraliaPosts: 447 |
Simple solution Have to add "&H" so the Val function knows it is in HEX [code] Start: OPEN "chiptest.bin" FOR input AS #1 DO WHILE not(eof(#1)) a$ = input$(2,#1) PRINT val("&H"+a$); LOOP CLOSE #1 [/code] VK4MU MicroController Units |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3801 |
If the comment "in HEX" means you could (in DOS) TYPE the file (in Linux, cat the file) and see what was posted (without the spaces!) then djuqa looks right. You may have to swap each pair of bytes. edit: on re-reading, I don't think the file is in the above format (ASCII HEX might be a reasonable term). Please explain more about what's in the file! John |
||||
BobDevries Senior Member Joined: 08/06/2011 Location: AustraliaPosts: 266 |
Hi all, I took the advice regarding reading 1 byte at a time, and also using the "&H" inside the VAL function. I dunno how I could have forgotten something so simple. Still, I don't understand why reading two bytes at once didn't work. Ah, well. It's all part of a CHIP8 disassembler I'm writing. I still have some issues to fix, but at least it outputs the correct mnemonics. I'll keep you posted. oh, CHIP8? It was used on the Aussie DREAM 6800 computer, which I once owned (SIGH). Ah, the memories Regards, Bob Devries Dalby, QLD, Australia |
||||
djuqa Guru Joined: 23/11/2011 Location: AustraliaPosts: 447 |
Reading 2 bytes is perfectly OK and normal The Interpreter can't parse the string and determine if the character string "12" is Decimal , HEX or Octal without the Prefix. I remember the CHIP8 & dream 6800 fondly . I actually wrote an emulator of it for Apple ][ & clones VK4MU MicroController Units |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3801 |
Suppose the 2 bytes were hex 41 42. What VAL saw was VAL ("AB") which is 0 (or an error, if VAL were defined that way). The default base is naturally 10. Whereas VAL("&HAB") would convert in hex. (If byte-swapping is needed, replace each AB by BA.) John |
||||
Print this page |