473,383 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Data conversion

Hello,

I need to get a 2-byte number into a string, but I DON'T want VB to
obligingly turn it into a string - in other words, I want to end up
with 2 bytes in the string, which is the number as held in the int16
variable, not an ASCII representation of its value.

I have tried doing stuff like

str = chr((no >> 8)) & chr((no and 256))

but this is giving me problems unstringing it again, on a Pocket PC.
Can anyone help, please?

Thanks,
Peter Royle
Nov 20 '05 #1
9 2453
Hi Peter,

I'm not sure what the issues are with unpacking. Have a go with this:

I = &hFACE
S = Chr(I \ 256 And 255) & Chr(I And 255)
I = Asc (S.Chars (0)) * 256 + Asc (S.Chars (1))

Regards,
Fergus
Nov 20 '05 #2
"Peter Royle" <pj*****@tiscali.co.uk> wrote in message
news:fc*************************@posting.google.co m...
.. . .
I need to get a 2-byte number into a string .. . . I have tried doing stuff like
str = chr((no >> 8)) & chr((no and 256)) .. . . this is giving me problems unstringing it again


The "encoding" looks fine to me - what problems are are getting
at the other end? If it's an overflow problem, you might try this:

no = Val("&H" _
& Right("0" & Hex(Asc(str.Substring(0, 1))), 2) _
& Right("0" & Hex(Asc(str.Substring(1, 1))), 2) _
)

HTH,
Phill W.
Nov 20 '05 #3
"Peter Royle" <pj*****@tiscali.co.uk> schrieb
I need to get a 2-byte number into a string, but I DON'T want VB
to obligingly turn it into a string - in other words, I want to end
up with 2 bytes in the string, which is the number as held in the
int16 variable, not an ASCII representation of its value.

I have tried doing stuff like

str = chr((no >> 8)) & chr((no and 256))
why not ...chr(no and 255)?
but this is giving me problems unstringing it again, on a Pocket
PC. Can anyone help, please?


Nov 20 '05 #4
pj*****@tiscali.co.uk (Peter Royle) scripsit:
I need to get a 2-byte number into a string, but I DON'T want VB to
obligingly turn it into a string - in other words, I want to end up
with 2 bytes in the string, which is the number as held in the int16
variable, not an ASCII representation of its value.

I have tried doing stuff like

str = chr((no >> 8)) & chr((no and 256))


\\\
Dim i As Short = 123
MsgBox(i.ToStrint())
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #5
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in message news:<ei**************@TK2MSFTNGP11.phx.gbl>...
pj*****@tiscali.co.uk (Peter Royle) scripsit:
I need to get a 2-byte number into a string, but I DON'T want VB to
obligingly turn it into a string - in other words, I want to end up
with 2 bytes in the string, which is the number as held in the int16
variable, not an ASCII representation of its value.

I have tried doing stuff like

str = chr((no >> 8)) & chr((no and 256))


\\\
Dim i As Short = 123
MsgBox(i.ToStrint())
///


I thought my original message was clear enough, but apparently not.
This is a beautiful example of a fundamental technique that I learnt
about 5 years ago - putting a number into a string - and it's wrong -
what is Strint()? Fortunately, the other responders have been helpful.
Nov 20 '05 #6
"Armin Zingler" <az*******@freenet.de> wrote in message news:<ef*************@tk2msftngp13.phx.gbl>...
"Peter Royle" <pj*****@tiscali.co.uk> schrieb
I need to get a 2-byte number into a string, but I DON'T want VB
to obligingly turn it into a string - in other words, I want to end
up with 2 bytes in the string, which is the number as held in the
int16 variable, not an ASCII representation of its value.

I have tried doing stuff like

str = chr((no >> 8)) & chr((no and 256))


why not ...chr(no and 255)?

Armin, you're quite right. This appears to be a typo. Sorry!

Peter
Nov 20 '05 #7
pj*****@tiscali.co.uk (Peter Royle) scripsit:
I need to get a 2-byte number into a string, but I DON'T want VB to
obligingly turn it into a string - in other words, I want to end up
with 2 bytes in the string, which is the number as held in the int16
variable, not an ASCII representation of its value.

I have tried doing stuff like

str = chr((no >> 8)) & chr((no and 256))


\\\
Dim i As Short = 123
MsgBox(i.ToStrint())
///


I thought my original message was clear enough, but apparently not.
This is a beautiful example of a fundamental technique that I learnt
about 5 years ago - putting a number into a string - and it's wrong -
what is Strint()? Fortunately, the other responders have been helpful.


