• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing out Ascii charactors  RSS feed

 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say I have a program that takes a string "This is a string", and it chops it up into a byte array, then I perform some bit shifting to get a nibble from bit 11 to bit 40. I then put that number into an Int or Long variabe depending on the size, and output in a variety of ways.

Currently I can print the value as binary, hex, decimal, and can do some boolean conversion. I know this printing is all relative since the number is the same, but I can't get it to print the Ascii text for these chunks I am taking?

Any ideas?
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer or int?
There is not such a thing as an Int, and the distinction between int and Integer is important here.

assuing an int i:

Since there are control-characters from 0-31, the output will look funny in some cases, like tab, vertical tab, backspace, ...
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is i is is holding the decimal values for four chars? Will they print?

In my case I am using a long variable since their is no unsigned int in Java and I need the 32nd bit.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On first read, I didn't follow what you're trying to do.

Moving this to the Intermediate forum...
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone wanna jump on this grenade?

 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ascii is only defined for 0-127.
Are you looking for utf-32?
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I want it to print in Ascii.

I think I need a better example..

I pass "CAT" into my parser. It creates a byte array. I then tell the parser to return a decimal value for Start bit = 5, size = 8

"CAT" = 067,065,084 or in my case. 01000011|01000001|01010100
My return value would be "00110100" = 026, or the ascii value of 2

I want to print the Ascii value of the returned value which is stored in a Long..

Thanks!
 
Susanta Chatterjee
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, ASCII value of '2' is not 026, rather 062 (octal) which is actually 50 decimal. Hence, I did not quite follow your bit manipulation, how you derived character '2'.

But in general, if the native long/int variable is 'aChar', then to print out corresponding ASCII character, do this:
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice catch on the Decimal value. I had a typo.

Ok let me see if I undertstand.

if I have a long/int containing the Decimal value for a char use:


What if My Int/Long contains more then one char?

Thanks,
 
Susanta Chatterjee
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To do that, bit shift right for upper bits to take them to lower bits and then print them.
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boy I wish their was an easier way. BitShifting will be my undoing...

 
Susanta Chatterjee
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really, while you bit shift, you do not change original variable. take the bit-shift result to perform this, while not changing the original variable.
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My shifting is done through class methods that are only appending to a long value. I am currently using a byte array to hold a string, and then pull out sections from the array dependant upon a StartBit, and Size. This could mean crossing over multiple elements in one iteration (which I can do). It is just taking my final result **A long value** and printing it in Ascii that I am stuck on... So I went from a String, to a byte array, to a Long and hopefully can cast it back into whatever will let me print in Ascii..

Thanks,
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like that:
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
::EDIT:: Cool I figured it out.. Quite a nice little bit of code their.

Do you have any Idea how I can get it to reverse the array?

My input is a Long = 4669780 which = "GAT"

the output of the program is "TAG"


[ September 24, 2004: Message edited by: Hosh Nasi ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well - that's the simple question of reverting an array.
For output:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!