Sorry for the typo... I misunderstood you (I am not a native English
speaker). Why not use 'Chr' or 'ChrW' directly?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #8
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in message news:<OL**************@TK2MSFTNGP10.phx.gbl>...
pj*****@tiscali.co.uk (Peter Royle) scripsit:
> I need to get a 2-byte number into a string, but I DON'T want VB to
> obligingly turn it into a string - in other words, I want to end up
> with 2 bytes in the string, which is the number as held in the int16
> variable, not an ASCII representation of its value.
>
> I have tried doing stuff like
>
> str = chr((no >> 8)) & chr((no and 256))

\\\
Dim i As Short = 123
MsgBox(i.ToStrint())
///


I thought my original message was clear enough, but apparently not.
This is a beautiful example of a fundamental technique that I learnt
about 5 years ago - putting a number into a string - and it's wrong -
what is Strint()? Fortunately, the other responders have been helpful.


Sorry for the typo... I misunderstood you (I am not a native English
speaker). Why not use 'Chr' or 'ChrW' directly?


Herfried - I understand perfectly. I was getting a bit wound up
yesterday - sorry!

I have tried using ascW and chrW (it is a 2-byte value) - it works on
the PC, but fails on the Pocket PC - Argument out of range. That's why
I am trying to manipulate the bytes separately - but as soon as you
use Asc, you get a signed, 2-byte integer - and so, I think, the
Pocket PC software is seeing the leftmost bit as a sign bit. So if I
send it 127 or 256, it extracts them fine. Any value with a 1 in the
leftmost bit - 128-255, for example - is just seen as 63. I can do
this easily on the PC - it's the Pocket PC that is fouling up,
somehow.

Peter
Nov 20 '05 #9
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in message news:<OL**************@TK2MSFTNGP10.phx.gbl>...
pj*****@tiscali.co.uk (Peter Royle) scripsit:
> I need to get a 2-byte number into a string, but I DON'T want VB to
> obligingly turn it into a string - in other words, I want to end up
> with 2 bytes in the string, which is the number as held in the int16
> variable, not an ASCII representation of its value.
>
> I have tried doing stuff like
>
> str = chr((no >> 8)) & chr((no and 256))

\\\
Dim i As Short = 123
MsgBox(i.ToStrint())
///


I thought my original message was clear enough, but apparently not.
This is a beautiful example of a fundamental technique that I learnt
about 5 years ago - putting a number into a string - and it's wrong -
what is Strint()? Fortunately, the other responders have been helpful.


Sorry for the typo... I misunderstood you (I am not a native English
speaker). Why not use 'Chr' or 'ChrW' directly?


A follow up to my last posting - the problem was not "Argument out of
range" - that was my doing. Now, using ChrW and AscW, it sees anything
over 127 wrongly. Again, it all works fine on the PC!!

Peter
Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Dillon Mantle | last post by:
HI All Does any one know how to convert a binary file into a readable (by human) file? Its from a bin file, has addresses and stuff which I can work out... but how do you do the actual...
10
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this...
6
by: New MSSQL DBA | last post by:
Hi all, we are now planning to upgrade our application from a non-unicode version to a unicode version. The application's backend is a SQL Server 2000 SP3. The concern is, existing business...
4
by: Ivan | last post by:
I am running a statically bound COBOL program on DB2 7.1 OS/390 trying to insert EBCDIC strings into a varchar column in a table on DB2 UDB 7.2 FP10a Workgroup edition. The varchar column is...
4
by: wb | last post by:
Newbie at .net having trouble with data conversion. I am using the random constructor and trying to pass in seed value of time as instructed by help. rvalue = new random(int) dim x as long...
1
by: tony.fountaine | last post by:
I am working on a project to read a Bosch Measurement Data File (MDF). The file contains a number of blocks that can be read from the file using a baisc structure. For example the ID BLOCK is as...
0
by: bpo_ccs | last post by:
We are into BPO and Software development business for past six and half years. We are looking for the following any kind of business from your end. Back Office Process Data Entry, Large Volume...
4
by: alacrite | last post by:
I have a class that I want to turn its contents into csv file. I want to be able to set the value of the delimiter, the name of the file it gets saved to, the path of that file, and maybe a few...
23
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
0
by: dataentryoffshore | last post by:
Get a Discount up to 60% on data entry, data capture, dataentry services, large volume data processing and data conversion services through offshore facilities in India. Offshore data entry also...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.